code-execution-fallback
What it does
This skill covers checking the working folder before code runs, and falling back to shell execution when needed. It is for people running AI-written scripts who find files saved in the wrong place.
When to use it
- βWhen files keep appearing in the wrong folder during an AI task.
- βWhen the sandbox fails repeatedly and a simpler version of the code does not fix it.
- βWhen I want the AI to run Python through the shell, with the code written inline.
Skill link
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/code-execution-fallback/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agentβs skills folder.
Example of what to ask your AI
βMy AI keeps saving files in the wrong place and the sandbox keeps failing. Please fix the folder and run the script from the shell.β
5 new skills a week. 1 minute to read.
How it works
The AI first checks its working folder, tries simpler code, and then moves to a shell run with the code written inline. It checks the output to confirm the run succeeded.
Technical content (original SKILL.md)
Original text by the author, straight from 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
FAQ
What is an inline shell run?
It means passing several lines of Python to a shell command in one block, instead of using the sandbox.
Why check the working folder first?
It keeps files from being written to unexpected places when the AI switches tools.
What comes before the shell switch?
The guide suggests trying simpler code first, then switching to shell execution if it still fails.
Details
Original description by the author
Handle code execution failures with fallback strategies and anchored workspace paths
repo: HKUDS/AgentPilot
Popularity
- GitHub stars
- 7.8k
- Ranking position
- #12847
- License
- MIT
- Updated
- 11/1/2026
Comments
- β¦
More skills from this repository
agentpilot
7.8kHelps your agent improve a project step by step by preparing a task and running the AgentPilot loop that proposes, tests and scores changes.
openspace
7.8kHelps your agent set up OpenSpace, a tool that stores and fetches agent skills, and connect it to your AI app through MCP.
rsi-arena
7.8kHelps your agent set up and run standard AI benchmark tests, such as AIME and Terminal-Bench, to compare different improvement methods.
experiment-guide
7.8kHelps your agent plan small experiments, read their results and record findings while it investigates how to improve a program.
Similar skills
diffs
394kLets your agent produce a real, shareable diff of edits as a viewer link or image file, instead of a hand-written summary.
coding-agent
394kHelps your agent hand a larger coding job to Codex, Claude Code, or OpenCode as a background worker, then check on its progress.
gh-issues
394kHelps your agent find GitHub issues, pick the ones to work on, and open pull requests with fixes, including follow-up on review comments.