Skip to content

Commit e91b1bb

Browse files
authored
Merge pull request #2 from shinpr/fix/improve-error-reporting
Improve error reporting, timeout, and Codex support
2 parents 9f75e86 + 9dcc965 commit e91b1bb

6 files changed

Lines changed: 118 additions & 130 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# Python
55
__pycache__/
6+
.venv/
67
*.py[cod]
78
*$py.class
89
.pytest_cache/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ To customize: `export SUB_AGENTS_DIR=/custom/path`
287287
| `--agent` | Yes* | Agent definition name from --list |
288288
| `--prompt` | Yes* | Task description to delegate |
289289
| `--cwd` | Yes* | Working directory (absolute path) |
290-
| `--timeout` | No | Timeout ms (default: 300000) |
290+
| `--timeout` | No | Timeout ms (default: 600000) |
291291
| `--cli` | No | Force CLI: `claude`, `cursor-agent`, `codex`, `gemini` |
292292

293293
*Required when not using --list

skills/sub-agents/SKILL.md

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,81 @@ Spawns external CLI AIs (claude, cursor-agent, codex, gemini) as isolated sub-ag
1111
## Resources
1212

1313
- **[run_subagent.py](scripts/run_subagent.py)** - Main execution script
14-
- **[cli-formats.md](references/cli-formats.md)** - CLI output format details
14+
- **[codex.md](references/codex.md)** - Codex-specific setup (permissions, timeout)
1515

16-
**Execution**: Run scripts using absolute path from this skill's directory with your environment's Python.
16+
**Script Path**: Use absolute path `{SKILL_DIR}/scripts/run_subagent.py` where `{SKILL_DIR}` is the directory containing this SKILL.md file.
17+
18+
## CLI-Specific Notes
19+
20+
Check the corresponding reference for your environment:
21+
- **Codex**: Read [references/codex.md](references/codex.md) BEFORE first execution
1722

1823
## Interpreting User Requests
1924

2025
Extract parameters from user's natural language request:
2126

