‹ 首页

execute-code-fallback

@hkuds · 收录于 5 天前 · 上游提交 1 周前

Fallback from execute_code_sandbox to run_shell when e2b sandbox is unavailable

适合你,如果需要在沙箱不可用时自动改用本地命令执行代码

/ 通过 npx 安装 校验哈希
npx oh-my-skill add hkuds/openspace/execute-code-fallback
/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- hkuds/openspace/execute-code-fallback
/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify hkuds/openspace/execute-code-fallback
安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
6920GitHub stars
~850上下文体积 · 单文件
索引托管

怎么用

商店整理自技能原文 · 版本 2c5cc40 · 表述以原文为准
它做什么

当e2b沙箱不可用时,自动放弃execute_code_sandbox,改用run_shell运行Python代码,确保任务继续。

什么时候触发

当execute_code_sandbox因e2b沙箱不可用而失败(例如返回“e2b service unavailable”等错误,或连续失败两次以上)时触发。

装好后可以这样说
触发回退机制。
用heredoc传递多行代码。
先安装依赖后执行。
技能原文 SKILL.md作者撰写 · MIT · 2c5cc40

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_sandbox returns 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
  1. State Persistence: Unlike execute_code_sandbox, run_shell executions may not share state between calls. Save intermediate results to files if needed.
  1. Working Directory: Ensure you're operating in the correct directory. Use pwd to verify or include cd /path/to/workdir in your commands.
  1. Python Version: Use python3 explicitly to avoid ambiguity. Verify with python3 --version if needed.
  1. Error Handling: Check the stdout/stderr from run_shell to confirm success. Failed Python scripts will return non-zero exit codes.
  1. Security: Be cautious when embedding user-provided data into shell commands. Escape appropriately or use file-based input.
  1. Performance: For large computations, run_shell may 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_sandbox failure (not a code bug)
  • [ ] Switch to run_shell immediately
  • [ ] 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
按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。