doctor.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "$HOME/.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 "$HOME/.claude/$name → $real — BROKEN SYMLINK"
  42. else
  43. pass "$HOME/.claude/$name"; _LINK_PASS=$((_LINK_PASS + 1))
  44. fi
  45. else
  46. warn "$HOME/.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. # `|| echo 0` is required because under `set -o pipefail`, a missing
  78. # gstack/skills/ dir makes find exit non-zero, killing the script.
  79. gstack_skills_count=$( { find "$HOME/.claude/skills/gstack/skills/" -maxdepth 1 -mindepth 1 2>/dev/null || true; } | wc -l | tr -d ' ')
  80. if [ "${gstack_skills_count:-0}" -gt 0 ]; then
  81. pass "GStack: ${gstack_skills_count} skills available"
  82. else
  83. warn "GStack symlink OK but no skills/ subdirectory found — may need: cd skills-external/gstack && ./setup"
  84. fi
  85. else
  86. fail "Symlink broken → $real"
  87. fi
  88. else
  89. warn "GStack not symlinked — run: bash link.sh"
  90. fi
  91. echo ""
  92. # ────────────────────────────────────────────────────────────
  93. # 3. Prerequisites
  94. # ────────────────────────────────────────────────────────────
  95. echo "── Prerequisites ──"
  96. if command -v git &>/dev/null; then
  97. pass "git $(git --version | awk '{print $3}')"
  98. else
  99. fail "git not found"
  100. fi
  101. if command -v node &>/dev/null; then
  102. NODE_VER=$(node --version | sed 's/v//' | cut -d. -f1)
  103. if [ "$NODE_VER" -ge 18 ]; then
  104. pass "Node.js $(node --version)"
  105. else
  106. warn "Node.js $(node --version) — need >=18"
  107. fi
  108. else
  109. fail "Node.js not found"
  110. fi
  111. if command -v cargo &>/dev/null; then
  112. pass "Cargo $(cargo --version | awk '{print $2}')"
  113. else
  114. warn "Cargo not found (RTK unavailable)"
  115. fi
  116. if command -v python3 &>/dev/null; then
  117. pass "Python $(python3 --version | awk '{print $2}')"
  118. else
  119. warn "Python3 not found"
  120. fi
  121. if command -v claude &>/dev/null; then
  122. pass "Claude Code $(claude --version 2>/dev/null | head -1 || echo 'installed')"
  123. else
  124. fail "Claude Code not found — install from https://code.claude.com"
  125. fi
  126. echo ""
  127. # ────────────────────────────────────────────────────────────
  128. # 4. Key plugins
  129. # ────────────────────────────────────────────────────────────
  130. echo "── Plugins ──"
  131. if detect_rtk; then
  132. pass "RTK installed"
  133. else
  134. warn "RTK not installed — run install-plugins.sh"
  135. fi
  136. if detect_superpowers; then
  137. pass "Superpowers plugin detected"
  138. else
  139. fail "Superpowers not detected — orchestrators (/init-project, /ship-feature) will fail"
  140. fi
  141. if detect_context7; then
  142. pass "Context7 CLI (ctx7) installed"
  143. else
  144. info "Context7 CLI not installed (optional — needed for fast-evolving libs: npm install -g ctx7)"
  145. fi
  146. if detect_gsd; then
  147. pass "GSD v2 installed ($(gsd --version 2>/dev/null | head -1 || echo 'gsd'))"
  148. else
  149. info "GSD v2 not installed (optional — run: npm install -g gsd-pi)"
  150. fi
  151. if detect_graphifyy; then
  152. pass "Graphifyy installed (graphify CLI)"
  153. else
  154. info "Graphifyy not installed (optional — codebase knowledge graph: pipx install graphifyy)"
  155. fi
  156. echo ""
  157. # ────────────────────────────────────────────────────────────
  158. # 5. Permissions check
  159. # ────────────────────────────────────────────────────────────
  160. echo "── Permissions ──"
  161. SETTINGS="$HOME/.claude/settings.json"
  162. if [ -f "$SETTINGS" ] || [ -L "$SETTINGS" ]; then
  163. if grep -q '"disableBypassPermissionsMode"' "$SETTINGS" 2>/dev/null; then
  164. pass "Bypass mode disabled"
  165. else
  166. warn "disableBypassPermissionsMode not found in settings"
  167. fi
  168. DENY_COUNT=$(python3 -c "
  169. import json
  170. with open('$SETTINGS') as f:
  171. d = json.load(f)
  172. print(len(d.get('permissions',{}).get('deny',[])))
  173. " 2>/dev/null || echo "?")
  174. if [ "$DENY_COUNT" = "?" ]; then
  175. warn "Could not parse deny count (python3 unavailable or JSON parse error)"
  176. else
  177. EXPECTED_DENY=100
  178. if [ "$DENY_COUNT" -eq "$EXPECTED_DENY" ] 2>/dev/null; then
  179. pass "Deny rules: $DENY_COUNT"
  180. else
  181. warn "Deny rules: $DENY_COUNT (expected $EXPECTED_DENY) — settings may have been manually modified"
  182. fi
  183. fi
  184. else
  185. fail "$HOME/.claude/settings.json not found"
  186. fi
  187. echo ""
  188. # ────────────────────────────────────────────────────────────
  189. # 6. Token budget estimate
  190. # ────────────────────────────────────────────────────────────
  191. echo "── Token budget estimate ──"
  192. # Reference: Claude Code Pro plan ~11k tokens/5h session (session budget, not context window).
  193. # Seuils: WARNING >15%, CRITICAL >30% of session budget.
  194. CLAUDE_MD_CHARS=$(wc -c < "$REPO/CLAUDE.md" 2>/dev/null || echo 0)
  195. CLAUDE_MD_TOKENS=$((CLAUDE_MD_CHARS / 4))
  196. # Skill descriptions only (frontmatter description field — loaded passively at startup)
  197. SKILL_DESC_CHARS=0
  198. for f in "$HOME/.claude/skills/"*/SKILL.md; do
  199. [ -f "$f" ] || continue
  200. desc=$(grep "^description:" "$f" 2>/dev/null | head -1 | sed 's/^description: *//' )
  201. SKILL_DESC_CHARS=$((SKILL_DESC_CHARS + ${#desc}))
  202. done
  203. SKILL_DESC_TOKENS=$((SKILL_DESC_CHARS / 4))
  204. SKILL_COUNT=$(find "$HOME/.claude/skills/" -maxdepth 2 -name "SKILL.md" 2>/dev/null | wc -l | tr -d ' ')
  205. # Plugin passive cost estimates (tokens)
  206. PLUGIN_TOKENS=0
  207. if detect_superpowers 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 800)); fi
  208. if detect_gstack 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 2750)); fi
  209. if detect_uiux_pro_max 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 400)); fi
  210. if detect_context7 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 200)); fi
  211. if detect_graphifyy 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 300)); fi
  212. TOTAL_TOKENS=$((CLAUDE_MD_TOKENS + SKILL_DESC_TOKENS + PLUGIN_TOKENS))
  213. SESSION_BUDGET=11000
  214. PCT=$((TOTAL_TOKENS * 100 / SESSION_BUDGET))
  215. echo ""
  216. echo " CLAUDE.md: ~${CLAUDE_MD_TOKENS}t"
  217. echo " Skill descriptions: ~${SKILL_DESC_TOKENS}t (${SKILL_COUNT} skills)"
  218. echo " Plugin passive cost: ~${PLUGIN_TOKENS}t (active plugins)"
  219. echo " ─────────────────────────────────────────"
  220. info " Total: ~${TOTAL_TOKENS}t"
  221. info " Session budget (Pro): ${SESSION_BUDGET}t"
  222. info " Usage: ~${PCT}%"
  223. echo ""
  224. if [ "$PCT" -gt 30 ]; then
  225. warn "CRITICAL: ${PCT}% of session budget — /plugin-check to disable unused plugins"
  226. elif [ "$PCT" -gt 15 ]; then
  227. warn "WARNING: ${PCT}% of session budget — consider disabling unused toggle plugins"
  228. else
  229. pass "Budget: ${PCT}% (comfortable)"
  230. fi
  231. # Per-file breakdown (skill bodies — loaded on demand, shown for awareness)
  232. if [ "$TOTAL_TOKENS" -gt 2000 ]; then
  233. info "Skill/agent bodies (loaded on demand, >200t each):"
  234. for f in "$HOME/.claude/skills/"*/SKILL.md "$HOME/.claude/agents/"*.md; do
  235. [ -f "$f" ] || continue
  236. size=$(wc -c < "$f" 2>/dev/null || echo 0)
  237. tokens=$((size / 4))
  238. if [ "$tokens" -gt 200 ]; then
  239. label=$(basename "$(dirname "$f")" 2>/dev/null)
  240. [ "$label" = "." ] && label=$(basename "$f")
  241. info " ~${tokens}t ${label}"
  242. fi
  243. done
  244. fi
  245. echo ""
  246. # ────────────────────────────────────────────────────────────
  247. # 7. File consistency
  248. # ────────────────────────────────────────────────────────────
  249. echo "── Consistency ──"
  250. # Check all skills have disable-model-invocation
  251. MISSING_DMI=()
  252. for f in "$HOME/.claude/skills/"*/SKILL.md; do
  253. [ -f "$f" ] || continue
  254. name=$(basename "$(dirname "$f")")
  255. if ! grep -q "disable-model-invocation" "$f" 2>/dev/null; then
  256. MISSING_DMI+=("$name")
  257. fi
  258. done
  259. if [ ${#MISSING_DMI[@]} -eq 0 ]; then
  260. pass "All skills have disable-model-invocation"
  261. else
  262. warn "Skills missing disable-model-invocation: ${MISSING_DMI[*]}"
  263. fi
  264. # Check expected skills are present
  265. EXPECTED_SKILLS=(
  266. "analyze" "health" "init-project" "onboard" "plugin-check"
  267. "readme" "refactor" "ship-feature" "status"
  268. )
  269. MISSING_SKILLS=()
  270. for skill in "${EXPECTED_SKILLS[@]}"; do
  271. if [ ! -f "$HOME/.claude/skills/${skill}/SKILL.md" ]; then
  272. MISSING_SKILLS+=("${skill}/")
  273. fi
  274. done
  275. if [ ${#MISSING_SKILLS[@]} -eq 0 ]; then
  276. pass "All ${#EXPECTED_SKILLS[@]} expected skills present (analyze, health, init-project, onboard, plugin-check, readme, refactor, ship-feature, status)"
  277. else
  278. warn "Missing skills: ${MISSING_SKILLS[*]} — run: bash link.sh"
  279. fi
  280. # Check expected agents are present
  281. EXPECTED_AGENTS=(
  282. "analyzer" "interviewer" "plugin-advisor" "readme-updater"
  283. "refactorer" "scaffolder" "onboarder" "status-reporter"
  284. )
  285. MISSING_AGENTS=()
  286. for agent in "${EXPECTED_AGENTS[@]}"; do
  287. if [ ! -f "$HOME/.claude/agents/${agent}.md" ]; then
  288. MISSING_AGENTS+=("${agent}.md")
  289. fi
  290. done
  291. if [ ${#MISSING_AGENTS[@]} -eq 0 ]; then
  292. pass "All 8 agents present (analyzer, interviewer, plugin-advisor, readme-updater, refactorer, scaffolder, onboarder, status-reporter)"
  293. else
  294. warn "Missing agents: ${MISSING_AGENTS[*]} — run: bash link.sh"
  295. fi
  296. # Check CRLF — portable: grep -P not available on macOS BSD grep
  297. CRLF_FILES=()
  298. for f in "$REPO"/*.md "$REPO"/agents/*.md "$REPO"/skills/*/SKILL.md; do
  299. [ -f "$f" ] || continue
  300. if grep -c $'\r' "$f" 2>/dev/null | grep -q "^[^0]"; then
  301. CRLF_FILES+=("$(basename "$f")")
  302. fi
  303. done
  304. if [ ${#CRLF_FILES[@]} -eq 0 ]; then
  305. pass "No CRLF line endings detected"
  306. else
  307. warn "CRLF detected in: ${CRLF_FILES[*]}"
  308. fi
  309. echo ""
  310. # ────────────────────────────────────────────────────────────
  311. # Summary
  312. # ────────────────────────────────────────────────────────────
  313. echo "═══════════════════════════════════════════"
  314. if [ "$ERRORS" -gt 0 ]; then
  315. echo -e "${RED} $ERRORS error(s)${NC}, ${YELLOW}$WARNS warning(s)${NC}"
  316. echo ""
  317. echo " Fix: cd $REPO && bash link.sh && bash install-plugins.sh"
  318. exit 1
  319. elif [ "$WARNS" -gt 0 ]; then
  320. echo -e " ${GREEN}No errors${NC}, ${YELLOW}$WARNS warning(s)${NC}"
  321. else
  322. echo -e " ${GREEN}All checks passed ✓${NC}"
  323. fi
  324. echo ""