reliable-script-execution
它能做什么
这项技能告诉 AI 智能体,在运行 Python 代码之前先把它保存为文件。它适合内联运行的 Python 总是因未知错误而失败的人。
适用场景
- ✓当内联 Python 代码因未知错误失败,尤其是多行脚本的时候。
- ✓当代码中有许多引号或特殊字符,导致内联运行出错的时候。
- ✓当你希望保留所运行脚本的记录的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/reliable-script-execution/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请编写一个简短的 Python 脚本,生成我的开支 JSON 报告,保存后运行它。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会把代码写入一个清晰文件夹中的 .py 文件,然后通过 shell 在设定的时间限制内运行它。任务完成后,它可以删除这个临时文件。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Reliable Script Execution
When executing Python code via shell commands, avoid inline heredoc execution which can fail unpredictably with 'unknown error'. Use this two-step file-first approach for more reliable script execution.
Problem
Direct heredoc Python execution like:
python3 << 'EOF'
# complex code here
EOF
Can fail with 'unknown error', especially when:
- Script contains multiple lines or complex logic
- Special characters or quotes are present
- Working directory context matters
Solution: File-First Approach
Step 1: Write Python Script to File
Use write_file to save your Python code to a .py file:
write_file(path="./temp_script.py", content="""
import json
data = {"key": "value"}
print(json.dumps(data))
""")
Step 2: Execute via Shell
Use run_shell with explicit working directory:
run_shell(command="python3 ./temp_script.py", timeout=60)
Step 3: Clean Up (Optional)
Remove temporary files after execution:
run_shell(command="rm ./temp_script.py")
Complete Example
Task: Generate a JSON report with calculations
# Step 1: Write the script
write_file(
path="./generate_report.py",
content="""
import json
from datetime import datetime
revenue = 500000.00
expenses = 379577.06
net_income = revenue - expenses
report = {
"generated": datetime.now().isoformat(),
"revenue": revenue,
"expenses": expenses,
"net_income": net_income
}
print(json.dumps(report, indent=2))
"""
)
# Step 2: Execute
run_shell(command="python3 ./generate_report.py", timeout=60)
# Step 3: Clean up
run_shell(command="rm ./generate_report.py")
Best Practices
Use descriptive filenames: Name scripts according to their purpose (e.g.,
calculate_pnl.py,transform_data.py)Set appropriate timeouts: For data processing scripts, use longer timeouts (60-300 seconds)
Specify working directory: If the script depends on relative paths, include
cd /path && python3 script.pyHandle errors gracefully: Check shell output for errors and retry if needed
Clean up temporary files: Don't leave
.pyfiles cluttering the workspace unless they need to persist
When to Use This Pattern
- Executing multi-line Python code via shell
- Scripts with complex string handling or special characters
- When heredoc execution has failed previously
- Any scenario where reliability matters more than brevity
Anti-Pattern to Avoid
Do NOT rely on heredoc for production-critical scripts:
# Unreliable - may fail with 'unknown error'
python3 << 'EOF'
# Your code here
EOF
Use file-first approach instead for consistent, debuggable execution.
常见问题
为什么文件比内联代码更可靠?
这项技能指出,内联 heredoc 运行可能出现不可预测的失败,而保存的文件则给出更清晰的结果。
之后应该删除文件吗?
这项技能说明清理是可选的,因此如果你想保留记录,也可以保留文件。
我可以设置时间限制吗?
可以。这项技能的示例会向 shell 命令传入超时参数。
详情
作者的原始描述
Execute Python scripts reliably using file-first approach instead of heredoc
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12969
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。