safe-script-execution
它能做什么
这项技能为 AI 智能体提供了一种手动方式,用于在其自动工具无法产生预期输出时创建并运行脚本。它适合希望清楚掌控每一步的人。
适用场景
- ✓当代码沙箱或 shell 智能体反复未能产生你预期的输出的时候。
- ✓当你希望智能体在创建任何文件之前,先确认其工作文件夹的时候。
- ✓当脚本包含会破坏简单命令的引号或特殊字符的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/safe-script-execution/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的脚本在沙箱中一直失败。请用完整路径把它保存为文件并运行,然后告诉我输出是否正确。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会检查当前文件夹,使用带有唯一结束标记的 heredoc 将脚本写入文件,使其可执行,然后通过完整路径运行。之后它会先检查输出,再向你汇报。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Safe Script Execution Fallback
When to Use This Skill
Use this pattern when execute_code_sandbox or shell_agent repeatedly fails to produce expected outputs. This manual fallback provides explicit control over script creation and execution with built-in verification.
Step-by-Step Instructions
Step 1: Verify Working Directory
Before creating any scripts, confirm your current location and permissions:
pwd
ls -la
Document the absolute path. All subsequent file operations should use explicit paths from this point.
Step 2: Create Script via Heredoc
Use shell heredoc syntax to create scripts. This avoids issues with multi-line string escaping:
cat > /full/path/to/script.py << 'HEREDOC_END'
#!/usr/bin/env python3
# Your script content here
# Single quotes around delimiter prevent variable expansion
print("Hello from script")
HEREDOC_END
Key escaping rules:
- Use single quotes around heredoc delimiter (
'EOF') to prevent shell variable expansion - If script contains the delimiter string, choose a different unique delimiter
- For bash scripts containing special characters, escape
$, backticks, and!
Step 3: Make Executable and Run with Explicit Path
chmod +x /full/path/to/script.py
/full/path/to/script.py
Always use the full absolute path, never rely on . or relative paths.
Step 4: Verify Output
Confirm files were created and inspect their properties:
# Check file exists with size
ls -lh /full/path/to/output.file
# For PDFs, inspect metadata
pdfinfo /full/path/to/output.pdf 2>/dev/null || file /full/path/to/output.pdf
# For Word docs, check file type
file /full/path/to/output.docx
Step 5: Error Handling
If execution fails:
- Check script syntax:
python3 -m py_compile /path/to/script.py - Check permissions:
ls -l /path/to/script.py - Check available disk space:
df -h . - Review stderr output carefully
Example: PDF Generation Fallback
# Step 1: Verify directory
pwd
ls -la
# Step 2: Create Python script
cat > /workspace/generate_pdf.py << 'SCRIPT_END'
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
c = canvas.Canvas("/workspace/output.pdf", pagesize=letter)
c.drawString(100, 750, "Generated PDF")
c.save()
SCRIPT_END
# Step 3: Execute
chmod +x /workspace/generate_pdf.py
python3 /workspace/generate_pdf.py
# Step 4: Verify
ls -lh /workspace/output.pdf
pdfinfo /workspace/output.pdf 2>/dev/null || echo "PDF created, pdfinfo unavailable"
Anti-Patterns to Avoid
- ❌ Don't use relative paths like
./script.py- always use absolute paths - ❌ Don't rely on unquoted heredoc delimiters if script contains variables
- ❌ Don't skip verification steps - always confirm output exists and has expected size
- ❌ Don't assume working directory - always
pwdfirst
Troubleshooting Checklist
- Working directory confirmed with
pwd - Script file exists after heredoc creation (
ls -l) - Script has execute permissions (
chmod +x) - Output file exists with non-zero size (
ls -lh) - File type verified (
filecommand or type-specific inspection)
常见问题
为什么用完整路径运行脚本?
完整路径可以避免因相对路径或工作文件夹错误而产生的问题。
为什么 heredoc 使用带引号的结束标记?
引号可以防止 shell 改动脚本中的美元符号或其他符号。
如果脚本中包含结束标记的文字怎么办?
这项技能要求换一个唯一的标记,以免文件被提前截断。
详情
作者的原始描述
Fallback workflow for code execution when automated tools fail, using shell heredoc with verification
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12981
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。