# Code Execution Fallback

Handle code execution failures with fallback strategies and anchored workspace paths

- Repository: https://github.com/HKUDS/AgentPilot (7784 stars)
- Category: Engineering
- License: MIT
- Last updated: 2026-11-01
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add HKUDS/AgentPilot --skill code-execution-fallback -g -a claude-code -y`
Codex: `npx skills add HKUDS/AgentPilot --skill code-execution-fallback -g -a codex -y`
Cursor: `npx skills add HKUDS/AgentPilot --skill code-execution-fallback -g -a cursor -y`
Gemini CLI: `npx skills add HKUDS/AgentPilot --skill code-execution-fallback -g -a gemini-cli -y`

## Description

Handle code execution failures with fallback strategies and anchored workspace paths

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# 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:

```python
# At the beginning of any code execution
import os
workspace_path = os.getcwd()
print(f"Working directory: {workspace_path}")
```

```bash
# 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:

```bash
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:

```python
# BAD - relies on implicit working directory
df.to_csv("output/data.csv")

The rest: https://github.com/HKUDS/AgentPilot/blob/HEAD/openspace/benchmarks/gdpval/skills/code-execution-fallback/SKILL.md
