Upload fsi_sandbox.py with huggingface_hub
Browse files- fsi_sandbox.py +120 -0
fsi_sandbox.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import os, sys, subprocess, tempfile, json, traceback, time, resource
|
| 3 |
+
|
| 4 |
+
class ExecutionSandbox:
|
| 5 |
+
def __init__(self, engine=None, timeout=5, max_memory_mb=256):
|
| 6 |
+
self.engine = engine
|
| 7 |
+
self.timeout = timeout
|
| 8 |
+
self.max_memory = max_memory_mb * 1024 * 1024
|
| 9 |
+
self.execution_history = []
|
| 10 |
+
|
| 11 |
+
def _run_in_subprocess(self, code, test_input=None):
|
| 12 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
| 13 |
+
f.write(code)
|
| 14 |
+
tmp = f.name
|
| 15 |
+
try:
|
| 16 |
+
script = f"""
|
| 17 |
+
import sys, json, traceback, builtins
|
| 18 |
+
import resource
|
| 19 |
+
resource.setrlimit(resource.RLIMIT_CPU, ({self.timeout}, {self.timeout}))
|
| 20 |
+
resource.setrlimit(resource.RLIMIT_AS, ({self.max_memory}, {self.max_memory}))
|
| 21 |
+
_original = builtins.__import__
|
| 22 |
+
def _restricted(name, *args, **kwargs):
|
| 23 |
+
blocked = ['os.system', 'subprocess', 'socket', 'urllib', 'http.client', 'ftplib']
|
| 24 |
+
if any(b in name for b in blocked):
|
| 25 |
+
raise ImportError(f"Blocked: {{name}}")
|
| 26 |
+
return _original(name, *args, **kwargs)
|
| 27 |
+
builtins.__import__ = _restricted
|
| 28 |
+
result = {{"stdout": "", "stderr": "", "output": None, "error": None, "success": False}}
|
| 29 |
+
old_out, old_err = sys.stdout, sys.stderr
|
| 30 |
+
try:
|
| 31 |
+
from io import StringIO
|
| 32 |
+
sys.stdout, sys.stderr = StringIO(), StringIO()
|
| 33 |
+
ns = {{"__builtins__": builtins}}
|
| 34 |
+
with open("{tmp}") as f:
|
| 35 |
+
exec(f.read(), ns)
|
| 36 |
+
for name, obj in ns.items():
|
| 37 |
+
if callable(obj) and name in ["main", "run", "solve", "test"]:
|
| 38 |
+
try:
|
| 39 |
+
result["output"] = obj("{test_input or ''}") if "{test_input or ''}" else obj()
|
| 40 |
+
except Exception as e:
|
| 41 |
+
result["error"] = str(e)
|
| 42 |
+
result["stdout"] = sys.stdout.getvalue()
|
| 43 |
+
result["stderr"] = sys.stderr.getvalue()
|
| 44 |
+
result["success"] = result["error"] is None
|
| 45 |
+
except Exception as e:
|
| 46 |
+
result["error"] = traceback.format_exc()
|
| 47 |
+
result["success"] = False
|
| 48 |
+
finally:
|
| 49 |
+
sys.stdout, sys.stderr = old_out, old_err
|
| 50 |
+
print(json.dumps(result))
|
| 51 |
+
"""
|
| 52 |
+
proc = subprocess.run([sys.executable, '-c', script], capture_output=True, text=True, timeout=self.timeout+2, cwd='/tmp')
|
| 53 |
+
out = proc.stdout.strip()
|
| 54 |
+
if out:
|
| 55 |
+
try:
|
| 56 |
+
return json.loads(out.split('\n')[-1])
|
| 57 |
+
except:
|
| 58 |
+
return {"stdout": proc.stdout, "stderr": proc.stderr, "error": proc.stderr or "Failed", "success": False}
|
| 59 |
+
return {"stdout": "", "stderr": proc.stderr, "error": proc.stderr or "No output", "success": False}
|
| 60 |
+
except subprocess.TimeoutExpired:
|
| 61 |
+
return {"error": f"Timeout after {self.timeout}s", "success": False}
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return {"error": str(e), "success": False}
|
| 64 |
+
finally:
|
| 65 |
+
try: os.unlink(tmp)
|
| 66 |
+
except: pass
|
| 67 |
+
|
| 68 |
+
def execute_and_verify(self, code, expected_output=None, test_input=None):
|
| 69 |
+
result = self._run_in_subprocess(code, test_input)
|
| 70 |
+
if expected_output is not None:
|
| 71 |
+
actual = result.get("output")
|
| 72 |
+
result["verified"] = True
|
| 73 |
+
result["match"] = actual == expected_output
|
| 74 |
+
result["expected"] = expected_output
|
| 75 |
+
result["actual"] = actual
|
| 76 |
+
else:
|
| 77 |
+
result["verified"] = False
|
| 78 |
+
if self.engine and hasattr(self.engine, '_update_pheromone'):
|
| 79 |
+
success = result.get("success") and result.get("match", True)
|
| 80 |
+
for idx in range(1000):
|
| 81 |
+
self.engine._update_pheromone(idx, success)
|
| 82 |
+
self.execution_history.append({"success": result.get("success"), "match": result.get("match")})
|
| 83 |
+
return result
|
| 84 |
+
|
| 85 |
+
def auto_fix(self, code, error, max_attempts=3):
|
| 86 |
+
if not self.engine:
|
| 87 |
+
return code, {"fixed": False}
|
| 88 |
+
current = code
|
| 89 |
+
for attempt in range(max_attempts):
|
| 90 |
+
prompt = f"Fix this Python code. Error: {error}\n\n```python\n{current}\n```\n\nProvide ONLY the corrected code."
|
| 91 |
+
try:
|
| 92 |
+
fixed = self.engine.generate(prompt, max_tokens=800)
|
| 93 |
+
if "```python" in fixed:
|
| 94 |
+
fixed = fixed.split("```python")[1].split("```")[0]
|
| 95 |
+
elif "```" in fixed:
|
| 96 |
+
fixed = fixed.split("```")[1].split("```")[0]
|
| 97 |
+
result = self._run_in_subprocess(fixed)
|
| 98 |
+
if result.get("success"):
|
| 99 |
+
return fixed, {"fixed": True, "attempts": attempt + 1}
|
| 100 |
+
current = fixed
|
| 101 |
+
error = result.get("error", "Unknown")
|
| 102 |
+
except Exception as e:
|
| 103 |
+
return current, {"fixed": False, "reason": str(e)}
|
| 104 |
+
return current, {"fixed": False, "reason": f"Failed after {max_attempts}"}
|
| 105 |
+
|
| 106 |
+
def get_stats(self):
|
| 107 |
+
total = len(self.execution_history)
|
| 108 |
+
if total == 0:
|
| 109 |
+
return {"total": 0, "success_rate": 0}
|
| 110 |
+
successes = sum(1 for h in self.execution_history if h.get("success"))
|
| 111 |
+
matches = sum(1 for h in self.execution_history if h.get("match"))
|
| 112 |
+
return {"total": total, "successes": successes, "success_rate": successes/total*100, "matches": matches}
|
| 113 |
+
|
| 114 |
+
if __name__ == "__main__":
|
| 115 |
+
s = ExecutionSandbox()
|
| 116 |
+
r1 = s.execute_and_verify("def add(a,b): return a+b\nprint(add(2,3))")
|
| 117 |
+
print(f"Test 1: success={r1['success']}, stdout={r1.get('stdout','')[:50]}")
|
| 118 |
+
r2 = s.execute_and_verify("def div(a,b): return a/b\ndiv(10,0)")
|
| 119 |
+
print(f"Test 2: success={r2['success']}, error={r2.get('error','')[:50]}")
|
| 120 |
+
print(f"Stats: {s.get_stats()}")
|