-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcmux-session-namer.sh
More file actions
executable file
·68 lines (59 loc) · 2.89 KB
/
cmux-session-namer.sh
File metadata and controls
executable file
·68 lines (59 loc) · 2.89 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
# cmux-session-namer.sh — Initialize tab/workspace naming on new Claude Code session
#
# Invoked by the cmux claude wrapper as a SessionStart hook (both new and resume).
#
# New session: clear cache for fresh AI summary, set project basename
# Resume session: scan entire transcript for custom-title and sync it immediately,
# write marker file so AI summary is suppressed (fixes the bug where
# /rename early in a long transcript was invisible to tail-500 checks)
#
# Environment: CMUX_WORKSPACE_ID, CMUX_SURFACE_ID (set by cmux shell integration)
# Stdin: JSON with session_id, cwd, transcript_path, etc.
set -e
INPUT=$(cat)
[ "$CMUX_TAB_NAMER_DISABLED" = "1" ] && exit 0
[ -z "$CMUX_WORKSPACE_ID" ] && exit 0
[ -z "$CMUX_SURFACE_ID" ] && exit 0
command -v cmux &>/dev/null || exit 0
# Register as workspace owner if no owner exists yet (first session wins).
# The workspace owner's AI summary / custom-title controls the workspace name.
WS_OWNER_FILE="/tmp/cmux-ws-owner-${CMUX_WORKSPACE_ID}"
if [ ! -f "$WS_OWNER_FILE" ]; then
echo "$CMUX_SURFACE_ID" > "$WS_OWNER_FILE"
fi
# Check entire transcript for an existing custom-title (handles resumed sessions
# where /rename was issued earlier in a long transcript, beyond tail-500 reach)
CUSTOM_MARKER="/tmp/cmux-custom-title-${CMUX_SURFACE_ID}"
TRANSCRIPT=$(echo "$INPUT" | python3 -c "import sys,json; print(json.load(sys.stdin).get('transcript_path',''))" 2>/dev/null || true)
if [ -n "$TRANSCRIPT" ] && [ -f "$TRANSCRIPT" ]; then
TITLE=$(grep '"type":"custom-title"' "$TRANSCRIPT" 2>/dev/null | tail -1 | python3 -c "
import sys,json
try:
print(json.loads(sys.stdin.readline()).get('customTitle',''))
except: pass
" 2>/dev/null)
if [ -n "$TITLE" ]; then
# Resumed session with custom-title: sync immediately and suppress AI summary
echo "$TITLE" > "$CUSTOM_MARKER"
cmux rename-tab --workspace "$CMUX_WORKSPACE_ID" --surface "$CMUX_SURFACE_ID" "$TITLE" 2>/dev/null || true
WS_OWNER=$(cat "$WS_OWNER_FILE" 2>/dev/null || true)
if [ "$WS_OWNER" = "$CMUX_SURFACE_ID" ]; then
cmux rename-workspace --workspace "$CMUX_WORKSPACE_ID" "$TITLE" 2>/dev/null || true
fi
exit 0
fi
fi
# No custom-title: clear marker and cache so the first Stop triggers a fresh AI summary
rm -f "$CUSTOM_MARKER" 2>/dev/null
rm -f "/tmp/cmux-tab-cache-${CMUX_SURFACE_ID}" 2>/dev/null
# Set project basename as initial workspace name — only if this tab owns the workspace.
# Non-owner sessions must not overwrite an already-established workspace name.
WS_OWNER=$(cat "$WS_OWNER_FILE" 2>/dev/null || true)
if [ "$WS_OWNER" = "$CMUX_SURFACE_ID" ]; then
CWD=$(echo "$INPUT" | python3 -c "import sys,json; print(json.load(sys.stdin).get('cwd',''))" 2>/dev/null || true)
if [ -n "$CWD" ]; then
BASENAME=$(basename "$CWD")
cmux rename-workspace --workspace "$CMUX_WORKSPACE_ID" "$BASENAME" 2>/dev/null || true
fi
fi