|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Deep audit of plugin layers: skills, rules, commands, agents, hooks. |
| 4 | + * Run from publish repo root: node scripts/audit-layers.mjs |
| 5 | + */ |
| 6 | +import fs from 'fs'; |
| 7 | +import path from 'path'; |
| 8 | +import { fileURLToPath } from 'url'; |
| 9 | + |
| 10 | +const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 11 | +const PLUGIN = path.join(ROOT, 'plugins', 'agentstack'); |
| 12 | +let fails = 0; |
| 13 | +let warns = 0; |
| 14 | + |
| 15 | +function ok(m) { |
| 16 | + console.log(`OK ${m}`); |
| 17 | +} |
| 18 | +function warn(m) { |
| 19 | + console.warn(`WARN ${m}`); |
| 20 | + warns += 1; |
| 21 | +} |
| 22 | +function fail(m) { |
| 23 | + console.error(`FAIL ${m}`); |
| 24 | + fails += 1; |
| 25 | +} |
| 26 | + |
| 27 | +function listDirs(dir) { |
| 28 | + if (!fs.existsSync(dir)) return []; |
| 29 | + return fs.readdirSync(dir, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name); |
| 30 | +} |
| 31 | + |
| 32 | +function listFiles(dir, ext) { |
| 33 | + if (!fs.existsSync(dir)) return []; |
| 34 | + return fs.readdirSync(dir).filter((f) => f.endsWith(ext)); |
| 35 | +} |
| 36 | + |
| 37 | +function parseFm(content) { |
| 38 | + const m = content.match(/^---\r?\n([\s\S]*?)\r?\n---/); |
| 39 | + if (!m) return null; |
| 40 | + const fields = {}; |
| 41 | + for (const line of m[1].split(/\r?\n/)) { |
| 42 | + const kv = line.match(/^([a-zA-Z_][\w-]*):\s*(.*)$/); |
| 43 | + if (kv) fields[kv[1]] = kv[2].trim(); |
| 44 | + } |
| 45 | + return fields; |
| 46 | +} |
| 47 | + |
| 48 | +console.log(`Layer audit — ${PLUGIN}\n`); |
| 49 | + |
| 50 | +// Skills |
| 51 | +const skillDirs = listDirs(path.join(PLUGIN, 'skills')); |
| 52 | +const missingLive = []; |
| 53 | +const skillNames = new Set(); |
| 54 | +for (const d of skillDirs) { |
| 55 | + const p = path.join(PLUGIN, 'skills', d, 'SKILL.md'); |
| 56 | + if (!fs.existsSync(p)) { |
| 57 | + fail(`skills/${d}: missing SKILL.md`); |
| 58 | + continue; |
| 59 | + } |
| 60 | + const t = fs.readFileSync(p, 'utf8'); |
| 61 | + const fm = parseFm(t); |
| 62 | + if (!fm?.name || !fm?.description) fail(`skills/${d}: frontmatter needs name + description`); |
| 63 | + else { |
| 64 | + skillNames.add(fm.name); |
| 65 | + ok(`skill ${fm.name}`); |
| 66 | + } |
| 67 | + if (!/\/mcp\/actions|live catalog/i.test(t)) missingLive.push(d); |
| 68 | + if (/docs\/MCP_CAPABILITY_MATRIX\.md/.test(t)) { |
| 69 | + fail(`skills/${d}: references docs/MCP_CAPABILITY_MATRIX.md (not in package) — use GET /mcp/actions`); |
| 70 | + } |
| 71 | +} |
| 72 | +if (missingLive.length) { |
| 73 | + for (const d of missingLive) warn(`skills/${d}: add live catalog pointer (GET /mcp/actions)`); |
| 74 | +} else { |
| 75 | + ok('all skills mention live catalog /mcp/actions'); |
| 76 | +} |
| 77 | + |
| 78 | +// Rules |
| 79 | +const rules = listFiles(path.join(PLUGIN, 'rules'), '.mdc'); |
| 80 | +let always = 0; |
| 81 | +for (const f of rules) { |
| 82 | + const t = fs.readFileSync(path.join(PLUGIN, 'rules', f), 'utf8'); |
| 83 | + const fm = parseFm(t); |
| 84 | + if (!fm?.description) fail(`rules/${f}: missing description frontmatter`); |
| 85 | + if (/alwaysApply:\s*true/.test(t)) always += 1; |
| 86 | + ok(`rule ${f}`); |
| 87 | +} |
| 88 | +if (always !== 1) fail(`expected exactly 1 alwaysApply rule, got ${always}`); |
| 89 | +else ok('exactly 1 alwaysApply rule (agentstack-prefer)'); |
| 90 | + |
| 91 | +// Commands |
| 92 | +const cmds = listFiles(path.join(PLUGIN, 'commands'), '.md'); |
| 93 | +for (const f of cmds) { |
| 94 | + const t = fs.readFileSync(path.join(PLUGIN, 'commands', f), 'utf8'); |
| 95 | + const fm = parseFm(t); |
| 96 | + if (!fm?.name || !fm?.description) fail(`commands/${f}: frontmatter needs name + description`); |
| 97 | + else ok(`command ${fm.name}`); |
| 98 | +} |
| 99 | +for (const need of ['agentstack-init.md', 'agentstack-login.md', 'agentstack-diagnose.md']) { |
| 100 | + if (!cmds.includes(need)) fail(`missing command ${need}`); |
| 101 | +} |
| 102 | + |
| 103 | +// Agents |
| 104 | +const agents = listFiles(path.join(PLUGIN, 'agents'), '.md'); |
| 105 | +for (const f of agents) { |
| 106 | + const t = fs.readFileSync(path.join(PLUGIN, 'agents', f), 'utf8'); |
| 107 | + const fm = parseFm(t); |
| 108 | + if (!fm?.name || !fm?.description) fail(`agents/${f}: frontmatter needs name + description`); |
| 109 | + else ok(`agent ${fm.name}`); |
| 110 | +} |
| 111 | + |
| 112 | +// Hooks |
| 113 | +const hooksPath = path.join(PLUGIN, 'hooks/hooks.json'); |
| 114 | +const hooks = JSON.parse(fs.readFileSync(hooksPath, 'utf8')); |
| 115 | +const requiredEvents = [ |
| 116 | + 'sessionStart', |
| 117 | + 'sessionEnd', |
| 118 | + 'beforeShellExecution', |
| 119 | + 'beforeMCPExecution', |
| 120 | + 'postToolUse', |
| 121 | + 'postToolUseFailure', |
| 122 | + 'afterFileEdit', |
| 123 | +]; |
| 124 | +for (const ev of requiredEvents) { |
| 125 | + const list = hooks.hooks?.[ev]; |
| 126 | + if (!Array.isArray(list) || !list.length) fail(`hooks.json missing ${ev}`); |
| 127 | + else { |
| 128 | + for (const entry of list) { |
| 129 | + const m = String(entry.command || '').match(/\.\/(.+\.mjs)/); |
| 130 | + if (!m || !fs.existsSync(path.join(PLUGIN, m[1]))) fail(`hooks ${ev}: missing ${entry.command}`); |
| 131 | + else ok(`hook ${ev} → ${m[1]}`); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// device-code is not a hook event but required script |
| 137 | +if (!fs.existsSync(path.join(PLUGIN, 'hooks/scripts/device-code.mjs'))) { |
| 138 | + fail('hooks/scripts/device-code.mjs missing'); |
| 139 | +} else ok('device-code.mjs present (install path)'); |
| 140 | + |
| 141 | +// Backend router covers skill folders (name match or known alias) |
| 142 | +const backend = fs.readFileSync(path.join(PLUGIN, 'skills/agentstack-backend/SKILL.md'), 'utf8'); |
| 143 | +const optional = new Set(['solana']); |
| 144 | +for (const d of skillDirs) { |
| 145 | + if (d === 'agentstack-backend') continue; |
| 146 | + if (optional.has(d)) continue; |
| 147 | + if (!backend.includes(d) && !backend.includes(`\`${d}\``)) { |
| 148 | + warn(`backend router may omit skill folder ${d}`); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +console.log(`\nsummary: failed=${fails} warnings=${warns}`); |
| 153 | +console.log(`counts: skills=${skillDirs.length} rules=${rules.length} commands=${cmds.length} agents=${agents.length}`); |
| 154 | +process.exit(fails ? 1 : 0); |
0 commit comments