From 47845b9d282d3b309a296d85fa5eef1ffe58935a Mon Sep 17 00:00:00 2001 From: Nick Sullivan Date: Wed, 1 Apr 2026 09:16:06 -0500 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Add=20session=20multiplexing=20?= =?UTF-8?q?detection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SessionStart hook that tracks active Claude Code sessions. When 3+ sessions are active (modified in last 2 hours), outputs a reminder to include explicit context in responses — repo name, branch, what we're working on. Designed for users who juggle multiple windows/repos simultaneously. Auto-cleans session marker files older than 24 hours. Inspired by gstack's ELI16 mode. Co-Authored-By: Claude Opus 4.6 --- plugins/core/hooks/hooks.json | 11 +++++++ plugins/core/hooks/session-multiplexing.sh | 38 ++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100755 plugins/core/hooks/session-multiplexing.sh diff --git a/plugins/core/hooks/hooks.json b/plugins/core/hooks/hooks.json index c7f2d7c..d900d8a 100644 --- a/plugins/core/hooks/hooks.json +++ b/plugins/core/hooks/hooks.json @@ -1,5 +1,16 @@ { "hooks": { + "SessionStart": [ + { + "matcher": "startup|resume|clear|compact", + "hooks": [ + { + "type": "command", + "command": "${CLAUDE_PLUGIN_ROOT}/hooks/session-multiplexing.sh" + } + ] + } + ], "PostToolUse": [ { "matcher": "TodoWrite", diff --git a/plugins/core/hooks/session-multiplexing.sh b/plugins/core/hooks/session-multiplexing.sh new file mode 100755 index 0000000..b63cdd0 --- /dev/null +++ b/plugins/core/hooks/session-multiplexing.sh @@ -0,0 +1,38 @@ +#!/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" + +# Touch this session's file +touch "$SESSION_DIR/$SESSION_ID" + +# Count sessions modified in last 2 hours +if [[ "$OSTYPE" == "darwin"* ]]; then + ACTIVE_COUNT=$(find "$SESSION_DIR" -type f -mmin -120 2>/dev/null | wc -l | tr -d ' ') +else + ACTIVE_COUNT=$(find "$SESSION_DIR" -type f -mmin -120 2>/dev/null | wc -l | tr -d ' ') +fi + +# Clean up sessions older than 24 hours +find "$SESSION_DIR" -type f -mmin +1440 -delete 2>/dev/null + +# When 3+ sessions active, add context re-grounding reminder +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." +fi + +exit 0 From 3f4f0ddf13e3ee168489606a94add93b9d5692cd Mon Sep 17 00:00:00 2001 From: Nick Sullivan Date: Wed, 1 Apr 2026 09:37:30 -0500 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=92=20Address=20PR=20#48=20bot=20f?= =?UTF-8?q?eedback=20on=20session-multiplexing=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove dead code OS detection (both branches were identical) find -mmin works the same on macOS and Linux - Add basename guard against path traversal in SESSION_ID - Add comment explaining the 3-session threshold choice Co-Authored-By: Claude --- plugins/core/hooks/session-multiplexing.sh | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/plugins/core/hooks/session-multiplexing.sh b/plugins/core/hooks/session-multiplexing.sh index b63cdd0..f4a9ba1 100755 --- a/plugins/core/hooks/session-multiplexing.sh +++ b/plugins/core/hooks/session-multiplexing.sh @@ -17,20 +17,18 @@ fi SESSION_DIR="$HOME/.claude/sessions" mkdir -p "$SESSION_DIR" -# Touch this session's file -touch "$SESSION_DIR/$SESSION_ID" - -# Count sessions modified in last 2 hours -if [[ "$OSTYPE" == "darwin"* ]]; then - ACTIVE_COUNT=$(find "$SESSION_DIR" -type f -mmin -120 2>/dev/null | wc -l | tr -d ' ') -else - ACTIVE_COUNT=$(find "$SESSION_DIR" -type f -mmin -120 2>/dev/null | wc -l | tr -d ' ') -fi +# 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 -# When 3+ sessions active, add context re-grounding reminder +# 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." fi