sandbox-fallback-python
它能做什么
这项技能指导 AI 智能体在无法连接沙箱时,通过 shell 运行 Python。它适合 Python 任务依赖软件包并需要输出文件的人。
适用场景
- ✓当沙箱报告不可用或连接失败的时候。
- ✓当 Python 脚本需要先安装软件包的时候。
- ✓当脚本必须读写工作文件夹中的文件的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/sandbox-fallback-python/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“沙箱不可用。请安装我的脚本所需的软件包,然后运行它,并把输出保存到我的工作文件夹中。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会用 pip 安装软件包,然后借助 heredoc 在 shell 中运行 Python 代码。它对文件使用明确的路径,并列出文件夹,以确认输出已经保存。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Sandbox Fallback: Python via run_shell
When to Use This Skill
Apply this skill when execute_code_sandbox fails with errors indicating:
- e2b service unavailability
- Sandbox timeout or connection issues
- Code execution environment not accessible
This fallback allows you to execute Python code directly via shell, bypassing the sandbox infrastructure.
Fallback Procedure
Step 1: Detect Sandbox Failure
Identify fallback necessity from error messages like:
- "e2b unavailable"
- "sandbox connection failed"
- "execution environment error"
- Timeout errors during code execution
Step 2: Switch to run_shell with Embedded Python
Instead of execute_code_sandbox, use run_shell with a Python heredoc or inline script:
python3 << 'EOF'
# Your Python code here
print("Hello from fallback execution")
EOF
Step 3: Install Dependencies First
If your Python code requires packages not guaranteed to be installed:
pip install package_name package_name2 && python3 << 'EOF'
# Your Python code here
import package_name
print("Code with dependencies executed")
EOF
Step 4: Handle File I/O
For scripts that need to read/write files:
- Use absolute or explicit relative paths
- Files persist in the workspace directory
- List directory contents with
lsto verify file operations
python3 << 'EOF'
import os
# Write output to file
with open("output.txt", "w") as f:
f.write("Results here")
# Verify
print(f"Working directory: {os.getcwd()}")
EOF
Code Examples
Simple Calculation
python3 << 'EOF'
result = sum(range(100))
print(f"Sum: {result}")
EOF
With Package Installation
pip install requests -q && python3 << 'EOF'
import requests
response = requests.get("https://api.example.com/data")
print(response.json())
EOF
With JSON Processing
python3 << 'EOF'
import json
data = {"key": "value", "count": 42}
output = json.dumps(data, indent=2)
print(output)
# Save to file
with open("data.json", "w") as f:
f.write(output)
EOF
Best Practices
- Use heredoc syntax -
<< 'EOF'prevents variable expansion issues - Install quietly - Use
pip install -qto reduce output noise - Error handling - Add try/except blocks in Python code for robustness
- Verify execution - Check stdout/stderr from run_shell to confirm success
- Keep code compact - Long scripts are harder to debug in shell context
Caveats
- No persistent state between run_shell calls (unlike execute_code_sandbox)
- Longer execution time for complex scripts due to Python interpreter startup
- Limited debugging - Cannot easily inspect variables between statements
- Output size limits - Very large outputs may be truncated
When NOT to Use This Fallback
- If the task specifically requires sandbox isolation for security
- If you need persistent state across multiple code executions
- If the code execution is extremely long-running (consider backgrounding)
常见问题
智能体如何检查输出?
它会读取 shell 的消息,并列出文件夹以查看新生成的文件。
为什么要使用明确的路径?
明确的路径可以确保文件保存在你预期的工作文件夹中。
如果不需要安装任何东西也能用吗?
如果软件包已经存在,可以跳过安装步骤。
详情
作者的原始描述
Fallback to run_shell with embedded Python when execute_code_sandbox fails due to e2b unavailability
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12992
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。