doctor.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. gstack_skills_count=$(find "$HOME/.claude/skills/gstack/skills/" -maxdepth 1 -mindepth 1 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_graphifyy; then
  150. pass "Graphifyy installed (graphify CLI)"
  151. else
  152. info "Graphifyy not installed (optional — codebase knowledge graph: pipx install graphifyy)"
  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 "$HOME/.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_uiux_pro_max 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 400)); fi
  208. if detect_context7 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 200)); fi
  209. if detect_graphifyy 2>/dev/null; then PLUGIN_TOKENS=$((PLUGIN_TOKENS + 300)); fi
  210. TOTAL_TOKENS=$((CLAUDE_MD_TOKENS + SKILL_DESC_TOKENS + PLUGIN_TOKENS))
  211. SESSION_BUDGET=11000
  212. PCT=$((TOTAL_TOKENS * 100 / SESSION_BUDGET))
  213. echo ""
  214. echo " CLAUDE.md: ~${CLAUDE_MD_TOKENS}t"
  215. echo " Skill descriptions: ~${SKILL_DESC_TOKENS}t (${SKILL_COUNT} skills)"
  216. echo " Plugin passive cost: ~${PLUGIN_TOKENS}t (active plugins)"
  217. echo " ─────────────────────────────────────────"
  218. info " Total: ~${TOTAL_TOKENS}t"
  219. info " Session budget (Pro): ${SESSION_BUDGET}t"
  220. info " Usage: ~${PCT}%"
  221. echo ""
  222. if [ "$PCT" -gt 30 ]; then
  223. warn "CRITICAL: ${PCT}% of session budget — /plugin-check to disable unused plugins"
  224. elif [ "$PCT" -gt 15 ]; then
  225. warn "WARNING: ${PCT}% of session budget — consider disabling unused toggle plugins"
  226. else
  227. pass "Budget: ${PCT}% (comfortable)"
  228. fi
  229. # Per-file breakdown (skill bodies — loaded on demand, shown for awareness)
  230. if [ "$TOTAL_TOKENS" -gt 2000 ]; then
  231. info "Skill/agent bodies (loaded on demand, >200t each):"
  232. for f in "$HOME/.claude/skills/"*/SKILL.md "$HOME/.claude/agents/"*.md; do
  233. [ -f "$f" ] || continue
  234. size=$(wc -c < "$f" 2>/dev/null || echo 0)
  235. tokens=$((size / 4))
  236. if [ "$tokens" -gt 200 ]; then
  237. label=$(basename "$(dirname "$f")" 2>/dev/null)
  238. [ "$label" = "." ] && label=$(basename "$f")
  239. info " ~${tokens}t ${label}"
  240. fi
  241. done
  242. fi
  243. echo ""
  244. # ────────────────────────────────────────────────────────────
  245. # 7. File consistency
  246. # ────────────────────────────────────────────────────────────
  247. echo "── Consistency ──"
  248. # Check all skills have disable-model-invocation
  249. MISSING_DMI=()
  250. for f in "$HOME/.claude/skills/"*/SKILL.md; do
  251. [ -f "$f" ] || continue
  252. name=$(basename "$(dirname "$f")")
  253. if ! grep -q "disable-model-invocation" "$f" 2>/dev/null; then
  254. MISSING_DMI+=("$name")
  255. fi
  256. done
  257. if [ ${#MISSING_DMI[@]} -eq 0 ]; then
  258. pass "All skills have disable-model-invocation"
  259. else
  260. warn "Skills missing disable-model-invocation: ${MISSING_DMI[*]}"
  261. fi
  262. # Check expected skills are present
  263. EXPECTED_SKILLS=(
  264. "analyze" "health" "init-project" "onboard" "plugin-check"
  265. "readme" "refactor" "ship-feature" "status"
  266. )
  267. MISSING_SKILLS=()
  268. for skill in "${EXPECTED_SKILLS[@]}"; do
  269. if [ ! -f "$HOME/.claude/skills/${skill}/SKILL.md" ]; then
  270. MISSING_SKILLS+=("${skill}/")
  271. fi
  272. done
  273. if [ ${#MISSING_SKILLS[@]} -eq 0 ]; then
  274. pass "All ${#EXPECTED_SKILLS[@]} expected skills present (analyze, health, init-project, onboard, plugin-check, readme, refactor, ship-feature, status)"
  275. else
  276. warn "Missing skills: ${MISSING_SKILLS[*]} — run: bash link.sh"
  277. fi
  278. # Check expected agents are present
  279. EXPECTED_AGENTS=(
  280. "analyzer" "interviewer" "plugin-advisor" "readme-updater"
  281. "refactorer" "scaffolder" "onboarder" "status-reporter"
  282. )
  283. MISSING_AGENTS=()
  284. for agent in "${EXPECTED_AGENTS[@]}"; do
  285. if [ ! -f "$HOME/.claude/agents/${agent}.md" ]; then
  286. MISSING_AGENTS+=("${agent}.md")
  287. fi
  288. done
  289. if [ ${#MISSING_AGENTS[@]} -eq 0 ]; then
  290. pass "All 8 agents present (analyzer, interviewer, plugin-advisor, readme-updater, refactorer, scaffolder, onboarder, status-reporter)"
  291. else
  292. warn "Missing agents: ${MISSING_AGENTS[*]} — run: bash link.sh"
  293. fi
  294. # Check CRLF — portable: grep -P not available on macOS BSD grep
  295. CRLF_FILES=()
  296. for f in "$REPO"/*.md "$REPO"/agents/*.md "$REPO"/skills/*/SKILL.md; do
  297. [ -f "$f" ] || continue
  298. if grep -c $'\r' "$f" 2>/dev/null | grep -q "^[^0]"; then
  299. CRLF_FILES+=("$(basename "$f")")
  300. fi
  301. done
  302. if [ ${#CRLF_FILES[@]} -eq 0 ]; then
  303. pass "No CRLF line endings detected"
  304. else
  305. warn "CRLF detected in: ${CRLF_FILES[*]}"
  306. fi
  307. echo ""
  308. # ────────────────────────────────────────────────────────────
  309. # Summary
  310. # ────────────────────────────────────────────────────────────
  311. echo "═══════════════════════════════════════════"
  312. if [ "$ERRORS" -gt 0 ]; then
  313. echo -e "${RED} $ERRORS error(s)${NC}, ${YELLOW}$WARNS warning(s)${NC}"
  314. echo ""
  315. echo " Fix: cd $REPO && bash link.sh && bash install-plugins.sh"
  316. exit 1
  317. elif [ "$WARNS" -gt 0 ]; then
  318. echo -e " ${GREEN}No errors${NC}, ${YELLOW}$WARNS warning(s)${NC}"
  319. else
  320. echo -e " ${GREEN}All checks passed ✓${NC}"
  321. fi
  322. echo ""