session-start.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/env bash
  2. # ============================================================
  3. # Claude Code — Session start plugin status
  4. # Runs once per session. Zero API calls. Filesystem only.
  5. # ============================================================
  6. # ── Quick health check (filesystem only, no subprocesses) ──
  7. BROKEN=()
  8. for f in CLAUDE.md settings.json agents skills; do
  9. [ ! -e "$HOME/.claude/$f" ] && BROKEN+=("$f")
  10. done
  11. if [ ${#BROKEN[@]} -gt 0 ]; then
  12. # Try to find the repo path from an existing symlink
  13. _repo_hint=""
  14. for _probe in CLAUDE.md settings.json; do
  15. if [ -L "$HOME/.claude/$_probe" ]; then
  16. _repo_hint="$(cd "$(dirname "$(readlink "$HOME/.claude/$_probe")")" 2>/dev/null && pwd)"
  17. break
  18. fi
  19. done
  20. _fix_cmd="${_repo_hint:+cd $_repo_hint && }bash link.sh"
  21. echo ""
  22. echo "┌─ ⚠️ CONFIG ISSUES ────────────────────────────────┐"
  23. for b in "${BROKEN[@]}"; do
  24. printf "│ MISSING: ~/.claude/%-30s│\n" "$b"
  25. done
  26. printf "│ → %-47s│\n" "$_fix_cmd"
  27. echo "│ → /health for full diagnostic │"
  28. echo "└───────────────────────────────────────────────────┘"
  29. unset _repo_hint _fix_cmd
  30. fi
  31. # ── Load shared detection library ──
  32. _lib="$(dirname "${BASH_SOURCE[0]}")/../lib/detect-plugins.sh"
  33. if [ -f "$_lib" ]; then
  34. # shellcheck source=../lib/detect-plugins.sh
  35. source "$_lib"
  36. else
  37. echo "⚠️ lib/detect-plugins.sh not found — config broken, run: bash link.sh"
  38. exit 0
  39. fi
  40. unset _lib
  41. # ── Toggle plugin detection ──
  42. TOGGLE_ACTIVE=()
  43. TOGGLE_INACTIVE=()
  44. for plugin in gstack uiux_pro_max frontend_design plugin_dev context7 ruflo graphifyy; do
  45. # Map function name to display name
  46. case "$plugin" in
  47. uiux_pro_max) display="ui-ux-pro-max" ;;
  48. frontend_design) display="frontend-design" ;;
  49. plugin_dev) display="plugin-dev" ;;
  50. *) display="$plugin" ;;
  51. esac
  52. if "detect_$plugin" 2>/dev/null; then
  53. TOGGLE_ACTIVE+=("$display")
  54. else
  55. TOGGLE_INACTIVE+=("$display")
  56. fi
  57. done
  58. # --- Format output ---
  59. ACTIVE_STR="${TOGGLE_ACTIVE[*]:-none}"
  60. INACTIVE_STR="${TOGGLE_INACTIVE[*]:-none}"
  61. # GSD v2 — standalone CLI (not a Claude Code plugin — shown separately)
  62. if detect_gsd 2>/dev/null; then
  63. GSD_STATUS="gsd v2 ✓"
  64. else
  65. GSD_STATUS="gsd v2 ✗ (npm install -g gsd-pi)"
  66. fi
  67. # Version detection: follow CLAUDE.md symlink back to repo, then read version.txt
  68. _claude_real="$(readlink "$HOME/.claude/CLAUDE.md" 2>/dev/null || true)"
  69. if [ -n "$_claude_real" ]; then
  70. _repo_dir="$(cd "$(dirname "$_claude_real")" 2>/dev/null && pwd)"
  71. CONFIG_VERSION=$(cat "$_repo_dir/version.txt" 2>/dev/null || echo "?")
  72. else
  73. CONFIG_VERSION="?"
  74. fi
  75. REPO_DIR="${_repo_dir:-}"
  76. unset _claude_real _repo_dir
  77. # Detect plan and set passive token budget
  78. PLAN=$(detect_plan 2>/dev/null || echo "pro")
  79. case "$PLAN" in
  80. max) _budget=20000; PLAN_LABEL="Max" ;;
  81. pro) _budget=11000; PLAN_LABEL="Pro" ;;
  82. free) _budget=5000; PLAN_LABEL="Free" ;;
  83. *) _budget=11000; PLAN_LABEL="Pro" ;;
  84. esac
  85. # Quick passive token cost estimate
  86. # Only count plugins that are ACTIVE (detected as ON), not just installed
  87. _passive_t=0
  88. detect_superpowers 2>/dev/null && _passive_t=$((_passive_t + 800))
  89. # Token costs for toggle plugins — map display name to cost
  90. declare -A _plugin_costs=(
  91. [gstack]=2750
  92. [ui-ux-pro-max]=400
  93. [frontend-design]=200
  94. [plugin-dev]=100
  95. [context7]=200
  96. [ruflo]=1000
  97. [graphifyy]=300
  98. )
  99. for _p in "${TOGGLE_ACTIVE[@]}"; do
  100. _cost="${_plugin_costs[$_p]:-0}"
  101. _passive_t=$((_passive_t + _cost))
  102. done
  103. _budget_pct=$((_passive_t * 100 / _budget))
  104. if [ "$_budget_pct" -gt 50 ]; then
  105. TOKEN_WARN="⚠️ ~${_passive_t}t passif (${_budget_pct}% budget $PLAN_LABEL)"
  106. elif [ "$_budget_pct" -gt 25 ]; then
  107. TOKEN_WARN="~${_passive_t}t passif (${_budget_pct}% budget $PLAN_LABEL)"
  108. else
  109. TOKEN_WARN=""
  110. fi
  111. unset _passive_t _budget_pct _budget
  112. echo ""
  113. echo "┌─ Claude Code config ──────────────────────────────────┐"
  114. printf "│ ✅ ON : %-40s│\n" "security-guidance rtk superpowers"
  115. # Plugin display — all plugins shown, split across 2 lines if >4
  116. _active_count=${#TOGGLE_ACTIVE[@]}
  117. _inactive_count=${#TOGGLE_INACTIVE[@]}
  118. if [ "$_active_count" -eq 0 ]; then
  119. printf "│ 🟢 ON : %-40s│\n" "none"
  120. elif [ "$_active_count" -le 4 ]; then
  121. printf "│ 🟢 ON : %-40s│\n" "$ACTIVE_STR"
  122. else
  123. # Split: first 4 on line 1, rest on continuation line
  124. _line1="${TOGGLE_ACTIVE[0]} ${TOGGLE_ACTIVE[1]} ${TOGGLE_ACTIVE[2]} ${TOGGLE_ACTIVE[3]}"
  125. _rest=("${TOGGLE_ACTIVE[@]:4}")
  126. _line2="${_rest[*]}"
  127. printf "│ 🟢 ON : %-40s│\n" "$_line1"
  128. printf "│ %-40s│\n" "$_line2"
  129. unset _line1 _line2 _rest
  130. fi
  131. if [ "$_inactive_count" -eq 0 ]; then
  132. printf "│ ⚫ OFF : %-40s│\n" "none"
  133. elif [ "$_inactive_count" -le 4 ]; then
  134. printf "│ ⚫ OFF : %-40s│\n" "$INACTIVE_STR"
  135. else
  136. _line1="${TOGGLE_INACTIVE[0]} ${TOGGLE_INACTIVE[1]} ${TOGGLE_INACTIVE[2]} ${TOGGLE_INACTIVE[3]}"
  137. _rest=("${TOGGLE_INACTIVE[@]:4}")
  138. _line2="${_rest[*]}"
  139. printf "│ ⚫ OFF : %-40s│\n" "$_line1"
  140. printf "│ %-40s│\n" "$_line2"
  141. unset _line1 _line2 _rest
  142. fi
  143. unset _active_count _inactive_count
  144. printf "│ 🖥️ CLI : %-40s│\n" "$GSD_STATUS"
  145. [ -n "$TOKEN_WARN" ] && printf "│ 💰 %-44s│\n" "${TOKEN_WARN:0:44}"
  146. printf "│ 📦 v%-45s│\n" "$CONFIG_VERSION"
  147. # Version check: compare local vs remote (non-blocking)
  148. _remote_ver=""
  149. if [ -n "$REPO_DIR" ] && [ -d "$REPO_DIR/.git" ]; then
  150. _remote_ver=$(cd "$REPO_DIR" 2>/dev/null && git fetch origin --quiet 2>/dev/null && git show origin/master:version.txt 2>/dev/null || true)
  151. fi
  152. if [ -n "$_remote_ver" ] && [ "$_remote_ver" != "$CONFIG_VERSION" ]; then
  153. printf "│ 🔄 update available: v%-27s│\n" "$_remote_ver"
  154. fi
  155. unset _remote_ver REPO_DIR
  156. echo "│ 💡 /plugin-check before starting a new project │"
  157. echo "│ 🩺 /health to run full diagnostic │"
  158. echo "└───────────────────────────────────────────────────┘"
  159. echo ""
  160. unset TOKEN_WARN