-
Notifications
You must be signed in to change notification settings - Fork 4
Add session multiplexing detection #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #!/bin/bash | ||
| # SessionStart hook: Detect multiple active Claude Code sessions | ||
| # Touches a session file and counts active sessions (modified in last 2 hours) | ||
| # When 3+ active, outputs a reminder to re-ground context in responses | ||
|
|
||
| # Read JSON input from stdin | ||
| INPUT=$(cat) | ||
|
|
||
| # Extract session_id | ||
| SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // empty') | ||
|
|
||
| if [ -z "$SESSION_ID" ]; then | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Session tracking directory | ||
| SESSION_DIR="$HOME/.claude/sessions" | ||
| mkdir -p "$SESSION_DIR" | ||
|
|
||
| # Sanitize session_id to prevent path traversal — use only the basename | ||
| SESSION_FILE="$SESSION_DIR/$(basename "$SESSION_ID")" | ||
| touch "$SESSION_FILE" | ||
|
|
||
| # Count sessions modified in last 2 hours (find -mmin works on both macOS and Linux) | ||
| ACTIVE_COUNT=$(find "$SESSION_DIR" -type f -mmin -120 2>/dev/null | wc -l | tr -d ' ') | ||
|
|
||
| # Clean up sessions older than 24 hours | ||
| find "$SESSION_DIR" -type f -mmin +1440 -delete 2>/dev/null | ||
|
|
||
| # Threshold: 3+ sessions is where context-juggling becomes error-prone. | ||
| # Below 3, a user can reasonably track what each window is doing. | ||
| if [ "$ACTIVE_COUNT" -ge 3 ]; then | ||
| echo "Multi-session mode: $ACTIVE_COUNT active Claude Code sessions detected. The user is juggling multiple windows — always include explicit context (repo name, branch, what we're working on) when reporting status or completing work." | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hook outputs plain text instead of required JSON formatHigh Severity The
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WONTFIX — incorrect. For |
||
| fi | ||
|
|
||
| exit 0 | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This tracking logic only runs on
SessionStartand never clears a session marker when a session ends, so closed windows are still counted as "active" for up to 2 hours (or 24 hours until cleanup). In practice, if a user opens 3 sessions, closes 2, and then starts another within 2 hours, the hook still reports 3+ active sessions and injects unnecessary guidance. Adding aSessionEndhook to delete the corresponding marker would make the count reflect truly active sessions.Useful? React with 👍 / 👎.