Skip to content

Commit 6511c98

Browse files
YonganZhangclaude
andauthored
Fix unhandled OSError in subprocess and unsafe dict access in subagent (#159)
- agents/s01_agent_loop.py: Add FileNotFoundError/OSError handling in run_bash() - agents/s04_subagent.py: Same fix + use .get() for block.input['prompt'] (consistent with .get() already used for 'description' on line 157) Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 117e59c commit 6511c98

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

agents/s01_agent_loop.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ def run_bash(command: str) -> str:
7373
return out[:50000] if out else "(no output)"
7474
except subprocess.TimeoutExpired:
7575
return "Error: Timeout (120s)"
76+
except (FileNotFoundError, OSError) as e:
77+
return f"Error: {e}"
7678

7779

7880
# -- The core pattern: a while loop that calls tools until the model stops --

agents/s04_subagent.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ def run_bash(command: str) -> str:
6161
return out[:50000] if out else "(no output)"
6262
except subprocess.TimeoutExpired:
6363
return "Error: Timeout (120s)"
64+
except (FileNotFoundError, OSError) as e:
65+
return f"Error: {e}"
6466

6567
def run_read(path: str, limit: int = None) -> str:
6668
try:
@@ -155,8 +157,9 @@ def agent_loop(messages: list):
155157
if block.type == "tool_use":
156158
if block.name == "task":
157159
desc = block.input.get("description", "subtask")
158-
print(f"> task ({desc}): {block.input['prompt'][:80]}")
159-
output = run_subagent(block.input["prompt"])
160+
prompt = block.input.get("prompt", "")
161+
print(f"> task ({desc}): {prompt[:80]}")
162+
output = run_subagent(prompt)
160163
else:
161164
handler = TOOL_HANDLERS.get(block.name)
162165
output = handler(**block.input) if handler else f"Unknown tool: {block.name}"

0 commit comments

Comments
 (0)