Fallback from execute_code_sandbox to file-based run_shell execution when sandbox fails
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-sandbox-execution-fallback-717e65-24b5bb03f3e7 ,按照其中的说明把「sandbox-execution-fallback-717e65」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
Apply this pattern when execute_code_sandbox fails repeatedly or exhibits instability. Common triggers include:
Recognize the failure pattern:
execute_code_sandbox produces repeated errors despite code correctionsUse write_file to save your script:
write_file(
path="script.py",
content="#!/usr/bin/env python3
# Your Python code here
import sys
print('Executing via file-based approach')
# ... rest of your code
"
)
For multi-file projects, write each file separately:
write_file(path="utils.py", content="# Utility functions\ndef helper(): ...")
write_file(path="main.py", content="from utils import helper\nhelper()")
Run the script using run_shell:
run_shell(command="python3 script.py")
For scripts in subdirectories:
run_shell(command="cd mydir && python3 script.py")
run_shell outputread_file if neededrun_shell(command="rm script.py")
| Benefit | Explanation |
|---|---|
| Bypasses provider limitations | No sandbox resource constraints |
| Better error visibility | Full stack traces and system errors |
| Environment control | Direct access to system Python and packages |
| Multi-file support | Easy imports and module structure |
| Persistence | Files remain for inspection and debugging |
| Reliability | More consistent execution behavior |
# Scenario: execute_code_sandbox failing on data processing task
# Step 1: Write the script
write_file(
path="process_data.py",
content="#!/usr/bin/env python3
import json
import csv
# Load and process data
with open('input.json', 'r') as f:
data = json.load(f)
# Transform data
results = []
for item in data:
results.append({'processed': item['value'] * 2})
# Write output
with open('output.csv', 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=['processed'])
writer.writeheader()
writer.writerows(results)
print('Processing complete')
"
)
# Step 2: Execute via shell
run_shell(command="python3 process_data.py")
# Step 3: Read results
read_file(filetype="csv", file_path="output.csv")
# Step 4: Clean up (optional)
run_shell(command="rm process_data.py")
| Issue | Solution |
|---|---|
python3 not found | Try python or specify full path /usr/bin/python3 |
| Import errors | Use run_shell(command="pip3 install package") first |
| Permission denied | Add execute permission: run_shell(command="chmod +x script.py") |
| Working directory issues | Use absolute paths or cd in the command |