Fallback workflow for reliable code execution when sandbox fails repeatedly
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-code-exec-fallback-266cba-0bf64c59de4e ,按照其中的说明把「code-exec-fallback-266cba」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
Apply this pattern when you encounter repeated failures with execute_code_sandbox:
Track execution failures. After 2 consecutive failures with execute_code_sandbox, switch to the fallback approach.
Use write_file to save your Python script:
write_file(
path="/workspace/script_name.py",
content="# Your Python code here\nimport sys\n..."
)
Use run_shell to run the script:
run_shell(
command="python /workspace/script_name.py",
timeout=300
)
Parse stdout/stderr from run_shell output to verify success or diagnose issues.
# Instead of this (which may fail):
result = execute_code_sandbox(code="import pandas as pd\n...")
# Use this fallback pattern:
script_content = """
import pandas as pd
import sys
try:
# Your logic here
df = pd.DataFrame({'col': [1, 2, 3]})
print(df.to_csv())
sys.exit(0)
except Exception as e:
print(f"ERROR: {e}", file=sys.stderr)
sys.exit(1)
"""
# Write the script
write_file(path="/workspace/my_script.py", content=script_content)
# Execute via shell
result = run_shell(command="python /workspace/my_script.py", timeout=300)
sys.exit() codesrun_shell default is 30s, increase for heavy operationswrite_file is more reliable for file I/O operationsrun_shell gives you direct control over execution environment