跳到正文
Skillpedia
通用编程7.8k · #12992

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/sandbox-fallback-python/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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 ls to 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

  1. Use heredoc syntax - << 'EOF' prevents variable expansion issues
  2. Install quietly - Use pip install -q to reduce output noise
  3. Error handling - Add try/except blocks in Python code for robustness
  4. Verify execution - Check stdout/stderr from run_shell to confirm success
  5. 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能