lint-skill.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/usr/bin/env node
  2. // lint-skill.js — Mavis SKILL.md validator
  3. //
  4. // Usage: node lint-skill.js <path/to/skill-directory>
  5. // Exit codes: 0 = pass, 1 = errors found.
  6. //
  7. // See Step 5 in the adjacent SKILL.md for validation checks.
  8. // No third-party dependencies; uses only Node.js built-in fs + path.
  9. import { readFileSync, existsSync, readdirSync, statSync } from 'node:fs';
  10. import { join, basename, resolve } from 'node:path';
  11. const FORBIDDEN_FILES = ['README.md', 'CHANGELOG.md', 'INSTALLATION.md', 'INSTALLATION_GUIDE.md', 'QUICK_REFERENCE.md'];
  12. const FORBIDDEN_FRONTMATTER_KEYS = ['allowed-tools', 'license', 'model'];
  13. const MAX_BODY_LINES = 500;
  14. const REDUNDANT_WHEN_TO_USE_HEADINGS = [
  15. '## When to use',
  16. '## When to use this skill',
  17. '## When this skill should be used',
  18. ];
  19. const README_SMELLS = [
  20. '## How it works',
  21. '## Usage',
  22. '### Command line',
  23. '### As a Python module',
  24. '### As a Node module',
  25. '## API',
  26. ];
  27. function fail(msg) {
  28. console.error(`[FAIL] ${msg}`);
  29. return false;
  30. }
  31. function warn(msg) {
  32. console.error(`[WARN] ${msg}`);
  33. }
  34. function ok(msg) {
  35. console.log(`[OK] ${msg}`);
  36. }
  37. // Minimal frontmatter parser: only top-level key: value entries enclosed by `---`.
  38. // No nesting or complex YAML beyond multiline strings; sufficient for SKILL.md.
  39. function parseFrontmatter(content) {
  40. const lines = content.split('\n');
  41. if (lines[0]?.trim() !== '---') return { frontmatter: null, bodyStart: 0 };
  42. let endIdx = -1;
  43. for (let i = 1; i < lines.length; i++) {
  44. if (lines[i].trim() === '---') {
  45. endIdx = i;
  46. break;
  47. }
  48. }
  49. if (endIdx === -1) return { frontmatter: null, bodyStart: 0 };
  50. const fm = {};
  51. let currentKey = null;
  52. let currentValue = [];
  53. let inBlockScalar = false;
  54. for (let i = 1; i < endIdx; i++) {
  55. const line = lines[i];
  56. if (inBlockScalar) {
  57. if (/^[a-zA-Z_-]+\s*:/.test(line)) {
  58. // Flush the previous key when a new one starts.
  59. fm[currentKey] = currentValue.join('\n').trim();
  60. inBlockScalar = false;
  61. currentKey = null;
  62. currentValue = [];
  63. // Do not continue; proceed to parse the new line.
  64. } else {
  65. currentValue.push(line.replace(/^\s{0,4}/, ''));
  66. continue;
  67. }
  68. }
  69. const m = line.match(/^([a-zA-Z_-]+)\s*:\s*(.*)$/);
  70. if (m) {
  71. const [, k, v] = m;
  72. if (v === '|' || v === '>' || v === '|-' || v === '>-') {
  73. // Block scalar
  74. currentKey = k;
  75. currentValue = [];
  76. inBlockScalar = true;
  77. } else {
  78. fm[k] = v.trim();
  79. }
  80. }
  81. }
  82. if (inBlockScalar && currentKey) {
  83. fm[currentKey] = currentValue.join('\n').trim();
  84. }
  85. return { frontmatter: fm, bodyStart: endIdx + 1 };
  86. }
  87. function lintSkill(skillDir) {
  88. const dirAbs = resolve(skillDir);
  89. const dirName = basename(dirAbs);
  90. const skillMdPath = join(dirAbs, 'SKILL.md');
  91. console.log(`Linting skill at: ${dirAbs}`);
  92. console.log('');
  93. let allOk = true;
  94. // 0. SKILL.md must exist.
  95. if (!existsSync(skillMdPath)) {
  96. fail(`SKILL.md not found at ${skillMdPath}`);
  97. process.exit(1);
  98. }
  99. ok('SKILL.md exists');
  100. const content = readFileSync(skillMdPath, 'utf-8');
  101. const { frontmatter, bodyStart } = parseFrontmatter(content);
  102. // 1. Frontmatter must exist.
  103. if (!frontmatter) {
  104. fail('frontmatter (--- ... ---) missing or unclosed');
  105. process.exit(1);
  106. }
  107. ok('frontmatter parsed');
  108. // 2. name is required, must use kebab-case, and must match the directory name.
  109. const name = frontmatter.name;
  110. if (!name) {
  111. allOk = fail('frontmatter.name is missing') && allOk;
  112. } else if (!/^[a-z][a-z0-9-]*$/.test(name)) {
  113. allOk = fail(`frontmatter.name "${name}" is not kebab-case (lowercase letters/digits/hyphens, start with letter)`) && allOk;
  114. } else if (name !== dirName) {
  115. allOk = fail(`frontmatter.name "${name}" does not match directory name "${dirName}"`) && allOk;
  116. } else {
  117. ok(`name "${name}" is valid kebab-case and matches directory`);
  118. }
  119. // 3. description is required and must include a trigger phrase.
  120. const desc = frontmatter.description;
  121. if (!desc) {
  122. allOk = fail('frontmatter.description is missing') && allOk;
  123. } else {
  124. const hasTriggerHint = /\b(when|trigger|use this|load this)\b/i.test(desc) || /["'']/.test(desc);
  125. if (!hasTriggerHint) {
  126. allOk = fail('frontmatter.description must contain at least one trigger phrase: a quoted phrase, "when", "trigger", "use this", or "load this"') && allOk;
  127. } else {
  128. ok('description contains trigger hint');
  129. }
  130. if (desc.length < 30) {
  131. warn(`description is very short (${desc.length} chars); make sure it covers what + when + near misses`);
  132. }
  133. }
  134. // 4. Forbidden frontmatter fields.
  135. for (const key of FORBIDDEN_FRONTMATTER_KEYS) {
  136. if (key in frontmatter) {
  137. allOk = fail(`frontmatter contains forbidden key "${key}" (Mavis does not recognize this; remove it)`) && allOk;
  138. }
  139. }
  140. if (FORBIDDEN_FRONTMATTER_KEYS.every((k) => !(k in frontmatter))) {
  141. ok('no forbidden frontmatter keys');
  142. }
  143. // 5. Body line-count check.
  144. const bodyLines = content.split('\n').length - bodyStart;
  145. if (bodyLines > MAX_BODY_LINES) {
  146. allOk = fail(`SKILL.md body is ${bodyLines} lines (> ${MAX_BODY_LINES}); split into references/<topic>.md`) && allOk;
  147. } else {
  148. ok(`body is ${bodyLines} lines (limit ${MAX_BODY_LINES})`);
  149. }
  150. // 5.1 README-style anti-pattern check.
  151. const body = content.split('\n').slice(bodyStart).join('\n');
  152. const hasRedundantWhenToUse = REDUNDANT_WHEN_TO_USE_HEADINGS.some((heading) => body.includes(heading));
  153. if (hasRedundantWhenToUse) {
  154. warn('body contains a "When to use" heading; prefer putting trigger/boundary rules in frontmatter.description to avoid duplication');
  155. }
  156. const readmeMatches = README_SMELLS.filter((heading) => body.includes(heading));
  157. if (readmeMatches.length > 0) {
  158. warn(`body contains README-style section(s): ${readmeMatches.join(', ')}; keep SKILL.md focused on execution rules unless these sections are truly necessary`);
  159. }
  160. // 5.2 Windows compatibility: WARN if the body indicates shell commands but the text never mentions windows/win32.
  161. const shellIndicators = [/python3\b/, /bash\s+scripts\//, /brew\s+install/, /\/tmp\//, /```(?:bash|sh)\b/];
  162. const hasShellIndicator = shellIndicators.some((re) => re.test(body));
  163. const mentionsWindows = /windows|win32/i.test(content);
  164. if (hasShellIndicator && !mentionsWindows) {
  165. warn('body contains shell/bash commands but does not mention Windows/win32; consider adding a "## Windows (win32) platform notes" section for cross-platform support');
  166. }
  167. // 6. Forbidden-file check.
  168. const entries = readdirSync(dirAbs);
  169. for (const entry of entries) {
  170. if (FORBIDDEN_FILES.includes(entry)) {
  171. allOk = fail(`forbidden file "${entry}" in skill directory (skill is for LLM, not human docs); remove it`) && allOk;
  172. }
  173. }
  174. if (FORBIDDEN_FILES.every((f) => !entries.includes(f))) {
  175. ok('no forbidden files in skill directory');
  176. }
  177. // 7. Referenced files under references/ must exist.
  178. const refRefs = [...content.matchAll(/references\/([a-zA-Z0-9_-]+\.md)/g)].map((m) => m[1]);
  179. const uniqueRefs = [...new Set(refRefs)];
  180. for (const ref of uniqueRefs) {
  181. const refPath = join(dirAbs, 'references', ref);
  182. if (!existsSync(refPath)) {
  183. allOk = fail(`SKILL.md references "references/${ref}" but file does not exist`) && allOk;
  184. }
  185. }
  186. if (uniqueRefs.length > 0 && uniqueRefs.every((r) => existsSync(join(dirAbs, 'references', r)))) {
  187. ok(`all ${uniqueRefs.length} references/*.md links resolve`);
  188. }
  189. console.log('');
  190. if (allOk) {
  191. console.log('✓ All checks passed');
  192. process.exit(0);
  193. } else {
  194. console.error('✗ Lint failed');
  195. process.exit(1);
  196. }
  197. }
  198. const arg = process.argv[2];
  199. if (!arg) {
  200. console.error('Usage: node lint-skill.js <path/to/skill-directory>');
  201. process.exit(2);
  202. }
  203. lintSkill(arg);