-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathsubagent-stop.sh
More file actions
executable file
·51 lines (43 loc) · 1.5 KB
/
subagent-stop.sh
File metadata and controls
executable file
·51 lines (43 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
# subagent-stop.sh
# Runs when a sub-agent completes
# Use for cleanup, logging, or metrics
INPUT=$(cat)
AGENT_NAME=$(echo "$INPUT" | jq -r '.agent_name // "unknown"')
SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // "unknown"')
EXIT_CODE=$(echo "$INPUT" | jq -r '.exit_code // 0')
DURATION=$(echo "$INPUT" | jq -r '.duration_ms // 0')
# Log subagent completion
LOG_DIR="$HOME/.claude/logs"
mkdir -p "$LOG_DIR"
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
LOG_ENTRY=$(jq -n \
--arg timestamp "$TIMESTAMP" \
--arg agent "$AGENT_NAME" \
--arg session "$SESSION_ID" \
--argjson exit_code "$EXIT_CODE" \
--argjson duration "$DURATION" \
'{timestamp: $timestamp, agent: $agent, session: $session, exit_code: $exit_code, duration_ms: $duration}')
echo "$LOG_ENTRY" >> "$LOG_DIR/subagents-$(date +%Y-%m-%d).jsonl"
# Calculate duration in seconds for readability
DURATION_SEC=$(echo "scale=2; $DURATION / 1000" | bc)
# Provide feedback on slow agents (>30 seconds)
if [[ $(echo "$DURATION > 30000" | bc) -eq 1 ]]; then
cat << EOF
{
"systemMessage": "⏱️ Subagent '$AGENT_NAME' took ${DURATION_SEC}s to complete. Consider optimizing or splitting into smaller tasks.",
"hookSpecificOutput": {
"additionalContext": "Subagent performance: $AGENT_NAME completed in ${DURATION_SEC}s"
}
}
EOF
fi
# Alert on failed subagents
if [[ $EXIT_CODE -ne 0 ]]; then
cat << EOF
{
"systemMessage": "❌ Subagent '$AGENT_NAME' failed with exit code $EXIT_CODE. Review task output for errors."
}
EOF
fi
exit 0