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

sandbox-fallback-python-e5d7ae

它能做什么

这项技能说明,当沙箱无法启动或连接时,AI 智能体如何通过 shell 运行 Python 代码。它适合使用 reportlab 或 pandas 等库的人。

适用场景

  • ✓当沙箱显示初始化、启动或连接错误的时候。
  • ✓当一个简短的脚本需要立即在 shell 中运行的时候。
  • ✓当较长的脚本必须保存为文件,并以更长的超时时间运行的时候。

技能链接

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/sandbox-fallback-python-e5d7ae/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

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

可以这样问 AI

“沙箱无法启动。请通过在 shell 中运行脚本,用 reportlab 为我制作 PDF 发票。”

每周 5 个新技能,1 分钟读完。

工作原理

对于简短的代码,智能体会在 shell 命令中使用 heredoc。对于较长的代码,它会把脚本写入临时文件并运行,必要时给任务更多的时间。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Python Sandbox Fallback Pattern

When to Use

Use this pattern when execute_code_sandbox fails with e2b/sandbox initialization errors, especially when running Python code that requires external libraries like reportlab, pandas, numpy, etc.

Common Failure Indicators

Detect these error patterns to trigger the fallback:

  • e2b initialization errors
  • sandbox creation/creation timeout failures
  • container startup errors
  • Connection/refusal errors from sandbox service

The Fallback Pattern

Basic Heredoc Approach

For short scripts, use a Python heredoc directly in run_shell:

run_shell(
    command="""python3 << 'EOF'
import sys
print(f"Python version: {sys.version}")
# Your code here
EOF
""",
    timeout=60
)

File-Based Approach for Complex Scripts

For longer scripts or when you need to preserve state across multiple executions:

# Step 1: Write the script to a temporary file
script_content = '''
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
import os

# Your code here
'''

run_shell(command=f"cat > /tmp/script.py << 'SCRIPT_EOF'\n{script_content}\nSCRIPT_EOF")

# Step 2: Execute the script
result = run_shell(command="python3 /tmp/script.py", timeout=120)

Key Considerations

Timeouts

  • run_shell typically needs longer timeouts than execute_code_sandbox
  • Use timeout=60 for simple scripts
  • Use timeout=120 or higher for library-heavy operations (PDF generation, data processing)

Library Availability

  • Verify required libraries are installed in the shell environment
  • Install missing libraries first if needed:
    run_shell(command="pip install reportlab", timeout=60)
    

Error Handling

  • Capture and inspect run_shell output for Python errors
  • Check stderr for import failures or runtime exceptions

Working Directory

  • Files created via run_shell are in the current working directory by default
  • Use absolute paths or os.getcwd() to reference files reliably

Example: PDF Generation with reportlab

# FALLBACK VERSION (when execute_code_sandbox fails):

pdf_code = """
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch

def create_memo(filename):
    c = canvas.Canvas(filename, pagesize=letter)
    c.drawString(1*inch, 10*inch, "Legal Memorandum")
    c.save()
    print(f"Created {filename}")

create_memo('/workspace/output.pdf')
"""

# Write script to file
run_shell(command=f"cat > /tmp/create_pdf.py << 'PYEOF'\n{pdf_code}\nPYEOF")

# Execute and verify
run_shell(command="python3 /tmp/create_pdf.py", timeout=120)

# Verify output exists
run_shell(command="ls -la /workspace/output.pdf", timeout=30)

Comparison Table

Aspect execute_code_sandbox run_shell (fallback)
Isolation High (containerized) Low (host environment)
Reliability Can fail with e2b errors More reliable
Library Access Pre-configured sandbox Depends on host
Timeout Default 30s Configurable
File Access ARTIFACT_PATH convention Direct filesystem

Migration Checklist

When switching from execute_code_sandbox to run_shell:

  • Increase timeout values (typically 2-4x)
  • Convert ARTIFACT_PATH references to actual file paths
  • Add explicit file verification steps after execution
  • Handle potential library import errors
  • Test script execution in shell environment first
常见问题

为什么要使用更长的超时时间?

这项技能指出,shell 运行通常比沙箱运行需要更多时间。

临时文件保存在哪里?

这项技能的示例把它写入一个临时文件夹,但智能体也可以选择其他位置。

它提到了哪些库?

它提到 reportlab、pandas 和 numpy,作为代码可能需要的库的例子。

详情

作者的原始描述

Fallback to run_shell with Python heredoc when execute_code_sandbox fails

repo: HKUDS/AgentPilot

热度

GitHub 星标
7.8k
排名
#12991
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能