execute-code-fallback
Fallback from execute_code_sandbox to run_shell when e2b sandbox is unavailable
- License 许可
- MIT
- 所属仓库
- HKUDS/AgentPilot
- 最近更新
- 2026-11-12
- Stars
- ★ 8.0k
- Forks
- 944
01 前言 Front Matter
- name
- execute-code-fallback
- description
- Fallback from execute_code_sandbox to run_shell when e2b sandbox is unavailable
02 资源文件 1
- 其他.skill_id35 B
03 SKILL.md 正文
Execute 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:
execute_code_sandboxreturns errors mentioning "e2b", "sandbox unavailable", or connection timeouts- 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,run_shellexecutions may not share state between calls. Save intermediate results to files if needed. -
Working Directory: Ensure you're operating in the correct directory. Use
pwdto verify or includecd /path/to/workdirin your commands. -
Python Version: Use
python3explicitly to avoid ambiguity. Verify withpython3 --versionif needed. -
Error Handling: Check the stdout/stderr from
run_shellto confirm success. Failed Python scripts will return non-zero exit codes. -
Security: Be cautious when embedding user-provided data into shell commands. Escape appropriately or use file-based input.
-
Performance: For large computations,
run_shellmay be slower than sandbox. Consider breaking into smaller steps if timeouts occur.
Quick Reference
| Task | Sandbox Approach | Fallback Approach |
|---|---|---|
| Simple calculation | execute_code_sandbox(code="print(2+2)") |
run_shell(command="python3 -c 'print(2+2)'") |
| Install + run | execute_code_sandbox(code="import pkg; ...") |
run_shell(command="pip install pkg && python3 -c '...'") |
| Multi-line script | execute_code_sandbox(code="...") |
run_shell(command="python3 << 'EOF'...EOF") |
| File I/O | execute_code_sandbox(code="...") |
run_shell(command="python3 << 'EOF'...EOF") |
Recovery Checklist
- [ ] Confirm
execute_code_sandboxfailure (not a code bug) - [ ] Switch to
run_shellimmediately - [ ] 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
04 同仓库更多 Skills
agentpilot
Set up AgentPilot and use its single entry to prepare missing inputs and run configured RSI experiments. Use when the user requests AgentPilot installation or work with AgentPilot.
remember
Review auto-memory entries and propose promotions to OPENSPACE.md, OPENSPACE.local.md, or retained auto-memory topics; detect duplicates, stale facts, conflicts, and ambiguous entries before making changes.
skill-discovery
Search reusable OpenSpace skills locally, and browse cloud packages step by step when a cloud skill may help. Reusing proven skills saves tokens, improves reliability, and extends your capabilities beyond built-in tools.
delegate-task
Delegate tasks to OpenSpace — a full-stack autonomous worker for coding, DevOps, web research, and desktop automation, backed by an extensive MCP tool and skill library. Skills auto-improve through use, reducing token consumption over time. A cloud community lets agents share and collectively evolve reusable skills.
write-special-chars-ts-check
Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, plus a reliable TypeScript type-checking step that filters output to only the newly-written files to avoid being misled by pre-existing errors.
write-special-chars-readback
Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, with mandatory post-write verification via read_file and cleanup of /tmp helper scripts.