OpenSpace execute-code-fallback
Fallback from execute_code_sandbox to run_shell when e2b sandbox is unavailable
git clone https://github.com/HKUDS/OpenSpace
T=$(mktemp -d) && git clone --depth=1 https://github.com/HKUDS/OpenSpace "$T" && mkdir -p ~/.claude/skills && cp -r "$T/gdpval_bench/skills/execute-code-fallback" ~/.claude/skills/hkuds-openspace-execute-code-fallback && rm -rf "$T"
gdpval_bench/skills/execute-code-fallback/SKILL.mdExecute Code Fallback Pattern
When
execute_code_sandbox fails due to e2b unavailability, immediately fall back to run_shell with embedded Python code. This ensures task continuity when the sandbox service is down.
When to Apply
Use this pattern when you observe:
returns errors mentioning "e2b", "sandbox unavailable", or connection timeoutsexecute_code_sandbox- Repeated sandbox execution failures (2+ attempts)
- Error messages indicating the code execution environment is unreachable
Fallback Procedure
Step 1: Detect the Failure
Identify that
execute_code_sandbox has failed. Common error indicators:
- "e2b service unavailable"
- "Sandbox connection failed"
- "Execution environment not reachable"
- Timeout errors during code execution
Step 2: Switch to run_shell with Embedded Python
Instead of:
execute_code_sandbox(code="...")
Use:
run_shell(command="python3 -c '...your Python code...'")
Step 3: Install Dependencies First (If Needed)
If your Python code requires external packages, install them first:
run_shell(command="pip install pandas requests matplotlib")
Then execute your main code:
run_shell(command="python3 << 'EOF' import pandas as pd import requests # Your code here print("Success") EOF ")
Step 4: Use Heredoc for Multi-line Code
For complex Python scripts, use heredoc syntax for cleaner code:
run_shell(command="python3 << 'PYTHON_SCRIPT' import json import os # Complex logic here data = {'key': 'value'} with open('output.json', 'w') as f: json.dump(data, f) print('File created successfully') PYTHON_SCRIPT ")
Complete Example
Scenario: You need to process a CSV file and generate a report.
Original approach (sandbox):
execute_code_sandbox(code=""" import pandas as pd df = pd.read_csv('data.csv') summary = df.describe() print(summary) """)
Fallback approach (run_shell):
# First install dependencies if needed run_shell(command="pip install pandas --quiet") # Then execute the code run_shell(command="python3 << 'EOF' import pandas as pd df = pd.read_csv('data.csv') summary = df.describe() print(summary) EOF ")
Important Considerations
-
State Persistence: Unlike
,execute_code_sandbox
executions may not share state between calls. Save intermediate results to files if needed.run_shell -
Working Directory: Ensure you're operating in the correct directory. Use
to verify or includepwd
in your commands.cd /path/to/workdir -
Python Version: Use
explicitly to avoid ambiguity. Verify withpython3
if needed.python3 --version -
Error Handling: Check the stdout/stderr from
to confirm success. Failed Python scripts will return non-zero exit codes.run_shell -
Security: Be cautious when embedding user-provided data into shell commands. Escape appropriately or use file-based input.
-
Performance: For large computations,
may be slower than sandbox. Consider breaking into smaller steps if timeouts occur.run_shell
Quick Reference
| Task | Sandbox Approach | Fallback Approach |
|---|---|---|
| Simple calculation | | |
| Install + run | | |
| Multi-line script | | |
| File I/O | | |
Recovery Checklist
- Confirm
failure (not a code bug)execute_code_sandbox - Switch to
immediatelyrun_shell - Install required packages with
pip install - Use heredoc for multi-line Python
- Verify output and handle errors
- Save intermediate results to files if multi-step