2227
| Parameter | Source |
2328
|-----------|--------|
24-
| --agent | Agent name from user request (if not specified, use --list to show candidates and ask user) |
29+
| --agent | Agent name from user request (see selection rules below) |
2530
| --prompt | Task instruction part (excluding agent specification) |
26-
| --cwd | Current working directory |
31+
| --cwd | Current working directory (absolute path) |
32+
33+
**Agent Selection Rules** (when user doesn't specify agent name):
34+
1. Run `--list` to get available agents
35+
2. **0 agents**: Inform user no agents available, show setup instructions (see [Agent Definition Format](#agent-definition-format))
36+
3. **1 agent**: Auto-select without asking
37+
4. **2+ agents**: Show list with descriptions, ask user to choose
2738

2839
**Example**:
2940
"Run code-reviewer on src/"
3041
`--agent code-reviewer --prompt "Review src/" --cwd $(pwd)`
3142

3243
## When to Use This Skill
3344

34-
| Scenario | Use Sub-Agent | Reason |
35-
|----------|---------------|--------|
36-
| Complex multi-step task | **Yes** | Isolated context prevents interference |
37-
| Parallel investigation | **Yes** | Multiple agents can run simultaneously |
38-
| Task needs fresh context | **Yes** | Sub-agent starts without conversation history |
39-
| Specialized agent definition exists | **Yes** | Leverage pre-defined expert prompts |
40-
| Simple single-step task | No | Direct execution is faster |
41-
| Task requires current conversation context | No | Sub-agent cannot access parent context |
45+
**Use sub-agent when ANY of these apply:**
46+
- Complex multi-step task (isolated context prevents interference)
47+
- Parallel investigation (multiple agents can run simultaneously)
48+
- Task needs fresh context (sub-agent starts without conversation history)
49+
- Specialized agent definition exists (leverage pre-defined expert prompts)
50+
51+
## Important: Permission and Timeout
52+
53+
This script executes external CLIs that require elevated permissions.
54+
55+
**Before first execution:**
56+
1. Request elevated permissions via your CLI's tool parameters
57+
2. Set tool timeout to match `--timeout` argument (default: 600000ms)
58+
59+
**For Codex CLI** (most common permission issues): See [references/codex.md](references/codex.md) for exact JSON parameter format.
4260

4361
## Workflow
4462

63+
### Step 0: Read CLI-Specific Setup (if applicable)
64+
65+
If you are running on Codex, read [references/codex.md](references/codex.md) first.
66+
4567
### Step 1: List Available Agents
4668

4769
**Always list agents first** to discover available definitions:
4870

4971
```bash
50-
python scripts/run_subagent.py --list
72+
scripts/run_subagent.py --list
5173
```
5274

5375
Output:
5476
```json
5577
{"agents": [{"name": "code-reviewer", "description": "Reviews code..."}], "agents_dir": "/path/.agents"}
5678
```
5779

58-
**If agents list is empty**: Agent definitions must be placed in `{cwd}/.agents/` directory.
80+
**If agents list is empty**:
81+
1. Create `{cwd}/.agents/` directory
82+
2. Add agent definition file (see [Agent Definition Format](#agent-definition-format))
83+
3. Re-run `--list` to verify
5984

6085
### Step 2: Execute Agent
6186

6287
```bash
63-
python scripts/run_subagent.py \
88+
scripts/run_subagent.py \
6489
--agent <name> \
6590
--prompt "<task>" \
6691
--cwd <absolute-path>
@@ -74,11 +99,22 @@ Parse JSON output and check `status` field:
7499
{"result": "...", "exit_code": 0, "status": "success", "cli": "claude"}
75100
```
76101

102+
**By status:**
103+
77104
| status | Meaning | Action |
78105
|--------|---------|--------|
79106
| `success` | Task completed | Use `result` directly |
80107
| `partial` | Timeout but has output | Review partial `result`, may need retry |
81-
| `error` | Execution failed | Check `error` field, fix and retry |
108+
| `error` | Execution failed | Check `error` field and `exit_code`, fix and retry |
109+
110+
**By exit_code** (when status is `error`):
111+
112+
| exit_code | Meaning | Resolution |
113+
|-----------|---------|------------|
114+
| 0 | Success | - |
115+
| 124 | Timeout | Increase `--timeout` or simplify task |
116+
| 127 | CLI not found | Install required CLI (claude, codex, etc.) |
117+
| 1 | General error | Check `error` field in response |
82118

83119
## Parameters
84120

@@ -88,7 +124,7 @@ Parse JSON output and check `status` field:
88124
| `--agent` | Yes* | Agent definition name from --list |
89125
| `--prompt` | Yes* | Task description to delegate |
90126
| `--cwd` | Yes* | Working directory (absolute path) |
91-
| `--timeout` | No | Timeout ms (default: 300000) |
127+
| `--timeout` | No | Timeout ms (default: 600000) |
92128
| `--cli` | No | Force CLI: `claude`, `cursor-agent`, `codex`, `gemini` |
93129

94130
*Required when not using --list
@@ -131,20 +167,11 @@ How results should be structured.
131167
3. Auto-detect caller environment
132168
4. Default: `codex`
133169

134-
## Error Handling
135-
136-
| exit_code | Meaning | Resolution |
137-
|-----------|---------|------------|
138-
| 0 | Success | - |
139-
| 124 | Timeout | Increase `--timeout` or simplify task |
140-
| 127 | CLI not found | Install required CLI (claude, codex, etc.) |
141-
| 1 | General error | Check `error` field in response |
142-
143-
## Anti-patterns
170+
## Common Mistakes
144171

145-
| Anti-pattern | Problem | Correct Approach |
146-
|--------------|---------|------------------|
147-
| Not listing agents first | Unknown agent name causes error | Always run `--list` first |
148-
| Relative path for --cwd | Validation fails | Use absolute path |
149-
| Ignoring status field | Missing errors | Always check status before using result |
172+
| Mistake | Result | Fix |
173+
|---------|--------|-----|
174+
| Skip `--list` before execution | Agent not found error | Always run `--list` first |
175+
| Use relative path for `--cwd` | Validation fails | Use absolute path |
176+
| Ignore `status` field in response | Undetected errors | Always check `status` before using `result` |
150177
| Very long prompts | May exceed CLI limits | Break into smaller tasks |

skills/sub-agents/references/cli-formats.md

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Codex-Specific Notes
2+
3+
Read this reference when executing sub-agents from Codex CLI.
4+
5+
## Where to Set Parameters
6+
7+
Codex CLI uses **Bash tool call parameters** for permissions and timeout. These are NOT command-line arguments—they are tool invocation settings.
8+
9+
```json
10+
{
11+
"tool": "Bash",
12+
"parameters": {
13+
"command": "/path/to/scripts/run_subagent.py --agent <name> --prompt \"<task>\" --cwd /absolute/path --timeout 600000",
14+
"timeout_ms": 600000,
15+
"sandbox_permissions": "require_escalated"
16+
}
17+
}
18+
```
19+
20+
## Permission: Request Escalated BEFORE First Execution
21+
22+
Sub-agent CLIs require access to session files outside the sandbox.
23+
24+
**Setting:** `"sandbox_permissions": "require_escalated"` in Bash tool parameters.
25+
26+
Always request escalated permissions on the first run. Sandboxed runs fail with:
27+
```
28+
Error: Fatal error: Codex cannot access session files at ~/.codex/sessions (permission denied)
29+
```
30+
31+
## Timeout: Match timeout_ms with --timeout
32+
33+
Codex's Bash tool has its own timeout (`timeout_ms`), separate from the script's `--timeout` argument.
34+
35+
**Setting:** `"timeout_ms": 600000` in Bash tool parameters (must match `--timeout` value).
36+
37+
| --timeout | timeout_ms | Result |
38+
|-----------|------------|--------|
39+
| Not set | Not set | Script: 600000ms, Tool: ~10000ms → Tool kills script early |
40+
| 600000 | Not set | Same problem |
41+
| 600000 | 600000 | Correct: both aligned |
42+
43+
## Common Errors
44+
45+
| Error | Cause | Solution |
46+
|-------|-------|----------|
47+
| `Operation not permitted (os error 1)` | Sandbox restriction | Use `sandbox_permissions:"require_escalated"` |
48+
| `(no output)` after few seconds | Tool timeout too short | Set `timeout_ms` to match `--timeout` |
49+
| `permission denied` on session files | Need elevated permissions | Request escalated permissions first |

skills/sub-agents/scripts/run_subagent.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
run_subagent.py - Execute external CLI AIs as sub-agents
44
55
Usage:
6-
python scripts/run_subagent.py --agent <name> --prompt "<task>" --cwd <path>
7-
python scripts/run_subagent.py --list
6+
scripts/run_subagent.py --agent <name> --prompt "<task>" --cwd <path>
7+
scripts/run_subagent.py --list
88
99
Supported CLIs: claude, cursor-agent, codex, gemini
1010
@@ -302,7 +302,7 @@ def build_command(cli: str, prompt: str) -> tuple[str, list]:
302302

303303

304304
def execute_agent(
305-
cli: str, system_context: str, prompt: str, cwd: str, timeout: int = 300000
305+
cli: str, system_context: str, prompt: str, cwd: str, timeout: int = 600000
306306
) -> dict:
307307
"""
308308
Execute agent CLI and return result.
@@ -375,7 +375,10 @@ def execute_agent(
375375
"cli": cli,
376376
}
377377
if status == "error":
378-
response["error"] = f"CLI exited with code {exit_code}, no result returned"
378+
error_msg = f"CLI exited with code {exit_code}"
379+
if stderr and stderr.strip():
380+
error_msg += f": {stderr.strip()}"
381+
response["error"] = error_msg
379382
return response
380383

381384
except Exception as e:
@@ -414,7 +417,7 @@ def main():
414417
parser.add_argument("--cwd", help="Working directory (absolute path)")
415418
parser.add_argument("--agents-dir", help="Directory containing agent definitions")
416419
parser.add_argument(
417-
"--timeout", type=int, default=300000, help="Timeout in ms (default: 300000)"
420+
"--timeout", type=int, default=600000, help="Timeout in ms (default: 600000)"
418421
)
419422
parser.add_argument("--cli", help="Force specific CLI (claude, cursor-agent, codex, gemini)")
420423

0 commit comments

Comments
 (0)