lint-agents.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/env bash
  2. #
  3. # Validates agent markdown files:
  4. # 1. YAML frontmatter must exist with name, description, color (ERROR)
  5. # 2. Recommended sections checked but only warned (WARN)
  6. # 3. File must have meaningful content
  7. #
  8. # Usage: ./scripts/lint-agents.sh [file ...]
  9. # If no files given, scans all agent directories.
  10. set -euo pipefail
  11. # Keep in sync with AGENT_DIRS in scripts/convert.sh
  12. AGENT_DIRS=(
  13. academic
  14. design
  15. engineering
  16. finance
  17. game-development
  18. gis
  19. healthcare
  20. marketing
  21. paid-media
  22. product
  23. project-management
  24. sales
  25. security
  26. spatial-computing
  27. specialized
  28. support
  29. testing
  30. )
  31. REQUIRED_FRONTMATTER=("name" "description" "color")
  32. RECOMMENDED_SECTIONS=("Identity" "Core Mission" "Critical Rules")
  33. errors=0
  34. warnings=0
  35. classify_header_target() {
  36. local header_lower="$1"
  37. if [[ "$header_lower" =~ identity ]] ||
  38. [[ "$header_lower" =~ learning.*memory ]] ||
  39. [[ "$header_lower" =~ communication ]] ||
  40. [[ "$header_lower" =~ style ]] ||
  41. [[ "$header_lower" =~ critical.rule ]] ||
  42. [[ "$header_lower" =~ rules.you.must.follow ]]; then
  43. printf 'soul'
  44. else
  45. printf 'agents'
  46. fi
  47. }
  48. lint_file() {
  49. local file="$1"
  50. if [[ ! -f "$file" ]]; then
  51. echo "ERROR $file: not a file or does not exist"
  52. errors=$((errors + 1))
  53. return
  54. fi
  55. # 0. Reject CRLF line endings (repo standard is LF — see .gitattributes).
  56. # A trailing \r otherwise makes the frontmatter check below fail with a
  57. # confusing "missing frontmatter ---" even when the file clearly starts ---.
  58. if LC_ALL=C grep -q $'\r' "$file"; then
  59. echo "ERROR $file: CRLF line endings detected — convert to LF (e.g. 'perl -i -pe \"s/\\r\$//\" $file'); repo uses LF per .gitattributes"
  60. errors=$((errors + 1))
  61. return
  62. fi
  63. # 1. Check frontmatter delimiters
  64. local first_line
  65. first_line=$(head -1 "$file")
  66. if [[ "$first_line" != "---" ]]; then
  67. echo "ERROR $file: missing frontmatter opening ---"
  68. errors=$((errors + 1))
  69. return
  70. fi
  71. # Extract frontmatter (between first and second ---)
  72. local frontmatter
  73. frontmatter=$(awk 'NR==1{next} /^---$/{exit} {print}' "$file")
  74. if [[ -z "$frontmatter" ]]; then
  75. echo "ERROR $file: empty or malformed frontmatter"
  76. errors=$((errors + 1))
  77. return
  78. fi
  79. # 2. Check required frontmatter fields
  80. for field in "${REQUIRED_FRONTMATTER[@]}"; do
  81. if ! echo "$frontmatter" | grep -qE "^${field}:"; then
  82. echo "ERROR $file: missing frontmatter field '${field}'"
  83. errors=$((errors + 1))
  84. fi
  85. done
  86. # 3. Check recommended sections (warn only)
  87. local body
  88. body=$(awk 'BEGIN{n=0} /^---$/{n++; next} n>=2{print}' "$file")
  89. for section in "${RECOMMENDED_SECTIONS[@]}"; do
  90. if ! echo "$body" | grep -qi "$section"; then
  91. echo "WARN $file: missing recommended section '${section}'"
  92. warnings=$((warnings + 1))
  93. fi
  94. done
  95. # 4. Check file has meaningful content (awk strips wc's leading whitespace on macOS/BSD)
  96. local word_count
  97. word_count=$(echo "$body" | wc -w | awk '{print $1}')
  98. if [[ "${word_count:-0}" -lt 50 ]]; then
  99. echo "WARN $file: body seems very short (< 50 words)"
  100. warnings=$((warnings + 1))
  101. fi
  102. local soul_headers=0
  103. local agents_headers=0
  104. while IFS= read -r line; do
  105. if [[ "$line" =~ ^##[[:space:]] ]]; then
  106. local header_lower
  107. header_lower=$(printf '%s' "$line" | tr '[:upper:]' '[:lower:]')
  108. local target
  109. target=$(classify_header_target "$header_lower")
  110. if [[ "$target" == "soul" ]]; then
  111. soul_headers=$((soul_headers + 1))
  112. else
  113. agents_headers=$((agents_headers + 1))
  114. fi
  115. fi
  116. done <<< "$body"
  117. if [[ $soul_headers -eq 0 ]]; then
  118. echo "WARN $file: no section headers map to SOUL.md in convert.sh"
  119. warnings=$((warnings + 1))
  120. fi
  121. if [[ $agents_headers -eq 0 ]]; then
  122. echo "WARN $file: no section headers map to AGENTS.md in convert.sh"
  123. warnings=$((warnings + 1))
  124. fi
  125. }
  126. # Collect files to lint
  127. files=()
  128. if [[ $# -gt 0 ]]; then
  129. files=("$@")
  130. else
  131. for dir in "${AGENT_DIRS[@]}"; do
  132. if [[ -d "$dir" ]]; then
  133. while IFS= read -r f; do
  134. files+=("$f")
  135. done < <(find "$dir" -name "*.md" -type f | sort)
  136. fi
  137. done
  138. fi
  139. if [[ ${#files[@]} -eq 0 ]]; then
  140. echo "No agent files found."
  141. exit 1
  142. fi
  143. echo "Linting ${#files[@]} agent files..."
  144. echo ""
  145. for file in "${files[@]}"; do
  146. lint_file "$file"
  147. done
  148. echo ""
  149. echo "Results: ${errors} error(s), ${warnings} warning(s) in ${#files[@]} files."
  150. if [[ $errors -gt 0 ]]; then
  151. echo "FAILED: fix the errors above before merging."
  152. exit 1
  153. else
  154. echo "PASSED"
  155. exit 0
  156. fi