Skip to content

Self-improving AI Employees for everyone

feat(python_executor): pre-inject documented sandbox modules

trohitgc63755542cfd
diff --git a/server/nodes/code/python_executor.py b/server/nodes/code/python_executor.py
index e3648d74..3598fed7 100644
--- a/server/nodes/code/python_executor.py
+++ b/server/nodes/code/python_executor.py
@@ -24,9 +24,13 @@ class PythonExecutorNode(CodeExecutorBase):
results are reachable; ``workspace_dir`` is the per-workflow
scratch directory.
"""
+ import datetime as datetime_module
import io
import json as json_module
import math
+ import random as random_module
+ import re as re_module
+ from collections import Counter, defaultdict
if not params.code.strip():
raise RuntimeError("No code provided")
@@ -46,7 +50,17 @@ class PythonExecutorNode(CodeExecutorBase):
"range": range, "round": round, "set": set, "sorted": sorted,
"str": str, "sum": sum, "tuple": tuple, "type": type, "zip": zip,
"True": True, "False": False, "None": None,
- "math": math, "json": json_module,
+ # Pre-injected modules — match the documented sandbox contract
+ # (skill docs, CLAUDE.md). No __import__: callers reference these
+ # by name, e.g. ``datetime.datetime.now()`` not ``import datetime``.
+ "math": math,
+ "json": json_module,
+ "datetime": datetime_module,
+ "timedelta": datetime_module.timedelta,
+ "re": re_module,
+ "random": random_module,
+ "Counter": Counter,
+ "defaultdict": defaultdict,
}
namespace = {
"__builtins__": safe_builtins,
diff --git a/server/skills/coding_agent/python-skill/SKILL.md b/server/skills/coding_agent/python-skill/SKILL.md
index c70ca808..b4f9bf25 100644
--- a/server/skills/coding_agent/python-skill/SKILL.md
+++ b/server/skills/coding_agent/python-skill/SKILL.md
@@ -29,16 +29,18 @@ Execute Python code and return results.
### Available Libraries
-| Library | Description |
-|---------|-------------|
-| `math` | Mathematical functions (sqrt, pow, sin, cos, etc.) |
-| `json` | JSON encoding/decoding |
-| `datetime` | Date and time manipulation |
-| `timedelta` | Time duration calculations |
-| `re` | Regular expressions |
-| `random` | Random number generation |
-| `Counter` | Count hashable objects |
-| `defaultdict` | Dictionary with default values |
+All modules below are **pre-injected as names** in the sandbox. **Do not use `import` statements** — they will fail (`ImportError: __import__ not found`). Reference each name directly.
+
+| Name | Reference Style | Description |
+|------|-----------------|-------------|
+| `math` | `math.sqrt(2)` | Mathematical functions |
+| `json` | `json.loads(s)` / `json.dumps(d)` | JSON encoding/decoding |
+| `datetime` | `datetime.datetime.now()` (the **module**) | Date/time module |
+| `timedelta` | `timedelta(days=30)` | Duration class (already unwrapped) |
+| `re` | `re.findall(pat, text)` | Regular expressions |
+| `random` | `random.randint(1, 10)` | Random number generation |
+| `Counter` | `Counter(items)` | Count hashable objects (already unwrapped) |
+| `defaultdict` | `defaultdict(list)` | Dictionary with defaults (already unwrapped) |
### Built-in Variables
@@ -71,14 +73,14 @@ Execute Python code and return results.
**Generate random numbers:**
```json
{
- "code": "import random\nnumbers = [random.randint(1, 100) for _ in range(5)]\nprint(f'Random numbers: {numbers}')\noutput = numbers"
+ "code": "numbers = [random.randint(1, 100) for _ in range(5)]\nprint(f'Random numbers: {numbers}')\noutput = numbers"
}
```
**Date calculations:**
```json
{
- "code": "from datetime import datetime, timedelta\ntoday = datetime.now()\nfuture = today + timedelta(days=30)\nresult = future.strftime('%Y-%m-%d')\nprint(f'30 days from now: {result}')\noutput = result"
+ "code": "today = datetime.datetime.now()\nfuture = today + timedelta(days=30)\nresult = future.strftime('%Y-%m-%d')\nprint(f'30 days from now: {result}')\noutput = result"
}
```
@@ -92,21 +94,21 @@ Execute Python code and return results.
**Parse JSON:**
```json
{
- "code": "import json\njson_str = '{\"name\": \"John\", \"age\": 30}'\ndata = json.loads(json_str)\nprint(f'Name: {data[\"name\"]}')\noutput = data"
+ "code": "json_str = '{\"name\": \"John\", \"age\": 30}'\ndata = json.loads(json_str)\nprint(f'Name: {data[\"name\"]}')\noutput = data"
}
```
**Count items:**
```json
{
- "code": "from collections import Counter\nitems = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']\ncounts = dict(Counter(items))\nprint(f'Counts: {counts}')\noutput = counts"
+ "code": "items = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']\ncounts = dict(Counter(items))\nprint(f'Counts: {counts}')\noutput = counts"
}
```
**Text processing:**
```json
{
- "code": "import re\ntext = 'Contact: [email protected] or [email protected]'\nemails = re.findall(r'\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b', text)\nprint(f'Found emails: {emails}')\noutput = emails"
+ "code": "text = 'Contact: [email protected] or [email protected]'\nemails = re.findall(r'\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b', text)\nprint(f'Found emails: {emails}')\noutput = emails"
}
```