Loading...
Claude Code multi-session overlap detector showing concurrent active sessions with visual indicators, session count, and workspace collision warnings for budget management.
#!/usr/bin/env bash
# Multi-Session Overlap Indicator for Claude Code
# Detects concurrent Claude sessions running in parallel
# Read JSON from stdin
read -r input
# Extract current session info
session_id=$(echo "$input" | jq -r '.session_id // "unknown"')
current_workspace=$(echo "$input" | jq -r '.workspace.current_dir // ""')
# Session tracking directory (stores active session metadata)
SESSION_DIR="${HOME}/.claude-code-sessions"
mkdir -p "$SESSION_DIR"
# Current session file
SESSION_FILE="${SESSION_DIR}/${session_id}.active"
# Write current session timestamp and workspace
echo "$(date +%s)|${current_workspace}" > "$SESSION_FILE"
# Cleanup stale sessions (older than 10 minutes = 600 seconds)
CURRENT_TIME=$(date +%s)
for session_file in "$SESSION_DIR"/*.active; do
if [ -f "$session_file" ]; then
session_timestamp=$(cut -d'|' -f1 < "$session_file")
age=$((CURRENT_TIME - session_timestamp))
if [ $age -gt 600 ]; then
rm -f "$session_file"
fi
fi
done
# Count active sessions
active_sessions=$(find "$SESSION_DIR" -name '*.active' -type f | wc -l | tr -d ' ')
# Check for workspace collisions (multiple sessions in same workspace)
workspace_collision=false
if [ -n "$current_workspace" ]; then
collision_count=$(grep -l "|${current_workspace}$" "$SESSION_DIR"/*.active 2>/dev/null | wc -l | tr -d ' ')
if [ "$collision_count" -gt 1 ]; then
workspace_collision=true
fi
fi
# Color coding based on session count
if [ $active_sessions -eq 1 ]; then
SESSION_COLOR="\033[38;5;46m" # Green: Single session
SESSION_ICON="●"
SESSION_STATUS="SOLO"
elif [ $active_sessions -le 3 ]; then
SESSION_COLOR="\033[38;5;226m" # Yellow: 2-3 sessions (moderate overlap)
SESSION_ICON="●●"
SESSION_STATUS="MULTI"
else
SESSION_COLOR="\033[38;5;196m" # Red: 4+ sessions (high overlap, budget concern)
SESSION_ICON="●●●"
SESSION_STATUS="OVERLAP!"
fi
# Workspace collision warning
if [ "$workspace_collision" = true ]; then
COLLISION_WARNING="\033[38;5;208m⚠ WORKSPACE COLLISION\033[0m"
else
COLLISION_WARNING=""
fi
RESET="\033[0m"
# Build session list visualization
if [ $active_sessions -gt 1 ]; then
session_list=""
for i in $(seq 1 $active_sessions); do
session_list="${session_list}●"
done
visual="[${session_list}]"
else
visual=""
fi
# Output statusline
if [ -n "$COLLISION_WARNING" ]; then
echo -e "${SESSION_COLOR}${SESSION_ICON} ${SESSION_STATUS}${RESET}: ${active_sessions} active ${visual} | ${COLLISION_WARNING}"
else
echo -e "${SESSION_COLOR}${SESSION_ICON} ${SESSION_STATUS}${RESET}: ${active_sessions} active ${visual}"
fi
{
"format": "bash",
"position": "left",
"refreshInterval": 2000
}Session count always showing 1 despite multiple Claude Code instances running
Verify session_id field exists in JSON: echo '$input' | jq .session_id. Check ~/.claude-code-sessions directory is writable: ls -la ~/.claude-code-sessions. Ensure each Claude Code instance has unique session_id. If all sessions share same ID (bug), script cannot distinguish them.
Workspace collision warning appearing incorrectly
Check workspace.current_dir field: echo '$input' | jq .workspace.current_dir. Script uses exact path matching - symlinks and relative paths may cause false positives. Verify sessions are actually in different directories with pwd. Collision is EXPECTED if multiple sessions genuinely share same workspace.
Stale sessions not being cleaned up automatically
Cleanup runs every statusline update (default 2s refresh). Threshold is 600 seconds (10 minutes) of inactivity. Check session files: ls -la ~/.claude-code-sessions/*.active. Verify date command works: date +%s. Manually cleanup: rm ~/.claude-code-sessions/*.active.
Permission denied when creating session tracking directory
Script creates ~/.claude-code-sessions on first run. Ensure HOME environment variable is set: echo $HOME. Check write permissions: mkdir -p ~/.claude-code-sessions. If permission denied, change location in script: SESSION_DIR="/tmp/claude-sessions-$(whoami)".
Visual session list not displaying dots correctly
Ensure terminal supports Unicode bullet character (●). Test with: echo -e '●●●'. If unsupported, replace with ASCII: SESSION_ICON='*' and visual characters. Check terminal encoding is UTF-8: echo $LANG (should show UTF-8).
Loading reviews...