code-execution-fallback
できること
このスキルは、コードを実行する前に作業フォルダを確認することと、必要に応じてシェルでの実行に切り替えることを扱います。AIが書いたスクリプトを実行していて、ファイルが誤った場所に保存されてしまう人向けです。
使う場面
- ✓AIのタスク中に、ファイルが誤ったフォルダに現れ続けるとき。
- ✓サンドボックスが繰り返し失敗し、コードを単純にしても直らないとき。
- ✓コードをインラインで書いたまま、シェルからPythonを実行したいとき。
スキルのリンク
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/code-execution-fallback/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“AIがファイルを誤った場所に保存し続け、サンドボックスも失敗し続けています。フォルダを直して、シェルからスクリプトを実行してください。”
毎週5つの新しいスキル。1分で読めます。
仕組み
AIはまず作業フォルダを確認し、より単純なコードを試し、それでも失敗すればコードをインラインで書いてシェルで実行します。実行が成功したかを、出力で確かめます。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
Code Execution Fallback & Workspace Anchoring
This skill provides a robust pattern for executing code when the primary method fails, combined with proper workspace path management to prevent file location errors.
Core Techniques
1. Workspace Path Anchoring
Always establish and verify your working directory at the start of any task:
# At the beginning of any code execution
import os
workspace_path = os.getcwd()
print(f"Working directory: {workspace_path}")
# In shell scripts
pwd
echo "Current directory: $(pwd)"
Why: Prevents files from being written to unexpected locations when agents switch between tools.
2. Execution Fallback Ladder
When execute_code_sandbox fails, follow this escalation pattern:
Level 1: Retry with Simpler Code
- Simplify the code structure
- Remove complex dependencies
- Add explicit error handling
Level 2: Use run_shell with Heredoc
When sandbox execution repeatedly fails, switch to shell execution:
python3 << 'EOF'
import os
import pandas as pd
# Your code here
data = {"col1": [1, 2, 3], "col2": ["a", "b", "c"]}
df = pd.DataFrame(data)
df.to_csv("output.csv", index=False)
print("File written successfully")
EOF
Key points:
- Use
<< 'EOF'(quoted) to prevent variable expansion - Include all imports and dependencies inline
- Add explicit success/failure messages
Level 3: Delegate to shell_agent
For complex multi-step tasks with error recovery needs:
Task: Create a data processing pipeline that reads CSV, transforms data, and outputs Excel
Requirements:
- Handle missing values
- Apply transformations
- Write to ./output/ directory
- Retry on transient errors
3. Explicit Path Management
Always use absolute or explicitly relative paths:
# BAD - relies on implicit working directory
df.to_csv("output/data.csv")
# GOOD - explicit path anchoring
import os
base_path = os.getcwd()
output_dir = os.path.join(base_path, "output")
os.makedirs(output_dir, exist_ok=True)
df.to_csv(os.path.join(output_dir, "data.csv"))
# BAD
cd some_dir && python script.py
# GOOD
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
python script.py
Decision Tree
execute_code_sandbox fails?
├── Yes, with syntax/import errors → Fix code, retry Level 1
├── Yes, with timeout/resource errors → Use Level 2 (run_shell heredoc)
├── Yes, with unknown/unclear errors → Use Level 3 (shell_agent)
└── No, success → Verify output file exists at expected path
Common Failure Scenarios & Solutions
| Error Type | Likely Cause | Recommended Fallback |
|---|---|---|
| ModuleNotFoundError | Missing packages | run_shell with pip install first |
| Timeout | Long-running operation | shell_agent with progress tracking |
| PermissionError | Wrong directory | Verify workspace path, use explicit paths |
| Unknown error | Sandbox limitations | run_shell or shell_agent |
Example: Robust File Generation
# Step 1: Anchor workspace
import os
workspace = os.getcwd()
print(f"Workspace: {workspace}")
# Step 2: Create output directory explicitly
output_path = os.path.join(workspace, "deliverables")
os.makedirs(output_path, exist_ok=True)
# Step 3: Generate content with error handling
try:
# Your generation logic here
with open(os.path.join(output_path, "report.txt"), "w") as f:
f.write("Content here")
print(f"Success: File written to {output_path}")
except Exception as e:
print(f"Error: {e}")
# Signal to escalate to run_shell or shell_agent
raise
Anti-Patterns to Avoid
- ❌ Assuming current directory without verification
- ❌ Using relative paths like
../output/file.txtwithout context - ❌ Repeatedly retrying failed
execute_code_sandboxwithout changing approach - ❌ Not checking if output files exist after generation
- ❌ Mixing implicit and explicit path styles in same task
Verification Checklist
After any code execution:
- Confirm working directory was verified at start
- Confirm output files exist at expected paths
- Confirm file contents are non-empty and valid
- If execution failed, escalate to next fallback level within 2 retries
よくある質問
インラインのシェル実行とは何ですか?
サンドボックスを使わず、数行のPythonコードを一つのシェルコマンドにまとめて渡すことです。
なぜ先に作業フォルダを確認するのですか?
AIがツールを切り替えたときに、ファイルが想定外の場所に書かれるのを防ぐためです。
シェルへの切り替えの前に何をしますか?
ガイドは、まずより単純なコードを試し、それでも失敗したらシェルでの実行に切り替えるよう勧めています。
詳細
作者による元の説明
Handle code execution failures with fallback strategies and anchored workspace paths
repo: HKUDS/AgentPilot
人気
- GitHubのスター
- 7.8k
- ランキング
- #12847
- ライセンス
- MIT
- 更新日
- 2026/11/1
コメント
- …
このリポジトリの他のスキル
agentpilot
7.8kエージェントが、作業を準備し、変更案を提案してテストし、評価するAgentPilotのループを実行することで、プロジェクトを段階的に改善できるよう支援します。
openspace
7.8kエージェントが、スキルを保存し取り出すツールであるOpenSpaceをセットアップし、MCPを通じてAIアプリに接続できるよう支援します。
rsi-arena
7.8kエージェントが、AIMEやTerminal-Benchなどの標準的なAIベンチマークテストをセットアップして実行し、異なる改善方法を比較できるよう支援します。
experiment-guide
7.8kエージェントが、プログラムを改善する方法を調べる間に、小さな実験を計画し、その結果を読み、発見を記録できるよう支援します。
似ているスキル
diffs
394k編集内容の実際の共有可能な差分を、手書きの要約ではなく、ビューアーのリンクや画像ファイルとして作成するようエージェントを支援します。
coding-agent
394kCodex、Claude Code、OpenCodeに大きめのコーディング作業をバックグラウンドのワーカーとして渡し、その進捗を確認するようエージェントを支援します。
gh-issues
394kGitHubのイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。