File size: 22,043 Bytes
ab07cb1 1c9cb5b ab07cb1 1c9cb5b ab07cb1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 | """
Superb Python REPL Tool
=======================
A persistent Python execution environment for the agent.
Uses a SUBPROCESS for true process isolation β can be cleanly killed on timeout.
PLOT CAPTURE: When running in web mode, plots are captured via callback.
"""
import sys
import io
import json
import logging
import gc
import os
import re
import base64
import tempfile
import subprocess
import threading
import traceback
import matplotlib
# Force non-interactive backend to prevent crashes on headless servers
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors # Pre-import for custom colormaps
logger = logging.getLogger(__name__)
import matplotlib.cm as cm # Pre-import for colormap access
# =============================================================================
# PUBLICATION-GRADE LIGHT THEME (white background for academic papers)
# =============================================================================
_EURUS_STYLE = {
# ββ Figure ββ
"figure.figsize": (10, 6),
"figure.dpi": 150,
"figure.facecolor": "white",
"figure.edgecolor": "white",
"savefig.facecolor": "white",
"savefig.edgecolor": "white",
"savefig.dpi": 300, # 300 DPI for print-quality
"savefig.bbox": "tight",
"savefig.pad_inches": 0.15,
# ββ Axes ββ
"axes.facecolor": "white",
"axes.edgecolor": "#333333",
"axes.labelcolor": "#1a1a1a",
"axes.titlecolor": "#000000",
"axes.labelsize": 12,
"axes.titlesize": 14,
"axes.titleweight": "bold",
"axes.titlepad": 12,
"axes.grid": True,
"axes.spines.top": False,
"axes.spines.right": False,
"axes.linewidth": 0.8,
# ββ Grid ββ
"grid.color": "#d0d0d0",
"grid.alpha": 0.5,
"grid.linewidth": 0.5,
"grid.linestyle": "--",
# ββ Ticks ββ
"xtick.color": "#333333",
"ytick.color": "#333333",
"xtick.labelsize": 10,
"ytick.labelsize": 10,
"xtick.direction": "out",
"ytick.direction": "out",
# ββ Text ββ
"text.color": "#1a1a1a",
"font.family": "sans-serif",
"font.sans-serif": ["DejaVu Sans", "Arial", "Helvetica"],
"font.size": 11,
# ββ Lines ββ
"lines.linewidth": 1.8,
"lines.antialiased": True,
"lines.markersize": 5,
# ββ Legend ββ
"legend.facecolor": "white",
"legend.edgecolor": "#cccccc",
"legend.fontsize": 10,
"legend.framealpha": 0.95,
"legend.shadow": False,
# ββ Colorbar ββ
"image.cmap": "viridis",
# ββ Patches ββ
"patch.edgecolor": "#333333",
}
matplotlib.rcParams.update(_EURUS_STYLE)
# Curated color cycle for white backgrounds (high-contrast, publication-safe)
_EURUS_COLORS = [
"#1f77b4", # steel blue
"#d62728", # brick red
"#2ca02c", # forest green
"#ff7f0e", # orange
"#9467bd", # muted purple
"#17becf", # cyan
"#e377c2", # pink
"#8c564b", # brown
]
matplotlib.rcParams["axes.prop_cycle"] = matplotlib.cycler(color=_EURUS_COLORS)
from typing import Dict, Optional, Type, Callable
from pathlib import Path
from pydantic import BaseModel, Field
from langchain_core.tools import BaseTool
# Import PLOTS_DIR for correct plot saving location
from eurus.config import PLOTS_DIR
# Pre-import common scientific libraries for convenience (parent-side only)
import pandas as pd
import numpy as np
import xarray as xr
from datetime import datetime, timedelta
# =============================================================================
# PERSISTENT SUBPROCESS REPL
# =============================================================================
# The Python script that runs inside the subprocess.
# It receives JSON commands on stdin and sends JSON responses on stdout.
_SUBPROCESS_SCRIPT = r'''
import sys
import os
import json
import gc
from io import StringIO
# Apply Eurus matplotlib style INSIDE the subprocess
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import matplotlib.cm as cm
_style = json.loads(os.environ.get("EURUS_MPL_STYLE", "{}"))
if _style:
matplotlib.rcParams.update(_style)
_colors = json.loads(os.environ.get("EURUS_MPL_COLORS", "[]"))
if _colors:
matplotlib.rcParams["axes.prop_cycle"] = matplotlib.cycler(color=_colors)
# Pre-import scientific stack
import pandas as pd
import numpy as np
import xarray as xr
from datetime import datetime, timedelta
# Set up execution globals with pre-loaded libraries
exec_globals = {
"__builtins__": __builtins__,
"pd": pd,
"np": np,
"xr": xr,
"plt": plt,
"mcolors": mcolors,
"cm": cm,
"datetime": datetime,
"timedelta": timedelta,
"PLOTS_DIR": os.environ.get("EURUS_PLOTS_DIR", "plots"),
}
# Signal readiness
print("SUBPROCESS_READY", flush=True)
while True:
try:
line = input()
if line == "EXIT_SUBPROCESS":
break
cmd = json.loads(line)
if cmd["type"] == "exec":
code = cmd["code"]
stdout_capture = StringIO()
stderr_capture = StringIO()
old_stdout, old_stderr = sys.stdout, sys.stderr
try:
sys.stdout = stdout_capture
sys.stderr = stderr_capture
# Try eval first (expression mode), fall back to exec
try:
compiled = compile(code, "<repl>", "eval")
result = eval(compiled, exec_globals)
output = stdout_capture.getvalue()
if result is not None:
output += repr(result)
if not output.strip():
output = repr(result) if result is not None else "(No output)"
except SyntaxError:
# Jupyter-style: auto-print last expression in multi-line code
import ast as _ast
try:
tree = _ast.parse(code)
if tree.body and isinstance(tree.body[-1], _ast.Expr):
# Separate the last expression from preceding stmts
last_expr_node = tree.body.pop()
if tree.body:
exec(compile(_ast.Module(body=tree.body, type_ignores=[]), "<repl>", "exec"), exec_globals)
result = eval(compile(_ast.Expression(body=last_expr_node.value), "<repl>", "eval"), exec_globals)
output = stdout_capture.getvalue()
if result is not None:
output += repr(result) if not output.strip() else "\n" + repr(result)
else:
exec(code, exec_globals)
output = stdout_capture.getvalue()
except SyntaxError:
exec(code, exec_globals)
output = stdout_capture.getvalue()
if not output.strip():
output = "(Executed successfully. Use print() to see results.)"
sys.stdout, sys.stderr = old_stdout, old_stderr
result_json = {
"status": "success",
"stdout": output.strip(),
"stderr": stderr_capture.getvalue(),
}
except Exception as e:
sys.stdout, sys.stderr = old_stdout, old_stderr
import traceback
result_json = {
"status": "error",
"error": f"Error: {str(e)}\n{traceback.format_exc()}",
"stdout": stdout_capture.getvalue(),
"stderr": stderr_capture.getvalue(),
}
finally:
plt.close("all")
gc.collect()
print(json.dumps(result_json), flush=True)
except EOFError:
break
except Exception as e:
# Fatal error in the communication loop itself
old_stdout = sys.__stdout__
sys.stdout = old_stdout
print(json.dumps({"status": "fatal", "error": str(e)}), flush=True)
'''
class PersistentREPL:
"""
Manages a persistent Python subprocess for code execution.
Provides true process isolation with clean kill on timeout.
"""
def __init__(self, working_dir: str = "."):
self._working_dir = working_dir
self._process: Optional[subprocess.Popen] = None
self._temp_script: Optional[str] = None
self._lock = threading.Lock() # Serialize access per instance
self._start_subprocess()
def _start_subprocess(self):
"""Start a new Python subprocess with Eurus environment."""
# Write the subprocess script to a temp file
with tempfile.NamedTemporaryFile(
mode="w", suffix=".py", delete=False, prefix="eurus_repl_"
) as f:
f.write(_SUBPROCESS_SCRIPT)
self._temp_script = f.name
# Build env: inject matplotlib style + PLOTS_DIR
env = os.environ.copy()
env["EURUS_MPL_STYLE"] = json.dumps(
{k: v for k, v in _EURUS_STYLE.items() if isinstance(v, (int, float, str, bool))}
)
env["EURUS_MPL_COLORS"] = json.dumps(_EURUS_COLORS)
env["EURUS_PLOTS_DIR"] = str(PLOTS_DIR)
env["MPLBACKEND"] = "Agg"
env["PYTHONUNBUFFERED"] = "1"
self._process = subprocess.Popen(
[sys.executable, "-u", self._temp_script],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=0,
cwd=self._working_dir if os.path.isdir(self._working_dir) else None,
env=env,
)
# Wait for ready signal
ready_line = self._process.stdout.readline()
if "SUBPROCESS_READY" not in ready_line:
raise RuntimeError(f"Subprocess failed to start: {ready_line!r}")
logger.info("Started REPL subprocess (PID: %d)", self._process.pid)
def _ensure_alive(self):
"""Restart subprocess if it has died."""
if self._process is None or self._process.poll() is not None:
logger.warning("REPL subprocess died β restarting")
self._cleanup_process()
self._start_subprocess()
def set_extra_env(self, env_vars: dict):
"""Inject extra environment variables into the running subprocess."""
if not env_vars:
return
with self._lock:
self._ensure_alive()
# Build a Python command that sets the env vars inside the subprocess
set_cmds = []
for key, value in env_vars.items():
if value: # Only set non-empty values
set_cmds.append(f"os.environ[{key!r}] = {value!r}")
if set_cmds:
code = "import os; " + "; ".join(set_cmds)
cmd = json.dumps({"type": "exec", "code": code}) + "\n"
try:
self._process.stdin.write(cmd)
self._process.stdin.flush()
self._read_with_timeout(5) # consume the response
except Exception as e:
logger.warning("Failed to inject env vars into subprocess: %s", e)
def run(self, code: str, timeout: int = 300) -> str:
"""Execute code in the subprocess. Returns output string."""
with self._lock:
self._ensure_alive()
cmd = json.dumps({"type": "exec", "code": code}) + "\n"
try:
self._process.stdin.write(cmd)
self._process.stdin.flush()
except (BrokenPipeError, OSError) as e:
logger.error("Subprocess stdin broken: %s β restarting", e)
self._cleanup_process()
self._start_subprocess()
return f"Error: REPL subprocess crashed. Please re-run your code."
# Read response with timeout
result_line = self._read_with_timeout(timeout)
if result_line is None:
# Timeout β kill subprocess and restart
logger.warning("REPL execution timed out after %ds β killing subprocess", timeout)
self._kill_subprocess()
self._start_subprocess()
return (
"TIMEOUT ERROR: Execution exceeded "
f"{timeout} seconds ({timeout // 60} min). "
"TIP: Resample data to daily/monthly before plotting "
"(e.g., ds.resample(time='D').mean())."
)
try:
result = json.loads(result_line)
except json.JSONDecodeError:
return f"Error: Malformed response from subprocess: {result_line!r}"
if result["status"] == "success":
output = result.get("stdout", "")
stderr = result.get("stderr", "")
if stderr:
output = f"{output}\n{stderr}" if output else stderr
return output or "(No output)"
elif result["status"] == "error":
return result.get("error", "Unknown error")
else:
return f"Fatal subprocess error: {result.get('error', 'Unknown')}"
def _read_with_timeout(self, timeout: int) -> Optional[str]:
"""Read one line from subprocess stdout with a timeout."""
result = [None]
def _reader():
try:
result[0] = self._process.stdout.readline()
except Exception:
pass
reader_thread = threading.Thread(target=_reader, daemon=True)
reader_thread.start()
reader_thread.join(timeout=timeout)
if reader_thread.is_alive():
return None # Timed out
return result[0] if result[0] else None
def _kill_subprocess(self):
"""Force-kill the subprocess."""
if self._process:
try:
self._process.terminate()
try:
self._process.wait(timeout=3)
except subprocess.TimeoutExpired:
self._process.kill()
self._process.wait(timeout=2)
except Exception as e:
logger.error("Error killing subprocess: %s", e)
self._process = None
def _cleanup_process(self):
"""Clean up subprocess and temp files."""
self._kill_subprocess()
if self._temp_script and os.path.exists(self._temp_script):
try:
os.unlink(self._temp_script)
except OSError:
pass
self._temp_script = None
def _update_plots_dir(self, plots_dir: str):
"""Update the PLOTS_DIR used by the subprocess."""
if self._process and self._process.poll() is None:
try:
# Send a command to update the plots directory in the subprocess
cmd = f"import os; os.environ['EURUS_PLOTS_DIR'] = {plots_dir!r}; PLOTS_DIR = {plots_dir!r}\n"
self._process.stdin.write(cmd)
self._process.stdin.flush()
# Clear the response
self._read_response(timeout=2)
except Exception as e:
logger.warning("Failed to update plots_dir in subprocess: %s", e)
def close(self):
"""Gracefully shutdown the subprocess."""
if self._process and self._process.poll() is None:
try:
self._process.stdin.write("EXIT_SUBPROCESS\n")
self._process.stdin.flush()
self._process.wait(timeout=3)
logger.info("REPL subprocess exited gracefully (PID: %d)", self._process.pid)
except Exception:
self._kill_subprocess()
self._cleanup_process()
# =============================================================================
# LANGCHAIN TOOL
# =============================================================================
class PythonREPLInput(BaseModel):
code: str = Field(description="The Python code to execute.")
class PythonREPLTool(BaseTool):
name: str = "python_repl"
description: str = (
"A Python REPL for data analysis and visualization.\n\n"
"CRITICAL PLOTTING RULES:\n"
"1. ALWAYS save to PLOTS_DIR: plt.savefig(f'{PLOTS_DIR}/filename.png')\n"
"2. Use descriptive filenames (e.g., 'route_risk_map.png')\n"
"\n\n"
"MEMORY RULES:\n"
"1. NEVER use .load() or .compute() on large datasets\n"
"2. Resample multi-year data first: ds.resample(time='D').mean()\n"
"3. Use .sel() to subset data before operations\n\n"
"Pre-loaded: pd, np, xr, plt, mcolors, cm, datetime, timedelta, PLOTS_DIR (string path)"
)
args_schema: Type[BaseModel] = PythonREPLInput
working_dir: str = "."
_repl: Optional[PersistentREPL] = None
_plot_callback: Optional[Callable] = None # For web interface
_displayed_plots: set = set()
_plots_dir: Optional[str] = None # Session-specific plot directory
def __init__(self, working_dir: str = ".", plots_dir: Optional[str] = None, **kwargs):
super().__init__(**kwargs)
self.working_dir = working_dir
self._plot_callback = None
self._displayed_plots = set()
self._plots_dir = plots_dir or str(PLOTS_DIR)
# Ensure the plots directory exists
Path(self._plots_dir).mkdir(parents=True, exist_ok=True)
self._repl = PersistentREPL(working_dir=working_dir)
# Override the subprocess PLOTS_DIR env var to use session-specific dir
if plots_dir:
self._repl._update_plots_dir(plots_dir)
def inject_env(self, env_vars: dict):
"""Inject environment variables into the REPL subprocess (session-scoped)."""
if self._repl:
self._repl.set_extra_env(env_vars)
def set_plot_callback(self, callback: Callable):
"""Set callback for plot capture (used by web interface)."""
self._plot_callback = callback
def close(self):
"""Clean up subprocess resources."""
if self._repl:
self._repl.close()
self._repl = None
def _display_image_in_terminal(self, filepath: str, base64_data: str):
"""Display image in terminal β iTerm2/VSCode inline, or macOS Preview fallback."""
# Skip if already displayed this file in this session
if filepath in self._displayed_plots:
return
self._displayed_plots.add(filepath)
try:
term_program = os.environ.get("TERM_PROGRAM", "")
# iTerm2 inline image protocol (only iTerm2 supports this)
if "iTerm.app" in term_program:
sys.stdout.write(f"\033]1337;File=inline=1;width=auto;preserveAspectRatio=1:{base64_data}\a\n")
sys.stdout.flush()
return
# Fallback: open in Preview on macOS (only in CLI, not web)
if not self._plot_callback and os.path.exists(filepath):
import subprocess as _sp
_sp.Popen(["open", filepath], stdout=_sp.DEVNULL, stderr=_sp.DEVNULL)
except Exception as e:
logger.warning(f"Failed to display image in terminal: {e}")
def _capture_and_notify_plots(self, saved_files: list, code: str = ""):
"""Capture plots and notify via callback."""
for filepath in saved_files:
try:
if os.path.exists(filepath):
with open(filepath, 'rb') as f:
img_data = f.read()
b64_data = base64.b64encode(img_data).decode('utf-8')
# Display in terminal
self._display_image_in_terminal(filepath, b64_data)
# Send to web UI via callback
if self._plot_callback:
self._plot_callback(b64_data, filepath, code)
except Exception as e:
print(f"Warning: Failed to capture plot {filepath}: {e}")
def _run(self, code: str) -> str:
"""Execute the python code in the subprocess and return the output."""
plots_dir = self._plots_dir or str(PLOTS_DIR)
# Snapshot plots directory BEFORE execution
image_exts = {'.png', '.jpg', '.jpeg', '.svg', '.pdf', '.gif', '.webp'}
try:
before_files = {
f: os.path.getmtime(os.path.join(plots_dir, f))
for f in os.listdir(plots_dir)
if os.path.splitext(f)[1].lower() in image_exts
}
except FileNotFoundError:
before_files = {}
# Execute in subprocess
output = self._repl.run(code, timeout=300)
# Detect NEW plot files by comparing directory snapshots
try:
after_files = {
f: os.path.getmtime(os.path.join(plots_dir, f))
for f in os.listdir(plots_dir)
if os.path.splitext(f)[1].lower() in image_exts
}
except FileNotFoundError:
after_files = {}
new_files = []
for fname, mtime in after_files.items():
full_path = os.path.join(plots_dir, fname)
if fname not in before_files or mtime > before_files[fname]:
if full_path not in self._displayed_plots:
new_files.append(full_path)
if new_files:
print(f"π {len(new_files)} plot(s) saved")
self._capture_and_notify_plots(new_files, code)
return output
async def _arun(self, code: str) -> str:
"""Use the tool asynchronously β avoids blocking the event loop."""
import asyncio
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, self._run, code)
|