# Run the setup script
./scripts/setup_python_wasi.sh
# Or manually download
mkdir -p wasm-interpreters
curl -L -o wasm-interpreters/python-3.11.4.wasm \
https://github.com/vmware-labs/webassembly-language-runtimes/releases/download/python%2F3.11.4%2B20230714-11be424/python-3.11.4.wasmAdd to your .env file:
# Python WASI interpreter path
PYTHON_WASI_WASM_PATH=/opt/wasm-interpreters/python-3.11.4.wasm# Rebuild and restart services
docker compose -f deploy/compose/docker-compose.yml down
docker compose -f deploy/compose/docker-compose.yml up -d --build./scripts/submit_task.sh "Execute this Python code: print('Hello from WASI Python')"./scripts/submit_task.sh "Execute Python code to calculate factorial of 10"./scripts/submit_task.sh "Use Python to generate the first 20 Fibonacci numbers"- Python Code Submission: User submits Python code via the orchestrator
- LLM Service Processing: The LLM service identifies Python execution need
- WASI Sandbox Execution: Code runs in the secure WASI sandbox via Agent Core
- Result Return: Output is captured and returned to the user
User Request → Orchestrator → LLM Service → Agent Core (WASI) → Python.wasm
↓
Secure Sandbox
- No network
- Read-only FS
- Memory limits
- Size: ~20MB
- Python Version: 3.11.4
- Standard Library: Full CPython
- Download: See setup script
- Size: 5-10MB
- Compatibility: ~95% CPython
- Best for: Simple scripts
# Download RustPython
curl -L -o wasm-interpreters/rustpython.wasm \
https://github.com/RustPython/RustPython/releases/latest/download/rustpython.wasm- Size: 20MB+
- Features: NumPy, Pandas support
- Note: Requires adaptation for WASI
The deploy/compose/docker-compose.yml already includes:
services:
agent-core:
volumes:
- ./wasm-interpreters:/opt/wasm-interpreters:ro
environment:
- PYTHON_WASI_WASM_PATH=/opt/wasm-interpreters/python-3.11.4.wasm
llm-service:
volumes:
- ./wasm-interpreters:/opt/wasm-interpreters:ro
environment:
- PYTHON_WASI_WASM_PATH=/opt/wasm-interpreters/python-3.11.4.wasmFor local development without Docker:
export PYTHON_WASI_WASM_PATH=$(pwd)/wasm-interpreters/python-3.11.4.wasm- No pip/package installation at runtime
- Limited to standard library
- No native C extensions
- No network access (security feature)
- Read-only filesystem access
- 256MB memory limit (configurable)
- ✅ Standard library (math, json, datetime, etc.)
- ✅ File operations (read-only)
- ✅ String manipulation
- ✅ Data structures
- ✅ Regular expressions
- ✅ Base64 encoding/decoding
- ❌ pip install
- ❌ Network requests
- ❌ Database connections
- ❌ GUI libraries
- ❌ System calls
- ❌ Multi-threading
Solution: Ensure the WASM file is downloaded and path is correct
ls -la wasm-interpreters/python-3.11.4.wasmSolution: Python startup takes ~100-500ms. Complex operations may need timeout adjustment:
# In config/polyagent.yaml
wasi:
execution_timeout_ms: 60000 # 60 secondsSolution: Increase memory limit in config:
wasi:
memory_limit_bytes: 536870912 # 512MBSolution: Only standard library modules are available. External packages must be pre-bundled.
To create a custom Python environment with additional packages:
- Build custom Python WASM with packages
- Replace the default interpreter
- Update PYTHON_WASI_WASM_PATH
Test Python WASM directly:
cd rust/agent-core
echo 'print("Test")' | wasmtime run \
--dir=/tmp \
/path/to/python-3.11.4.wasm -- -c 'import sys; exec(sys.stdin.read())'The WASI sandbox provides strong isolation:
- Memory Safety: Separate linear memory space
- No Network: Network capabilities not granted
- Filesystem: Read-only access to whitelisted paths
- Resource Limits: CPU, memory, and time limits enforced
- No System Calls: Cannot spawn processes or access system
- Startup Time: ~100-500ms for Python interpreter
- Memory Usage: Base ~50MB + your code
- Execution Speed: ~50-70% of native Python
- Best Practices:
- Keep code simple and focused
- Avoid large data structures
- Use efficient algorithms
- Minimize imports
# Fibonacci sequence
def fibonacci(n):
fib = [0, 1]
for i in range(2, n):
fib.append(fib[-1] + fib[-2])
return fib[:n]
print(fibonacci(20))import json
data = {"users": [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]}
processed = json.dumps(data, indent=2)
print(processed)import math
# Calculate compound interest
principal = 1000
rate = 0.05
time = 10
amount = principal * math.pow((1 + rate), time)
print(f"Amount after {time} years: ${amount:.2f}")- Run
./scripts/setup_python_wasi.shto download the interpreter - Test with simple Python code submissions
- Monitor logs for any issues
- Adjust resource limits as needed
For more details, see: