doctor.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #!/usr/bin/env bash
  2. # ============================================================
  3. # Claude Code — Config doctor
  4. # Diagnoses symlinks, prerequisites, plugins, permissions,
  5. # and token budget. Run after install or when something breaks.
  6. # ============================================================
  7. set -euo pipefail
  8. RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
  9. ERRORS=0; WARNS=0
  10. pass() { echo -e " ${GREEN}✓${NC} $1"; }
  11. fail() { echo -e " ${RED}✗${NC} $1"; ERRORS=$((ERRORS + 1)); }
  12. warn() { echo -e " ${YELLOW}⚠${NC} $1"; WARNS=$((WARNS + 1)); }
  13. info() { echo -e " ${BLUE}→${NC} $1"; }
  14. REPO="$(cd "$(dirname "$0")" && pwd)"
  15. VERSION=$(cat "$REPO/version.txt" 2>/dev/null || echo "unknown")
  16. # Load shared detection library
  17. # shellcheck source=lib/detect-plugins.sh
  18. source "$REPO/lib/detect-plugins.sh"
  19. echo ""
  20. echo "═══ claude-config doctor (v${VERSION}) ═══"
  21. echo ""
  22. # ────────────────────────────────────────────────────────────
  23. # 1. Core symlinks
  24. # ────────────────────────────────────────────────────────────
  25. echo "── Symlinks ──"
  26. # Expected: CLAUDE.md, settings.json, agents, skills, templates, hooks/session-start.sh
  27. _EXPECTED_LINKS=7
  28. _LINK_PASS=0
  29. check_symlink() {
  30. local name="$1"
  31. local target="$HOME/.claude/$name"
  32. if [ ! -e "$target" ] && [ ! -L "$target" ]; then
  33. fail "~/.claude/$name — MISSING"
  34. return
  35. fi
  36. if [ -L "$target" ]; then
  37. # readlink -f is not available on macOS BSD — use -f with fallback
  38. local real
  39. real=$(readlink -f "$target" 2>/dev/null) || real=$(readlink "$target")
  40. if [ ! -e "$real" ]; then
  41. fail "~/.claude/$name → $real — BROKEN SYMLINK"
  42. else
  43. pass "~/.claude/$name"; _LINK_PASS=$((_LINK_PASS + 1))
  44. fi
  45. else
  46. warn "~/.claude/$name exists but is NOT a symlink (expected symlink to repo)"
  47. fi
  48. }
  49. check_symlink "CLAUDE.md"
  50. check_symlink "settings.json"
  51. check_symlink "agents"
  52. check_symlink "skills"
  53. check_symlink "templates"
  54. check_symlink "lib"
  55. check_symlink "hooks/session-start.sh"
  56. info "Symlinks: ${_LINK_PASS}/${_EXPECTED_LINKS} OK"
  57. unset _EXPECTED_LINKS _LINK_PASS
  58. echo ""
  59. # ────────────────────────────────────────────────────────────
  60. # 2. GStack submodule
  61. # ────────────────────────────────────────────────────────────
  62. echo "── GStack submodule ──"
  63. GSTACK_DIR="$REPO/skills-external/gstack"
  64. if [ -f "$GSTACK_DIR/.git" ] || [ -d "$GSTACK_DIR/.git" ]; then
  65. pass "Submodule initialized at skills-external/gstack"
  66. warn "GStack tracks branch = main (no commit hash pin). Review upstream before updating."
  67. elif [ -d "$GSTACK_DIR" ]; then
  68. warn "skills-external/gstack exists but submodule not initialized — run: git submodule update --init"
  69. else
  70. warn "GStack submodule missing — run: git submodule update --init"
  71. fi
  72. if [ -L "$HOME/.claude/skills/gstack" ]; then
  73. real=$(readlink -f "$HOME/.claude/skills/gstack" 2>/dev/null || readlink "$HOME/.claude/skills/gstack")
  74. if [ -d "$real" ]; then
  75. pass "Symlink OK → $real"
  76. # Check for skills/ subdirectory (referenced by plugin-advisor PHASE 1)
  77. gstack_skills_count=$(ls "$HOME/.claude/skills/gstack/skills/" 2>/dev/null | wc -l | tr -d ' ')
  78. if [ "${gstack_skills_count:-0}" -gt 0 ]; then
  79. pass "GStack: ${gstack_skills_count} skills available"
  80. else
  81. warn "GStack symlink OK but no skills/ subdirectory found — may need: cd skills-external/gstack && ./setup"
  82. fi
  83. else
  84. fail "Symlink broken → $real"
  85. fi
  86. else
  87. warn "GStack not symlinked — run: bash link.sh"
  88. fi
  89. echo ""
  90. # ────────────────────────────────────────────────────────────
  91. # 3. Prerequisites
  92. # ────────────────────────────────────────────────────────────
  93. echo "── Prerequisites ──"
  94. if command -v git &>/dev/null; then
  95. pass "git $(git --version | awk '{print $3}')"
  96. else
  97. fail "git not found"
  98. fi
  99. if command -v node &>/dev/null; then
  100. NODE_VER=$(node --version | sed 's/v//' | cut -d. -f1)
  101. if [ "$NODE_VER" -ge 18 ]; then
  102. pass "Node.js $(node --version)"
  103. else
  104. warn "Node.js $(node --version) — need >=18"
  105. fi
  106. else
  107. fail "Node.js not found"
  108. fi
  109. if command -v cargo &>/dev/null; then
  110. pass "Cargo $(cargo --version | awk '{print $2}')"
  111. else
  112. warn "Cargo not found (RTK unavailable)"
  113. fi
  114. if command -v python3 &>/dev/null; then
  115. pass "Python $(python3 --version | awk '{print $2}')"
  116. else
  117. warn "Python3 not found"
  118. fi
  119. if command -v claude &>/dev/null; then
  120. pass "Claude Code $(claude --version 2>/dev/null | head -1 || echo 'installed')"
  121. else
  122. fail "Claude Code not found — install from https://code.claude.com"
  123. fi
  124. echo ""
  125. # ────────────────────────────────────────────────────────────
  126. # 4. Key plugins
  127. # ────────────────────────────────────────────────────────────
  128. echo "── Plugins ──"
  129. if detect_rtk; then
  130. pass "RTK installed"
  131. else
  132. warn "RTK not installed — run install-plugins.sh"
  133. fi
  134. if detect_superpowers; then
  135. pass "Superpowers plugin detected"
  136. else
  137. fail "Superpowers not detected — orchestrators (/init-project, /ship-feature) will fail"
  138. fi
  139. if detect_context7; then
  140. pass "Context7 CLI (ctx7) installed"
  141. else
  142. info "Context7 CLI not installed (optional — needed for fast-evolving libs: npm install -g ctx7)"
  143. fi
  144. if detect_gsd; then
  145. pass "GSD v2 installed ($(gsd --version 2>/dev/null | head -1 || echo 'gsd'))"
  146. else
  147. info "GSD v2 not installed (optional — run: npm install -g gsd-pi)"
  148. fi
  149. if detect_ruflo; then
  150. pass "Ruflo CLI installed ($(ruflo --version 2>/dev/null | head -1 || echo 'installed'))"
  151. else
  152. info "Ruflo CLI not installed (optional — enterprise multi-agent: npm install -g ruflo@latest --omit=optional)"
  153. fi
  154. echo ""
  155. # ────────────────────────────────────────────────────────────
  156. # 5. Permissions check
  157. # ────────────────────────────────────────────────────────────
  158. echo "── Permissions ──"
  159. SETTINGS="$HOME/.claude/settings.json"
  160. if [ -f "$SETTINGS" ] || [ -L "$SETTINGS" ]; then
  161. if grep -q '"disableBypassPermissionsMode"' "$SETTINGS" 2>/dev/null; then
  162. pass "Bypass mode disabled"
  163. else
  164. warn "disableBypassPermissionsMode not found in settings"
  165. fi
  166. DENY_COUNT=$(python3 -c "
  167. import json
  168. with open('$SETTINGS') as f:
  169. d = json.load(f)
  170. print(len(d.get('permissions',{}).get('deny',[])))
  171. " 2>/dev/null || echo "?")
  172. if [ "$DENY_COUNT" = "?" ]; then
  173. warn "Could not parse deny count (python3 unavailable or JSON parse error)"
  174. else
  175. EXPECTED_DENY=100
  176. if [ "$DENY_COUNT" -eq "$EXPECTED_DENY" ] 2>/dev/null; then
  177. pass "Deny rules: $DENY_COUNT"
  178. else
  179. warn "Deny rules: $DENY_COUNT (expected $EXPECTED_DENY) — settings may have been manually modified"
  180. fi
  181. fi
  182. else
  183. fail "~/.claude/settings.json not found"
  184. fi
  185. echo ""
  186. # ────────────────────────────────────────────────────────────
  187. # 6. Token budget estimate
  188. # ────────────────────────────────────────────────────────────
  189. echo "── Token budget estimate ──"
  190. # Reference: Claude Code Pro plan ~11k tokens/5h session (session budget, not context window).
  191. # Seuils: WARNING >15%, CRITICAL >30% of session budget.
  192. CLAUDE_MD_CHARS=$(wc -c < "$REPO/CLAUDE.md" 2>/dev/null || echo 0)
  193. CLAUDE_MD_TOKENS=$((CLAUDE_MD_CHARS / 4))
  194. # Skill descriptions only (frontmatter description field — loaded passively at startup)
  195. SKILL_DESC_CHARS=0
  196. for f in "$HOME/.claude/skills/"*/SKILL.md; do
  197. [ -f "$f" ] || continue
  198. desc=$(grep "^description:" "$f" 2>/dev/null | head -1 | sed 's/^description: *//' )
  199. SKILL_DESC_CHARS=$((SKILL_DESC_CHARS + ${#desc}))
  200. done
  201. SKILL_DESC_TOKENS=$((SKILL_DESC_CHARS / 4))
  202. SKILL_COUNT=$(find "$HOME/.claude/skills/" -maxdepth 2 -name "SKILL.md" 2>/dev/null | wc -l | tr -d ' ')
  203. # Plugin passive cost estimates (tokens)
  204. PLUGIN_TOKENS=0
  205. if detect_superpowers 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 800)); fi
  206. if detect_gstack 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 2750)); fi
  207. if detect_frontend_design 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 200)); fi
  208. if detect_uiux_pro_max 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 400)); fi
  209. if detect_context7 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 200)); fi
  210. if detect_ruflo 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 1000)); fi
  211. TOTAL_TOKENS=$((CLAUDE_MD_TOKENS + SKILL_DESC_TOKENS + PLUGIN_TOKENS))
  212. SESSION_BUDGET=11000
  213. PCT=$((TOTAL_TOKENS * 100 / SESSION_BUDGET))
  214. echo ""
  215. echo " CLAUDE.md: ~${CLAUDE_MD_TOKENS}t"
  216. echo " Skill descriptions: ~${SKILL_DESC_TOKENS}t (${SKILL_COUNT} skills)"
  217. echo " Plugin passive cost: ~${PLUGIN_TOKENS}t (active plugins)"
  218. echo " ─────────────────────────────────────────"
  219. info " Total: ~${TOTAL_TOKENS}t"
  220. info " Session budget (Pro): ${SESSION_BUDGET}t"
  221. info " Usage: ~${PCT}%"
  222. echo ""
  223. if [ "$PCT" -gt 30 ]; then
  224. warn "CRITICAL: ${PCT}% of session budget — /plugin-check to disable unused plugins"
  225. elif [ "$PCT" -gt 15 ]; then
  226. warn "WARNING: ${PCT}% of session budget — consider disabling unused toggle plugins"
  227. else
  228. pass "Budget: ${PCT}% (comfortable)"
  229. fi
  230. # Per-file breakdown (skill bodies — loaded on demand, shown for awareness)
  231. if [ "$TOTAL_TOKENS" -gt 2000 ]; then
  232. info "Skill/agent bodies (loaded on demand, >200t each):"
  233. for f in "$HOME/.claude/skills/"*/SKILL.md "$HOME/.claude/agents/"*.md; do
  234. [ -f "$f" ] || continue
  235. size=$(wc -c < "$f" 2>/dev/null || echo 0)
  236. tokens=$((size / 4))
  237. if [ "$tokens" -gt 200 ]; then
  238. label=$(basename "$(dirname "$f")" 2>/dev/null)
  239. [ "$label" = "." ] && label=$(basename "$f")
  240. info " ~${tokens}t ${label}"
  241. fi
  242. done
  243. fi
  244. echo ""
  245. # ────────────────────────────────────────────────────────────
  246. # 7. File consistency
  247. # ────────────────────────────────────────────────────────────
  248. echo "── Consistency ──"
  249. # Check all skills have disable-model-invocation
  250. MISSING_DMI=()
  251. for f in "$HOME/.claude/skills/"*/SKILL.md; do
  252. [ -f "$f" ] || continue
  253. name=$(basename "$(dirname "$f")")
  254. if ! grep -q "disable-model-invocation" "$f" 2>/dev/null; then
  255. MISSING_DMI+=("$name")
  256. fi
  257. done
  258. if [ ${#MISSING_DMI[@]} -eq 0 ]; then
  259. pass "All skills have disable-model-invocation"
  260. else
  261. warn "Skills missing disable-model-invocation: ${MISSING_DMI[*]}"
  262. fi
  263. # Check expected skills are present
  264. EXPECTED_SKILLS=(
  265. "analyze" "health" "init-project" "onboard" "plugin-check"
  266. "readme" "refactor" "ship-feature" "status"
  267. )
  268. MISSING_SKILLS=()
  269. for skill in "${EXPECTED_SKILLS[@]}"; do
  270. if [ ! -f "$HOME/.claude/skills/${skill}/SKILL.md" ]; then
  271. MISSING_SKILLS+=("${skill}/")
  272. fi
  273. done
  274. if [ ${#MISSING_SKILLS[@]} -eq 0 ]; then
  275. pass "All ${#EXPECTED_SKILLS[@]} expected skills present (analyze, health, init-project, onboard, plugin-check, readme, refactor, ship-feature, status)"
  276. else
  277. warn "Missing skills: ${MISSING_SKILLS[*]} — run: bash link.sh"
  278. fi
  279. # Check expected agents are present
  280. EXPECTED_AGENTS=(
  281. "analyzer" "interviewer" "plugin-advisor" "readme-updater"
  282. "refactorer" "scaffolder" "onboarder" "status-reporter"
  283. )
  284. MISSING_AGENTS=()
  285. for agent in "${EXPECTED_AGENTS[@]}"; do
  286. if [ ! -f "$HOME/.claude/agents/${agent}.md" ]; then
  287. MISSING_AGENTS+=("${agent}.md")
  288. fi
  289. done
  290. if [ ${#MISSING_AGENTS[@]} -eq 0 ]; then
  291. pass "All 8 agents present (analyzer, interviewer, plugin-advisor, readme-updater, refactorer, scaffolder, onboarder, status-reporter)"
  292. else
  293. warn "Missing agents: ${MISSING_AGENTS[*]} — run: bash link.sh"
  294. fi
  295. # Check CRLF — portable: grep -P not available on macOS BSD grep
  296. CRLF_FILES=()
  297. for f in "$REPO"/*.md "$REPO"/agents/*.md "$REPO"/skills/*/SKILL.md; do
  298. [ -f "$f" ] || continue
  299. if grep -c $'\r' "$f" 2>/dev/null | grep -q "^[^0]"; then
  300. CRLF_FILES+=("$(basename "$f")")
  301. fi
  302. done
  303. if [ ${#CRLF_FILES[@]} -eq 0 ]; then
  304. pass "No CRLF line endings detected"
  305. else
  306. warn "CRLF detected in: ${CRLF_FILES[*]}"
  307. fi
  308. echo ""
  309. # ────────────────────────────────────────────────────────────
  310. # Summary
  311. # ────────────────────────────────────────────────────────────
  312. echo "═══════════════════════════════════════════"
  313. if [ "$ERRORS" -gt 0 ]; then
  314. echo -e "${RED} $ERRORS error(s)${NC}, ${YELLOW}$WARNS warning(s)${NC}"
  315. echo ""
  316. echo " Fix: cd $REPO && bash link.sh && bash install-plugins.sh"
  317. exit 1
  318. elif [ "$WARNS" -gt 0 ]; then
  319. echo -e " ${GREEN}No errors${NC}, ${YELLOW}$WARNS warning(s)${NC}"
  320. else
  321. echo -e " ${GREEN}All checks passed ✓${NC}"
  322. fi
  323. echo ""