javi-forge 1.5.0 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +191 -3
- package/ci-local/hooks/pre-push +17 -13
- package/dist/commands/analyze.d.ts +1 -1
- package/dist/commands/analyze.js +15 -15
- package/dist/commands/atlassian-mcp.d.ts +42 -0
- package/dist/commands/atlassian-mcp.js +98 -0
- package/dist/commands/ci.d.ts +3 -3
- package/dist/commands/ci.js +185 -147
- package/dist/commands/crash-recovery.d.ts +34 -0
- package/dist/commands/crash-recovery.js +123 -0
- package/dist/commands/doctor.d.ts +2 -2
- package/dist/commands/doctor.js +113 -61
- package/dist/commands/harness-audit.d.ts +35 -0
- package/dist/commands/harness-audit.js +277 -0
- package/dist/commands/init.d.ts +1 -1
- package/dist/commands/init.js +415 -118
- package/dist/commands/llmstxt.d.ts +1 -1
- package/dist/commands/llmstxt.js +36 -34
- package/dist/commands/parallel-batch.d.ts +42 -0
- package/dist/commands/parallel-batch.js +90 -0
- package/dist/commands/plugin.d.ts +26 -1
- package/dist/commands/plugin.js +138 -24
- package/dist/commands/secret-scanner.d.ts +30 -0
- package/dist/commands/secret-scanner.js +272 -0
- package/dist/commands/security-analysis.d.ts +74 -0
- package/dist/commands/security-analysis.js +487 -0
- package/dist/commands/security.d.ts +31 -0
- package/dist/commands/security.js +445 -0
- package/dist/commands/skill-scanner.d.ts +63 -0
- package/dist/commands/skill-scanner.js +383 -0
- package/dist/commands/skills.d.ts +139 -0
- package/dist/commands/skills.js +895 -0
- package/dist/commands/supply-chain.d.ts +23 -0
- package/dist/commands/supply-chain.js +126 -0
- package/dist/commands/tdd-pipeline.d.ts +17 -0
- package/dist/commands/tdd-pipeline.js +144 -0
- package/dist/commands/tdd.d.ts +21 -0
- package/dist/commands/tdd.js +120 -0
- package/dist/commands/team-presets.d.ts +53 -0
- package/dist/commands/team-presets.js +201 -0
- package/dist/commands/workflow.d.ts +23 -0
- package/dist/commands/workflow.js +114 -0
- package/dist/constants.d.ts +21 -0
- package/dist/constants.js +208 -37
- package/dist/index.js +400 -54
- package/dist/lib/agent-skills.d.ts +73 -0
- package/dist/lib/agent-skills.js +260 -0
- package/dist/lib/auto-skill-install.d.ts +37 -0
- package/dist/lib/auto-skill-install.js +92 -0
- package/dist/lib/auto-wire.d.ts +20 -0
- package/dist/lib/auto-wire.js +240 -0
- package/dist/lib/claudemd.d.ts +20 -0
- package/dist/lib/claudemd.js +222 -0
- package/dist/lib/codex-export.d.ts +16 -0
- package/dist/lib/codex-export.js +109 -0
- package/dist/lib/common.d.ts +1 -1
- package/dist/lib/common.js +52 -44
- package/dist/lib/context.d.ts +27 -0
- package/dist/lib/context.js +204 -0
- package/dist/lib/docker.d.ts +1 -1
- package/dist/lib/docker.js +141 -112
- package/dist/lib/frontmatter.d.ts +1 -1
- package/dist/lib/frontmatter.js +29 -15
- package/dist/lib/plugin.d.ts +19 -1
- package/dist/lib/plugin.js +174 -47
- package/dist/lib/skill-publish.d.ts +40 -0
- package/dist/lib/skill-publish.js +146 -0
- package/dist/lib/stack-detector.d.ts +38 -0
- package/dist/lib/stack-detector.js +207 -0
- package/dist/lib/template.d.ts +16 -1
- package/dist/lib/template.js +46 -17
- package/dist/lib/workflow/discovery.d.ts +19 -0
- package/dist/lib/workflow/discovery.js +68 -0
- package/dist/lib/workflow/index.d.ts +5 -0
- package/dist/lib/workflow/index.js +5 -0
- package/dist/lib/workflow/parser.d.ts +16 -0
- package/dist/lib/workflow/parser.js +198 -0
- package/dist/lib/workflow/renderer.d.ts +9 -0
- package/dist/lib/workflow/renderer.js +152 -0
- package/dist/lib/workflow/validator.d.ts +10 -0
- package/dist/lib/workflow/validator.js +189 -0
- package/dist/tasks/index.d.ts +4 -0
- package/dist/tasks/index.js +4 -0
- package/dist/tasks/scaffold-tasks.d.ts +3 -0
- package/dist/tasks/scaffold-tasks.js +14 -0
- package/dist/tasks/task-id.d.ts +30 -0
- package/dist/tasks/task-id.js +55 -0
- package/dist/tasks/task-tracker.d.ts +15 -0
- package/dist/tasks/task-tracker.js +81 -0
- package/dist/types/index.d.ts +252 -5
- package/dist/types/index.js +11 -1
- package/dist/ui/AnalyzeUI.d.ts +1 -1
- package/dist/ui/AnalyzeUI.js +38 -39
- package/dist/ui/App.d.ts +5 -3
- package/dist/ui/App.js +92 -46
- package/dist/ui/AutoSkills.d.ts +9 -0
- package/dist/ui/AutoSkills.js +124 -0
- package/dist/ui/CI.d.ts +2 -2
- package/dist/ui/CI.js +24 -26
- package/dist/ui/CIContext.d.ts +1 -1
- package/dist/ui/CIContext.js +3 -2
- package/dist/ui/CISelector.d.ts +2 -2
- package/dist/ui/CISelector.js +23 -15
- package/dist/ui/Doctor.d.ts +1 -1
- package/dist/ui/Doctor.js +35 -29
- package/dist/ui/Header.d.ts +1 -1
- package/dist/ui/Header.js +14 -14
- package/dist/ui/HookProfileSelector.d.ts +9 -0
- package/dist/ui/HookProfileSelector.js +54 -0
- package/dist/ui/LlmsTxt.d.ts +1 -1
- package/dist/ui/LlmsTxt.js +31 -22
- package/dist/ui/MemorySelector.d.ts +2 -2
- package/dist/ui/MemorySelector.js +28 -16
- package/dist/ui/NameInput.d.ts +1 -1
- package/dist/ui/NameInput.js +21 -21
- package/dist/ui/OptionSelector.d.ts +8 -2
- package/dist/ui/OptionSelector.js +83 -26
- package/dist/ui/Plugin.d.ts +4 -3
- package/dist/ui/Plugin.js +89 -29
- package/dist/ui/Progress.d.ts +3 -3
- package/dist/ui/Progress.js +23 -22
- package/dist/ui/Skills.d.ts +11 -0
- package/dist/ui/Skills.js +148 -0
- package/dist/ui/StackSelector.d.ts +2 -2
- package/dist/ui/StackSelector.js +26 -16
- package/dist/ui/Summary.d.ts +3 -3
- package/dist/ui/Summary.js +60 -50
- package/dist/ui/Welcome.d.ts +1 -1
- package/dist/ui/Welcome.js +15 -16
- package/dist/ui/theme.d.ts +1 -1
- package/dist/ui/theme.js +6 -6
- package/package.json +9 -6
- package/templates/common/atlassian/mcp-atlassian-snippet.json +16 -0
- package/templates/common/repoforge/mcp-repoforge-snippet.json +11 -0
- package/templates/common/repoforge/repoforge.yaml +34 -0
- package/templates/github/deploy-docker-zero-downtime.yml +140 -0
- package/templates/github/repoforge-graph.yml +45 -0
- package/templates/gitlab/deploy-docker-zero-downtime.yml +57 -0
- package/templates/local-ai/.env.example +17 -0
- package/templates/local-ai/docker-compose.yml +95 -0
- package/templates/security-hooks/claude-settings-security.json +30 -0
- package/templates/security-hooks/commit-msg-signing +29 -0
- package/templates/security-hooks/pre-commit-permissions +74 -0
- package/templates/security-hooks/pre-commit-secrets +74 -0
- package/templates/security-hooks/pre-push-branch-protection +62 -0
- package/templates/security-hooks/pre-push-deps +83 -0
- package/templates/security-hooks/pre-push-signing +67 -0
- package/templates/woodpecker/deploy-docker-zero-downtime.yml +50 -0
- package/templates/workflows/ci-pipeline.dot +15 -0
- package/templates/workflows/feature-flow.dot +21 -0
- package/templates/workflows/release.dot +16 -0
- package/dist/__integration__/helpers.d.ts +0 -20
- package/dist/__integration__/helpers.d.ts.map +0 -1
- package/dist/__integration__/helpers.js +0 -31
- package/dist/__integration__/helpers.js.map +0 -1
- package/dist/commands/analyze.d.ts.map +0 -1
- package/dist/commands/analyze.js.map +0 -1
- package/dist/commands/ci.d.ts.map +0 -1
- package/dist/commands/ci.js.map +0 -1
- package/dist/commands/doctor.d.ts.map +0 -1
- package/dist/commands/doctor.js.map +0 -1
- package/dist/commands/init.d.ts.map +0 -1
- package/dist/commands/init.js.map +0 -1
- package/dist/commands/llmstxt.d.ts.map +0 -1
- package/dist/commands/llmstxt.js.map +0 -1
- package/dist/commands/plugin.d.ts.map +0 -1
- package/dist/commands/plugin.js.map +0 -1
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/common.d.ts.map +0 -1
- package/dist/lib/common.js.map +0 -1
- package/dist/lib/docker.d.ts.map +0 -1
- package/dist/lib/docker.js.map +0 -1
- package/dist/lib/frontmatter.d.ts.map +0 -1
- package/dist/lib/frontmatter.js.map +0 -1
- package/dist/lib/plugin.d.ts.map +0 -1
- package/dist/lib/plugin.js.map +0 -1
- package/dist/lib/template.d.ts.map +0 -1
- package/dist/lib/template.js.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js.map +0 -1
- package/dist/ui/AnalyzeUI.d.ts.map +0 -1
- package/dist/ui/AnalyzeUI.js.map +0 -1
- package/dist/ui/App.d.ts.map +0 -1
- package/dist/ui/App.js.map +0 -1
- package/dist/ui/CI.d.ts.map +0 -1
- package/dist/ui/CI.js.map +0 -1
- package/dist/ui/CIContext.d.ts.map +0 -1
- package/dist/ui/CIContext.js.map +0 -1
- package/dist/ui/CISelector.d.ts.map +0 -1
- package/dist/ui/CISelector.js.map +0 -1
- package/dist/ui/Doctor.d.ts.map +0 -1
- package/dist/ui/Doctor.js.map +0 -1
- package/dist/ui/Header.d.ts.map +0 -1
- package/dist/ui/Header.js.map +0 -1
- package/dist/ui/LlmsTxt.d.ts.map +0 -1
- package/dist/ui/LlmsTxt.js.map +0 -1
- package/dist/ui/MemorySelector.d.ts.map +0 -1
- package/dist/ui/MemorySelector.js.map +0 -1
- package/dist/ui/NameInput.d.ts.map +0 -1
- package/dist/ui/NameInput.js.map +0 -1
- package/dist/ui/OptionSelector.d.ts.map +0 -1
- package/dist/ui/OptionSelector.js.map +0 -1
- package/dist/ui/Plugin.d.ts.map +0 -1
- package/dist/ui/Plugin.js.map +0 -1
- package/dist/ui/Progress.d.ts.map +0 -1
- package/dist/ui/Progress.js.map +0 -1
- package/dist/ui/StackSelector.d.ts.map +0 -1
- package/dist/ui/StackSelector.js.map +0 -1
- package/dist/ui/Summary.d.ts.map +0 -1
- package/dist/ui/Summary.js.map +0 -1
- package/dist/ui/Welcome.d.ts.map +0 -1
- package/dist/ui/Welcome.js.map +0 -1
- package/dist/ui/theme.d.ts.map +0 -1
- package/dist/ui/theme.js.map +0 -1
|
@@ -0,0 +1,895 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { parseFrontmatter } from "../lib/frontmatter.js";
|
|
4
|
+
// ── Constants ────────────────────────────────────────────────────────────────
|
|
5
|
+
const DEFAULT_SKILLS_DIR = path.join(process.env["HOME"] ?? "~", ".claude", "skills");
|
|
6
|
+
const DEFAULT_BUDGET = 8000;
|
|
7
|
+
/** Approximate tokens per character (GPT/Claude rough average) */
|
|
8
|
+
const CHARS_PER_TOKEN = 4;
|
|
9
|
+
// ── Contradiction keywords (pairs that signal opposite intent) ───────────────
|
|
10
|
+
const CONTRADICTION_PAIRS = [
|
|
11
|
+
[/\buse semicolons\b/i, /\bno semicolons\b/i],
|
|
12
|
+
[/\bsemicolons required\b/i, /\bno semicolons\b/i],
|
|
13
|
+
[/\bsingle quotes\b/i, /\bdouble quotes\b/i],
|
|
14
|
+
[/\btabs\b/i, /\bspaces\b/i],
|
|
15
|
+
[/\b2[- ]?spaces?\b/i, /\b4[- ]?spaces?\b/i],
|
|
16
|
+
[/\bclass[- ]?based\b/i, /\bfunctional\b/i],
|
|
17
|
+
[/\bOOP\b/i, /\bfunctional\b/i],
|
|
18
|
+
[/\bmutable\b/i, /\bimmutable\b/i],
|
|
19
|
+
[/\bany\b.*\ballowed\b/i, /\bno any\b/i],
|
|
20
|
+
[/\bdefault export\b/i, /\bnamed export\b/i],
|
|
21
|
+
[/\bnever use\b/i, /\balways use\b/i],
|
|
22
|
+
[/\bavoid\b/i, /\bprefer\b/i],
|
|
23
|
+
[/\bdo not\b/i, /\bmust\b/i],
|
|
24
|
+
];
|
|
25
|
+
/**
|
|
26
|
+
* Positive and negative signal patterns.
|
|
27
|
+
* Order matters — first match wins, so more specific patterns go first.
|
|
28
|
+
*/
|
|
29
|
+
const POSITIVE_SIGNALS = [
|
|
30
|
+
/\balways use\b/i,
|
|
31
|
+
/\bmust use\b/i,
|
|
32
|
+
/\bprefer\b/i,
|
|
33
|
+
/\balways\b/i,
|
|
34
|
+
/\bmust\b/i,
|
|
35
|
+
/\brequire\b/i,
|
|
36
|
+
/\buse\b/i,
|
|
37
|
+
/\benable\b/i,
|
|
38
|
+
/\bshould\b/i,
|
|
39
|
+
];
|
|
40
|
+
const NEGATIVE_SIGNALS = [
|
|
41
|
+
/\bnever use\b/i,
|
|
42
|
+
/\bdo not use\b/i,
|
|
43
|
+
/\bdon't use\b/i,
|
|
44
|
+
/\bnever\b/i,
|
|
45
|
+
/\bavoid\b/i,
|
|
46
|
+
/\bdo not\b/i,
|
|
47
|
+
/\bdon't\b/i,
|
|
48
|
+
/\bdisable\b/i,
|
|
49
|
+
/\bno\b/i,
|
|
50
|
+
/\bforbid\b/i,
|
|
51
|
+
];
|
|
52
|
+
/**
|
|
53
|
+
* Extract a directive (sentiment + subject) from a rule string.
|
|
54
|
+
* Returns null if the rule has no clear directive.
|
|
55
|
+
*/
|
|
56
|
+
export function extractDirective(rule) {
|
|
57
|
+
const norm = rule.toLowerCase().trim();
|
|
58
|
+
// Try negative first (more specific: "never use X" before "use X")
|
|
59
|
+
for (const pattern of NEGATIVE_SIGNALS) {
|
|
60
|
+
const match = norm.match(pattern);
|
|
61
|
+
if (match) {
|
|
62
|
+
const subject = norm
|
|
63
|
+
.slice(match.index + match[0].length)
|
|
64
|
+
.trim()
|
|
65
|
+
.replace(/^(the|a|an)\s+/i, "")
|
|
66
|
+
.replace(/[.;,!]+$/, "")
|
|
67
|
+
.trim();
|
|
68
|
+
if (subject.length >= 3) {
|
|
69
|
+
return { sentiment: "negative", subject };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
for (const pattern of POSITIVE_SIGNALS) {
|
|
74
|
+
const match = norm.match(pattern);
|
|
75
|
+
if (match) {
|
|
76
|
+
const subject = norm
|
|
77
|
+
.slice(match.index + match[0].length)
|
|
78
|
+
.trim()
|
|
79
|
+
.replace(/^(the|a|an)\s+/i, "")
|
|
80
|
+
.replace(/[.;,!]+$/, "")
|
|
81
|
+
.trim();
|
|
82
|
+
if (subject.length >= 3) {
|
|
83
|
+
return { sentiment: "positive", subject };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Check if two subjects are similar enough to be "about the same thing".
|
|
91
|
+
* Uses simple word-overlap (Jaccard-like) — no external NLP needed.
|
|
92
|
+
*/
|
|
93
|
+
export function subjectsSimilar(a, b, threshold = 0.5) {
|
|
94
|
+
const wordsA = new Set(a.split(/\s+/).filter((w) => w.length > 2));
|
|
95
|
+
const wordsB = new Set(b.split(/\s+/).filter((w) => w.length > 2));
|
|
96
|
+
if (wordsA.size === 0 || wordsB.size === 0)
|
|
97
|
+
return false;
|
|
98
|
+
let intersection = 0;
|
|
99
|
+
for (const w of wordsA) {
|
|
100
|
+
if (wordsB.has(w))
|
|
101
|
+
intersection++;
|
|
102
|
+
}
|
|
103
|
+
const union = new Set([...wordsA, ...wordsB]).size;
|
|
104
|
+
return union > 0 && intersection / union >= threshold;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Detect a directive clash between two rules:
|
|
108
|
+
* opposite sentiments about the same subject.
|
|
109
|
+
*/
|
|
110
|
+
export function detectDirectiveClash(ruleA, ruleB) {
|
|
111
|
+
const dA = extractDirective(ruleA);
|
|
112
|
+
const dB = extractDirective(ruleB);
|
|
113
|
+
if (!dA || !dB)
|
|
114
|
+
return null;
|
|
115
|
+
if (dA.sentiment === dB.sentiment)
|
|
116
|
+
return null;
|
|
117
|
+
if (subjectsSimilar(dA.subject, dB.subject)) {
|
|
118
|
+
const posRule = dA.sentiment === "positive" ? ruleA : ruleB;
|
|
119
|
+
const negRule = dA.sentiment === "negative" ? ruleA : ruleB;
|
|
120
|
+
return `Directive clash on "${dA.subject}": positive="${posRule.slice(0, 50)}" vs negative="${negRule.slice(0, 50)}"`;
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
// ── Budget Optimization ─────────────────────────────────────────────────────
|
|
125
|
+
/**
|
|
126
|
+
* Generate minimal disable sets to bring total tokens under budget.
|
|
127
|
+
* Uses a greedy approach: disable largest skills first until under budget.
|
|
128
|
+
* Returns up to 3 alternative optimization suggestions.
|
|
129
|
+
*/
|
|
130
|
+
export function generateBudgetOptimizations(entries, totalTokens, budget) {
|
|
131
|
+
if (totalTokens <= budget)
|
|
132
|
+
return [];
|
|
133
|
+
const excess = totalTokens - budget;
|
|
134
|
+
const suggestions = [];
|
|
135
|
+
// Strategy 1: Greedy — disable largest skills first
|
|
136
|
+
const greedy = greedyDisableSet(entries, excess);
|
|
137
|
+
suggestions.push(makeSuggestion(greedy, entries, totalTokens, budget));
|
|
138
|
+
// Strategy 2: Minimal count — find smallest number of skills to disable
|
|
139
|
+
// (try single-skill solutions first, then pairs)
|
|
140
|
+
const singles = entries.filter((e) => e.tokens >= excess);
|
|
141
|
+
if (singles.length > 0) {
|
|
142
|
+
// Pick the smallest single that still meets budget
|
|
143
|
+
const sorted = [...singles].sort((a, b) => a.tokens - b.tokens);
|
|
144
|
+
const minimal = sorted[0];
|
|
145
|
+
if (minimal.skillName !== greedy[0]?.skillName) {
|
|
146
|
+
suggestions.push(makeSuggestion([minimal], entries, totalTokens, budget));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// Strategy 3: If we have many small skills, show a "trim many" approach
|
|
150
|
+
// Disable the bottom 50% by token count (many small skills)
|
|
151
|
+
if (entries.length >= 4) {
|
|
152
|
+
const sortedAsc = [...entries].sort((a, b) => a.tokens - b.tokens);
|
|
153
|
+
const halfCount = Math.ceil(sortedAsc.length / 2);
|
|
154
|
+
const bottomHalf = sortedAsc.slice(0, halfCount);
|
|
155
|
+
const saved = bottomHalf.reduce((s, e) => s + e.tokens, 0);
|
|
156
|
+
if (saved >= excess) {
|
|
157
|
+
const trimSet = greedyDisableSet([...bottomHalf].sort((a, b) => b.tokens - a.tokens), excess);
|
|
158
|
+
const names = new Set(trimSet.map((e) => e.skillName));
|
|
159
|
+
const alreadySuggested = suggestions.some((s) => s.disableSkills.length === names.size &&
|
|
160
|
+
s.disableSkills.every((n) => names.has(n)));
|
|
161
|
+
if (!alreadySuggested) {
|
|
162
|
+
suggestions.push(makeSuggestion(trimSet, entries, totalTokens, budget));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return suggestions;
|
|
167
|
+
}
|
|
168
|
+
/** Greedy: pick largest entries until saved >= excess */
|
|
169
|
+
function greedyDisableSet(entries, excess) {
|
|
170
|
+
const sorted = [...entries].sort((a, b) => b.tokens - a.tokens);
|
|
171
|
+
const result = [];
|
|
172
|
+
let saved = 0;
|
|
173
|
+
for (const entry of sorted) {
|
|
174
|
+
if (saved >= excess)
|
|
175
|
+
break;
|
|
176
|
+
result.push(entry);
|
|
177
|
+
saved += entry.tokens;
|
|
178
|
+
}
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
function makeSuggestion(disableSet, _allEntries, totalTokens, budget) {
|
|
182
|
+
const tokensSaved = disableSet.reduce((s, e) => s + e.tokens, 0);
|
|
183
|
+
const remaining = totalTokens - tokensSaved;
|
|
184
|
+
return {
|
|
185
|
+
disableSkills: disableSet.map((e) => e.skillName),
|
|
186
|
+
tokensSaved,
|
|
187
|
+
remainingTokens: remaining,
|
|
188
|
+
meetsbudget: remaining <= budget,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
192
|
+
/** Estimate token count from a string */
|
|
193
|
+
export function estimateTokens(text) {
|
|
194
|
+
return Math.ceil(text.length / CHARS_PER_TOKEN);
|
|
195
|
+
}
|
|
196
|
+
/** Read a SKILL.md and extract its name + critical rules section */
|
|
197
|
+
export async function parseSkillFile(skillPath) {
|
|
198
|
+
if (!(await fs.pathExists(skillPath)))
|
|
199
|
+
return null;
|
|
200
|
+
const raw = await fs.readFile(skillPath, "utf-8");
|
|
201
|
+
const fm = parseFrontmatter(raw);
|
|
202
|
+
const name = fm?.data?.["name"] ?? path.basename(path.dirname(skillPath));
|
|
203
|
+
// Extract critical rules — look for "Critical Rules" or numbered list after it
|
|
204
|
+
const rules = extractCriticalRules(fm?.content ?? raw);
|
|
205
|
+
// Extract trigger keywords from description
|
|
206
|
+
const description = fm?.data?.["description"] ?? "";
|
|
207
|
+
const triggers = extractTriggers(description);
|
|
208
|
+
return { name, rules, rawContent: raw, triggers };
|
|
209
|
+
}
|
|
210
|
+
/** Extract critical rules from markdown content */
|
|
211
|
+
export function extractCriticalRules(content) {
|
|
212
|
+
const rules = [];
|
|
213
|
+
// Strategy 1: Find "Critical Rules" or "## Critical Rules" section
|
|
214
|
+
const block1 = extractSection(content, /Critical Rules?/i);
|
|
215
|
+
if (block1) {
|
|
216
|
+
extractListItems(block1, rules);
|
|
217
|
+
}
|
|
218
|
+
// Strategy 2: If no critical rules section, look for rules/conventions in any section
|
|
219
|
+
if (rules.length === 0) {
|
|
220
|
+
const block2 = extractSection(content, /Rules?/i);
|
|
221
|
+
if (block2) {
|
|
222
|
+
extractListItems(block2, rules);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return rules;
|
|
226
|
+
}
|
|
227
|
+
/** Extract a markdown section body by heading pattern */
|
|
228
|
+
function extractSection(content, headingPattern) {
|
|
229
|
+
const lines = content.split("\n");
|
|
230
|
+
let capturing = false;
|
|
231
|
+
const blockLines = [];
|
|
232
|
+
for (const line of lines) {
|
|
233
|
+
if (capturing) {
|
|
234
|
+
// Stop at next heading
|
|
235
|
+
if (/^#+\s/.test(line) || /^---/.test(line))
|
|
236
|
+
break;
|
|
237
|
+
blockLines.push(line);
|
|
238
|
+
}
|
|
239
|
+
else if (/^#+\s/.test(line) && headingPattern.test(line)) {
|
|
240
|
+
capturing = true;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return blockLines.length > 0 ? blockLines.join("\n") : null;
|
|
244
|
+
}
|
|
245
|
+
/** Extract numbered or bulleted list items from a markdown block */
|
|
246
|
+
function extractListItems(block, out) {
|
|
247
|
+
const lines = block.split("\n");
|
|
248
|
+
for (const line of lines) {
|
|
249
|
+
const match = line.match(/^\s*(?:\d+[.)]\s+|-\s+|\*\s+)(.+)/);
|
|
250
|
+
if (match) {
|
|
251
|
+
const cleaned = match[1].trim();
|
|
252
|
+
if (cleaned.length > 5)
|
|
253
|
+
out.push(cleaned);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/** Extract trigger keywords from a skill description */
|
|
258
|
+
export function extractTriggers(description) {
|
|
259
|
+
const triggerMatch = description.match(/Trigger:\s*(.+)/i);
|
|
260
|
+
if (!triggerMatch)
|
|
261
|
+
return [];
|
|
262
|
+
const triggerText = triggerMatch[1];
|
|
263
|
+
// Split on commas, "or", "and", common delimiters
|
|
264
|
+
const keywords = triggerText
|
|
265
|
+
.split(/[,;]|\bor\b/i)
|
|
266
|
+
.map((k) => k
|
|
267
|
+
.trim()
|
|
268
|
+
.toLowerCase()
|
|
269
|
+
.replace(/^when\s+/i, ""))
|
|
270
|
+
.filter((k) => k.length > 2);
|
|
271
|
+
return keywords;
|
|
272
|
+
}
|
|
273
|
+
// ── Core: Scan installed skills ─────────────────────────────────────────────
|
|
274
|
+
/** Discover all SKILL.md files in a skills directory */
|
|
275
|
+
export async function discoverSkills(skillsDir) {
|
|
276
|
+
if (!(await fs.pathExists(skillsDir)))
|
|
277
|
+
return [];
|
|
278
|
+
const entries = await fs.readdir(skillsDir);
|
|
279
|
+
const skillFiles = [];
|
|
280
|
+
for (const entry of entries) {
|
|
281
|
+
if (entry.startsWith(".") || entry.startsWith("_"))
|
|
282
|
+
continue;
|
|
283
|
+
const skillPath = path.join(skillsDir, entry, "SKILL.md");
|
|
284
|
+
if (await fs.pathExists(skillPath)) {
|
|
285
|
+
skillFiles.push(skillPath);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return skillFiles.sort();
|
|
289
|
+
}
|
|
290
|
+
// ── Conflict Detection ──────────────────────────────────────────────────────
|
|
291
|
+
/** Check if two rules contradict each other (regex pairs + directive clash) */
|
|
292
|
+
export function detectRuleConflict(ruleA, ruleB) {
|
|
293
|
+
const normA = ruleA.toLowerCase().trim();
|
|
294
|
+
const normB = ruleB.toLowerCase().trim();
|
|
295
|
+
// Strategy 1: Hardcoded regex pairs (fast, high confidence)
|
|
296
|
+
for (const [patternA, patternB] of CONTRADICTION_PAIRS) {
|
|
297
|
+
if ((patternA.test(normA) && patternB.test(normB)) ||
|
|
298
|
+
(patternB.test(normA) && patternA.test(normB))) {
|
|
299
|
+
return {
|
|
300
|
+
reason: `"${ruleA.slice(0, 60)}" vs "${ruleB.slice(0, 60)}"`,
|
|
301
|
+
kind: "regex-pair",
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
// Strategy 2: Semantic directive clash (broader, medium confidence)
|
|
306
|
+
const clashReason = detectDirectiveClash(ruleA, ruleB);
|
|
307
|
+
if (clashReason) {
|
|
308
|
+
return { reason: clashReason, kind: "directive-clash" };
|
|
309
|
+
}
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
/** Scan all skills for conflicting critical rules */
|
|
313
|
+
export async function findConflicts(skillsDir) {
|
|
314
|
+
const skillPaths = await discoverSkills(skillsDir);
|
|
315
|
+
const allRules = [];
|
|
316
|
+
for (const sp of skillPaths) {
|
|
317
|
+
const parsed = await parseSkillFile(sp);
|
|
318
|
+
if (!parsed)
|
|
319
|
+
continue;
|
|
320
|
+
for (const rule of parsed.rules) {
|
|
321
|
+
allRules.push({
|
|
322
|
+
skillName: parsed.name,
|
|
323
|
+
skillPath: sp,
|
|
324
|
+
rule,
|
|
325
|
+
normalized: rule.toLowerCase().trim(),
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
const conflicts = [];
|
|
330
|
+
// Compare every pair (O(n^2) but skill count is small ~20-50)
|
|
331
|
+
for (let i = 0; i < allRules.length; i++) {
|
|
332
|
+
for (let j = i + 1; j < allRules.length; j++) {
|
|
333
|
+
const a = allRules[i];
|
|
334
|
+
const b = allRules[j];
|
|
335
|
+
// Skip rules from the same skill
|
|
336
|
+
if (a.skillName === b.skillName)
|
|
337
|
+
continue;
|
|
338
|
+
const result = detectRuleConflict(a.rule, b.rule);
|
|
339
|
+
if (result) {
|
|
340
|
+
conflicts.push({
|
|
341
|
+
ruleA: a,
|
|
342
|
+
ruleB: b,
|
|
343
|
+
reason: result.reason,
|
|
344
|
+
kind: result.kind,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return conflicts;
|
|
350
|
+
}
|
|
351
|
+
// ── Context Budget ──────────────────────────────────────────────────────────
|
|
352
|
+
/** Calculate token budget for all installed skills */
|
|
353
|
+
export async function calculateBudget(skillsDir, budget = DEFAULT_BUDGET) {
|
|
354
|
+
const skillPaths = await discoverSkills(skillsDir);
|
|
355
|
+
const entries = [];
|
|
356
|
+
for (const sp of skillPaths) {
|
|
357
|
+
const parsed = await parseSkillFile(sp);
|
|
358
|
+
if (!parsed)
|
|
359
|
+
continue;
|
|
360
|
+
entries.push({
|
|
361
|
+
skillName: parsed.name,
|
|
362
|
+
skillPath: sp,
|
|
363
|
+
tokens: estimateTokens(parsed.rawContent),
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
// Sort by token count descending (biggest consumers first)
|
|
367
|
+
entries.sort((a, b) => b.tokens - a.tokens);
|
|
368
|
+
const totalTokens = entries.reduce((sum, e) => sum + e.tokens, 0);
|
|
369
|
+
const overBudget = totalTokens > budget;
|
|
370
|
+
const suggestions = [];
|
|
371
|
+
if (overBudget) {
|
|
372
|
+
const excess = totalTokens - budget;
|
|
373
|
+
suggestions.push(`Over budget by ~${excess} tokens`);
|
|
374
|
+
// Suggest disabling the largest skills until under budget
|
|
375
|
+
let saved = 0;
|
|
376
|
+
for (const entry of entries) {
|
|
377
|
+
if (saved >= excess)
|
|
378
|
+
break;
|
|
379
|
+
suggestions.push(`Consider disabling "${entry.skillName}" (~${entry.tokens} tokens)`);
|
|
380
|
+
saved += entry.tokens;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
const optimizations = generateBudgetOptimizations(entries, totalTokens, budget);
|
|
384
|
+
return {
|
|
385
|
+
entries,
|
|
386
|
+
totalTokens,
|
|
387
|
+
budget,
|
|
388
|
+
overBudget,
|
|
389
|
+
suggestions,
|
|
390
|
+
optimizations,
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
// ── Duplicate Detection ─────────────────────────────────────────────────────
|
|
394
|
+
/** Find skills that overlap in scope/triggers */
|
|
395
|
+
export async function findDuplicates(skillsDir) {
|
|
396
|
+
const skillPaths = await discoverSkills(skillsDir);
|
|
397
|
+
const skillData = [];
|
|
398
|
+
for (const sp of skillPaths) {
|
|
399
|
+
const parsed = await parseSkillFile(sp);
|
|
400
|
+
if (!parsed || parsed.triggers.length === 0)
|
|
401
|
+
continue;
|
|
402
|
+
skillData.push({ name: parsed.name, triggers: parsed.triggers });
|
|
403
|
+
}
|
|
404
|
+
const duplicates = [];
|
|
405
|
+
for (let i = 0; i < skillData.length; i++) {
|
|
406
|
+
for (let j = i + 1; j < skillData.length; j++) {
|
|
407
|
+
const a = skillData[i];
|
|
408
|
+
const b = skillData[j];
|
|
409
|
+
const sharedTriggers = a.triggers.filter((t) => b.triggers.some((bt) => bt.includes(t) || t.includes(bt)));
|
|
410
|
+
if (sharedTriggers.length === 0)
|
|
411
|
+
continue;
|
|
412
|
+
const maxTriggers = Math.max(a.triggers.length, b.triggers.length);
|
|
413
|
+
const similarity = maxTriggers > 0
|
|
414
|
+
? Math.round((sharedTriggers.length / maxTriggers) * 100)
|
|
415
|
+
: 0;
|
|
416
|
+
if (similarity >= 30) {
|
|
417
|
+
duplicates.push({
|
|
418
|
+
skillA: a.name,
|
|
419
|
+
skillB: b.name,
|
|
420
|
+
sharedTriggers,
|
|
421
|
+
similarity,
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
// Sort by similarity descending
|
|
427
|
+
duplicates.sort((a, b) => b.similarity - a.similarity);
|
|
428
|
+
return duplicates;
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Run the skills doctor analysis.
|
|
432
|
+
* - `doctor --deep`: full conflict + budget + duplicate analysis
|
|
433
|
+
* - `budget -b N`: budget-only analysis with custom token limit
|
|
434
|
+
*/
|
|
435
|
+
export async function runSkillsDoctor(options) {
|
|
436
|
+
const skillsDir = options.skillsDir ?? DEFAULT_SKILLS_DIR;
|
|
437
|
+
const budget = options.budget ?? DEFAULT_BUDGET;
|
|
438
|
+
if (options.mode === "budget") {
|
|
439
|
+
const budgetResult = await calculateBudget(skillsDir, budget);
|
|
440
|
+
return { conflicts: [], budget: budgetResult, duplicates: [] };
|
|
441
|
+
}
|
|
442
|
+
// Deep doctor mode
|
|
443
|
+
const [conflicts, budgetResult, duplicates] = await Promise.all([
|
|
444
|
+
options.deep ? findConflicts(skillsDir) : Promise.resolve([]),
|
|
445
|
+
calculateBudget(skillsDir, budget),
|
|
446
|
+
options.deep ? findDuplicates(skillsDir) : Promise.resolve([]),
|
|
447
|
+
]);
|
|
448
|
+
return { conflicts, budget: budgetResult, duplicates };
|
|
449
|
+
}
|
|
450
|
+
// ── Quality Scoring ────────────────────────────────────────────────────────
|
|
451
|
+
const DEFAULT_THRESHOLD = 50;
|
|
452
|
+
/** Vague terms that reduce clarity score */
|
|
453
|
+
const VAGUE_TERMS = [
|
|
454
|
+
/\bstuff\b/i,
|
|
455
|
+
/\bthings?\b/i,
|
|
456
|
+
/\betc\.?\b/i,
|
|
457
|
+
/\bmisc\b/i,
|
|
458
|
+
/\bvarious\b/i,
|
|
459
|
+
/\bsome\b/i,
|
|
460
|
+
/\bmaybe\b/i,
|
|
461
|
+
/\bprobably\b/i,
|
|
462
|
+
];
|
|
463
|
+
/** Action verbs that indicate actionable rules */
|
|
464
|
+
const ACTION_VERBS = [
|
|
465
|
+
/\buse\b/i,
|
|
466
|
+
/\bavoid\b/i,
|
|
467
|
+
/\bprefer\b/i,
|
|
468
|
+
/\bnever\b/i,
|
|
469
|
+
/\balways\b/i,
|
|
470
|
+
/\bmust\b/i,
|
|
471
|
+
/\bshould\b/i,
|
|
472
|
+
/\bshall\b/i,
|
|
473
|
+
/\bensure\b/i,
|
|
474
|
+
/\bwrite\b/i,
|
|
475
|
+
/\bcreate\b/i,
|
|
476
|
+
/\bfollow\b/i,
|
|
477
|
+
/\bdo not\b/i,
|
|
478
|
+
/\bapply\b/i,
|
|
479
|
+
/\bimplement\b/i,
|
|
480
|
+
/\brun\b/i,
|
|
481
|
+
];
|
|
482
|
+
/** Dangerous patterns in skill content that indicate safety risks */
|
|
483
|
+
const DANGEROUS_PATTERNS = [
|
|
484
|
+
{ pattern: /\beval\s*\(/i, label: "eval() usage", weight: 20 },
|
|
485
|
+
{ pattern: /\bexec\s*\(/i, label: "exec() usage", weight: 15 },
|
|
486
|
+
{
|
|
487
|
+
pattern: /\bchild_process\b/i,
|
|
488
|
+
label: "child_process reference",
|
|
489
|
+
weight: 10,
|
|
490
|
+
},
|
|
491
|
+
{ pattern: /\brm\s+-rf\b/i, label: "rm -rf command", weight: 20 },
|
|
492
|
+
{
|
|
493
|
+
pattern: /\bcurl\b.*\|\s*(?:sh|bash)\b/i,
|
|
494
|
+
label: "curl piped to shell",
|
|
495
|
+
weight: 25,
|
|
496
|
+
},
|
|
497
|
+
{ pattern: /\bsudo\b/i, label: "sudo usage", weight: 15 },
|
|
498
|
+
{ pattern: /\bchmod\s+777\b/i, label: "chmod 777", weight: 15 },
|
|
499
|
+
{
|
|
500
|
+
pattern: /\b(?:password|secret|token|api_key)\s*[:=]\s*['"][^'"]+['"]/i,
|
|
501
|
+
label: "hardcoded secret",
|
|
502
|
+
weight: 25,
|
|
503
|
+
},
|
|
504
|
+
{
|
|
505
|
+
pattern: /\b__proto__\b|\bconstructor\s*\[/i,
|
|
506
|
+
label: "prototype pollution",
|
|
507
|
+
weight: 20,
|
|
508
|
+
},
|
|
509
|
+
{
|
|
510
|
+
pattern: /\binnerHTML\s*=/i,
|
|
511
|
+
label: "innerHTML assignment (XSS risk)",
|
|
512
|
+
weight: 10,
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
pattern: /\bdangerouslySetInnerHTML\b/i,
|
|
516
|
+
label: "dangerouslySetInnerHTML",
|
|
517
|
+
weight: 10,
|
|
518
|
+
},
|
|
519
|
+
{
|
|
520
|
+
pattern: /\bno[- ]?verify\b.*\bgit\b|\bgit\b.*\bno[- ]?verify\b/i,
|
|
521
|
+
label: "git --no-verify bypass",
|
|
522
|
+
weight: 10,
|
|
523
|
+
},
|
|
524
|
+
{
|
|
525
|
+
pattern: /\bforce[- ]?push\b|\bpush\s+--force\b/i,
|
|
526
|
+
label: "force push instruction",
|
|
527
|
+
weight: 10,
|
|
528
|
+
},
|
|
529
|
+
{
|
|
530
|
+
pattern: /\bdisable.*(?:eslint|typescript|security)\b/i,
|
|
531
|
+
label: "linter/security disable",
|
|
532
|
+
weight: 10,
|
|
533
|
+
},
|
|
534
|
+
];
|
|
535
|
+
/** Default registry quality threshold */
|
|
536
|
+
const DEFAULT_REGISTRY_THRESHOLD = 60;
|
|
537
|
+
/**
|
|
538
|
+
* Score completeness (0-100): frontmatter fields, critical rules, structure.
|
|
539
|
+
*/
|
|
540
|
+
export function scoreCompleteness(parsed) {
|
|
541
|
+
let score = 0;
|
|
542
|
+
const max = 100;
|
|
543
|
+
// Has a name (10 pts)
|
|
544
|
+
if (parsed.name && parsed.name.length > 0)
|
|
545
|
+
score += 10;
|
|
546
|
+
// Has triggers / description with "Trigger:" (15 pts)
|
|
547
|
+
if (parsed.triggers.length > 0)
|
|
548
|
+
score += 15;
|
|
549
|
+
// Has critical rules section (20 pts)
|
|
550
|
+
if (parsed.rules.length > 0)
|
|
551
|
+
score += 20;
|
|
552
|
+
// Number of rules: 1-2 = 10, 3-5 = 20, 6+ = 25
|
|
553
|
+
if (parsed.rules.length >= 6)
|
|
554
|
+
score += 25;
|
|
555
|
+
else if (parsed.rules.length >= 3)
|
|
556
|
+
score += 20;
|
|
557
|
+
else if (parsed.rules.length >= 1)
|
|
558
|
+
score += 10;
|
|
559
|
+
// Has substantial content (>= 200 chars = 10, >= 500 = 20, >= 1000 = 30)
|
|
560
|
+
const len = parsed.rawContent.length;
|
|
561
|
+
if (len >= 1000)
|
|
562
|
+
score += 30;
|
|
563
|
+
else if (len >= 500)
|
|
564
|
+
score += 20;
|
|
565
|
+
else if (len >= 200)
|
|
566
|
+
score += 10;
|
|
567
|
+
return Math.min(score, max);
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Score clarity (0-100): description quality, rule actionability, no vague terms.
|
|
571
|
+
*/
|
|
572
|
+
export function scoreClarity(parsed) {
|
|
573
|
+
let score = 0;
|
|
574
|
+
const max = 100;
|
|
575
|
+
// Trigger description exists and is meaningful (>= 50 chars in raw = 20 pts)
|
|
576
|
+
if (parsed.rawContent.length >= 50)
|
|
577
|
+
score += 20;
|
|
578
|
+
// Rules contain action verbs (up to 40 pts)
|
|
579
|
+
if (parsed.rules.length > 0) {
|
|
580
|
+
const actionableCount = parsed.rules.filter((rule) => ACTION_VERBS.some((verb) => verb.test(rule))).length;
|
|
581
|
+
const ratio = actionableCount / parsed.rules.length;
|
|
582
|
+
score += Math.round(ratio * 40);
|
|
583
|
+
}
|
|
584
|
+
// Penalty for vague terms in rules (-5 each, max -20)
|
|
585
|
+
let penalty = 0;
|
|
586
|
+
for (const rule of parsed.rules) {
|
|
587
|
+
for (const vague of VAGUE_TERMS) {
|
|
588
|
+
if (vague.test(rule)) {
|
|
589
|
+
penalty += 5;
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
score -= Math.min(penalty, 20);
|
|
595
|
+
// Name is descriptive (not single char) (10 pts)
|
|
596
|
+
if (parsed.name.length >= 3)
|
|
597
|
+
score += 10;
|
|
598
|
+
// Has multiple triggers (10 pts for >= 2, 20 for >= 3)
|
|
599
|
+
if (parsed.triggers.length >= 3)
|
|
600
|
+
score += 20;
|
|
601
|
+
else if (parsed.triggers.length >= 2)
|
|
602
|
+
score += 10;
|
|
603
|
+
// Base content score for having structured sections (10 pts)
|
|
604
|
+
if (/^#+\s/m.test(parsed.rawContent))
|
|
605
|
+
score += 10;
|
|
606
|
+
return Math.max(0, Math.min(score, max));
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Score testability (0-100): Given/When/Then scenarios, specific rules.
|
|
610
|
+
*/
|
|
611
|
+
export function scoreTestability(parsed) {
|
|
612
|
+
let score = 0;
|
|
613
|
+
const max = 100;
|
|
614
|
+
// Has Given/When/Then scenarios (40 pts)
|
|
615
|
+
const gwtMatches = parsed.rawContent.match(/\bGIVEN\b.*\bWHEN\b.*\bTHEN\b/gis);
|
|
616
|
+
const gwtCount = gwtMatches?.length ?? 0;
|
|
617
|
+
if (gwtCount >= 3)
|
|
618
|
+
score += 40;
|
|
619
|
+
else if (gwtCount >= 1)
|
|
620
|
+
score += 25;
|
|
621
|
+
// Rules are specific enough (contain file paths, code refs, or patterns)
|
|
622
|
+
const specificRules = parsed.rules.filter((rule) => /[`'"]/.test(rule) ||
|
|
623
|
+
/\.\w+/.test(rule) ||
|
|
624
|
+
/\bfile\b/i.test(rule) ||
|
|
625
|
+
/\bpath\b/i.test(rule)).length;
|
|
626
|
+
if (parsed.rules.length > 0) {
|
|
627
|
+
const specificity = specificRules / parsed.rules.length;
|
|
628
|
+
score += Math.round(specificity * 30);
|
|
629
|
+
}
|
|
630
|
+
// Has examples or code blocks (20 pts)
|
|
631
|
+
const codeBlocks = (parsed.rawContent.match(/```/g) ?? []).length / 2;
|
|
632
|
+
if (codeBlocks >= 2)
|
|
633
|
+
score += 20;
|
|
634
|
+
else if (codeBlocks >= 1)
|
|
635
|
+
score += 10;
|
|
636
|
+
// Has a "Testing" or "Test" section (10 pts)
|
|
637
|
+
if (/^#+\s.*test/im.test(parsed.rawContent))
|
|
638
|
+
score += 10;
|
|
639
|
+
return Math.min(score, max);
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Score token efficiency (0-100): information density (rules per 1000 tokens).
|
|
643
|
+
*/
|
|
644
|
+
export function scoreTokenEfficiency(parsed) {
|
|
645
|
+
const tokens = estimateTokens(parsed.rawContent);
|
|
646
|
+
if (tokens === 0)
|
|
647
|
+
return 0;
|
|
648
|
+
// Rules per 1000 tokens — higher is more efficient
|
|
649
|
+
const rulesPerKToken = (parsed.rules.length / tokens) * 1000;
|
|
650
|
+
// Ideal: 3-8 rules per 1000 tokens
|
|
651
|
+
// < 1 = bloated, > 10 = maybe too terse
|
|
652
|
+
let score;
|
|
653
|
+
if (rulesPerKToken >= 3 && rulesPerKToken <= 8) {
|
|
654
|
+
score = 100;
|
|
655
|
+
}
|
|
656
|
+
else if (rulesPerKToken >= 2) {
|
|
657
|
+
score = 80;
|
|
658
|
+
}
|
|
659
|
+
else if (rulesPerKToken >= 1) {
|
|
660
|
+
score = 60;
|
|
661
|
+
}
|
|
662
|
+
else if (rulesPerKToken > 0) {
|
|
663
|
+
score = 40;
|
|
664
|
+
}
|
|
665
|
+
else {
|
|
666
|
+
score = 10;
|
|
667
|
+
}
|
|
668
|
+
// Bonus for small total size (under 2000 tokens = +0, under 1000 = already great)
|
|
669
|
+
// Penalty for huge skills (> 5000 tokens = -20)
|
|
670
|
+
if (tokens > 5000)
|
|
671
|
+
score -= 20;
|
|
672
|
+
else if (tokens > 3000)
|
|
673
|
+
score -= 10;
|
|
674
|
+
return Math.max(0, Math.min(score, 100));
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* Score safety (0-100): absence of dangerous patterns, injection risks, credential leaks.
|
|
678
|
+
* Starts at 100 and deducts for each dangerous pattern found.
|
|
679
|
+
*/
|
|
680
|
+
export function scoreSafety(parsed) {
|
|
681
|
+
let score = 100;
|
|
682
|
+
for (const { pattern, weight } of DANGEROUS_PATTERNS) {
|
|
683
|
+
if (pattern.test(parsed.rawContent)) {
|
|
684
|
+
score -= weight;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
// Bonus: skill explicitly mentions security best practices (+10, capped at 100)
|
|
688
|
+
if (/\bsanitiz/i.test(parsed.rawContent) ||
|
|
689
|
+
/\bescap/i.test(parsed.rawContent)) {
|
|
690
|
+
score += 10;
|
|
691
|
+
}
|
|
692
|
+
if (/\bvalidat/i.test(parsed.rawContent)) {
|
|
693
|
+
score += 5;
|
|
694
|
+
}
|
|
695
|
+
return Math.max(0, Math.min(score, 100));
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* Score agent readiness (0-100): how well-prepared a skill is for AI agent consumption.
|
|
699
|
+
* Checks for triggers, tool restrictions, examples, structured output, and error handling.
|
|
700
|
+
*/
|
|
701
|
+
export function scoreAgentReadiness(parsed) {
|
|
702
|
+
let score = 0;
|
|
703
|
+
// Has triggers for auto-activation (25 pts)
|
|
704
|
+
if (parsed.triggers.length >= 3)
|
|
705
|
+
score += 25;
|
|
706
|
+
else if (parsed.triggers.length >= 1)
|
|
707
|
+
score += 15;
|
|
708
|
+
// Has tool restrictions or permissions (e.g., "only use", "do not use", "allowed tools") (20 pts)
|
|
709
|
+
if (/\b(?:only use|allowed tools?|restricted to|do not use|forbidden|prohibited)\b/i.test(parsed.rawContent)) {
|
|
710
|
+
score += 20;
|
|
711
|
+
}
|
|
712
|
+
// Has examples with expected input/output or code blocks (20 pts)
|
|
713
|
+
const codeBlocks = (parsed.rawContent.match(/```/g) ?? []).length / 2;
|
|
714
|
+
if (codeBlocks >= 3)
|
|
715
|
+
score += 20;
|
|
716
|
+
else if (codeBlocks >= 1)
|
|
717
|
+
score += 10;
|
|
718
|
+
// Has structured output format (JSON, YAML, or explicit format section) (15 pts)
|
|
719
|
+
if (/\boutput format\b/i.test(parsed.rawContent) ||
|
|
720
|
+
/\breturn.*(?:json|yaml|structured)\b/i.test(parsed.rawContent)) {
|
|
721
|
+
score += 15;
|
|
722
|
+
}
|
|
723
|
+
else if (/```(?:json|yaml)/i.test(parsed.rawContent)) {
|
|
724
|
+
score += 10;
|
|
725
|
+
}
|
|
726
|
+
// Has error handling guidance ("if error", "when fails", "fallback") (10 pts)
|
|
727
|
+
if (/\b(?:if.*(?:error|fail)|fallback|edge case|error handling)\b/i.test(parsed.rawContent)) {
|
|
728
|
+
score += 10;
|
|
729
|
+
}
|
|
730
|
+
// Has a clear "when NOT to use" or scope boundary (10 pts)
|
|
731
|
+
if (/\b(?:do not trigger|not applicable|out of scope|when not to)\b/i.test(parsed.rawContent)) {
|
|
732
|
+
score += 10;
|
|
733
|
+
}
|
|
734
|
+
return Math.min(score, 100);
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* Convert numeric score to letter grade.
|
|
738
|
+
*/
|
|
739
|
+
export function computeGrade(overall) {
|
|
740
|
+
if (overall >= 90)
|
|
741
|
+
return "A";
|
|
742
|
+
if (overall >= 80)
|
|
743
|
+
return "B";
|
|
744
|
+
if (overall >= 70)
|
|
745
|
+
return "C";
|
|
746
|
+
if (overall >= 60)
|
|
747
|
+
return "D";
|
|
748
|
+
return "F";
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Score a skill on all 6 dimensions and compute overall with letter grade.
|
|
752
|
+
*/
|
|
753
|
+
export async function scoreSkill(skillPath, threshold = DEFAULT_THRESHOLD) {
|
|
754
|
+
const parsed = await parseSkillFile(skillPath);
|
|
755
|
+
if (!parsed)
|
|
756
|
+
return null;
|
|
757
|
+
const completeness = scoreCompleteness(parsed);
|
|
758
|
+
const clarity = scoreClarity(parsed);
|
|
759
|
+
const testability = scoreTestability(parsed);
|
|
760
|
+
const tokenEfficiency = scoreTokenEfficiency(parsed);
|
|
761
|
+
const safety = scoreSafety(parsed);
|
|
762
|
+
const agentReadiness = scoreAgentReadiness(parsed);
|
|
763
|
+
// Weighted average: completeness 20%, clarity 20%, testability 15%,
|
|
764
|
+
// token-efficiency 15%, safety 15%, agent-readiness 15%
|
|
765
|
+
const overall = Math.round(completeness * 0.2 +
|
|
766
|
+
clarity * 0.2 +
|
|
767
|
+
testability * 0.15 +
|
|
768
|
+
tokenEfficiency * 0.15 +
|
|
769
|
+
safety * 0.15 +
|
|
770
|
+
agentReadiness * 0.15);
|
|
771
|
+
const grade = computeGrade(overall);
|
|
772
|
+
return {
|
|
773
|
+
skillName: parsed.name,
|
|
774
|
+
completeness,
|
|
775
|
+
clarity,
|
|
776
|
+
testability,
|
|
777
|
+
tokenEfficiency,
|
|
778
|
+
safety,
|
|
779
|
+
agentReadiness,
|
|
780
|
+
overall,
|
|
781
|
+
grade,
|
|
782
|
+
threshold,
|
|
783
|
+
passing: overall >= threshold,
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* Gate check for registry inclusion. Rejects skills below the configured threshold.
|
|
788
|
+
*/
|
|
789
|
+
export async function registryGate(skillPath, threshold = DEFAULT_REGISTRY_THRESHOLD) {
|
|
790
|
+
const score = await scoreSkill(skillPath, threshold);
|
|
791
|
+
if (!score)
|
|
792
|
+
return null;
|
|
793
|
+
const accepted = score.passing;
|
|
794
|
+
let reason;
|
|
795
|
+
if (!accepted) {
|
|
796
|
+
const failures = [];
|
|
797
|
+
if (score.safety < 60)
|
|
798
|
+
failures.push(`safety=${score.safety}`);
|
|
799
|
+
if (score.completeness < 40)
|
|
800
|
+
failures.push(`completeness=${score.completeness}`);
|
|
801
|
+
if (score.clarity < 40)
|
|
802
|
+
failures.push(`clarity=${score.clarity}`);
|
|
803
|
+
if (score.agentReadiness < 30)
|
|
804
|
+
failures.push(`agent-readiness=${score.agentReadiness}`);
|
|
805
|
+
reason =
|
|
806
|
+
failures.length > 0
|
|
807
|
+
? `Rejected (${score.grade}, ${score.overall}/100): weak dimensions — ${failures.join(", ")}`
|
|
808
|
+
: `Rejected (${score.grade}, ${score.overall}/100): below threshold ${threshold}`;
|
|
809
|
+
}
|
|
810
|
+
return {
|
|
811
|
+
skillName: score.skillName,
|
|
812
|
+
score,
|
|
813
|
+
accepted,
|
|
814
|
+
reason,
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
// ── Benchmarking ───────────────────────────────────────────────────────────
|
|
818
|
+
/**
|
|
819
|
+
* Run structural quality benchmark checks against a skill.
|
|
820
|
+
*/
|
|
821
|
+
export async function benchmarkSkill(skillPath) {
|
|
822
|
+
const parsed = await parseSkillFile(skillPath);
|
|
823
|
+
if (!parsed)
|
|
824
|
+
return null;
|
|
825
|
+
const checks = [];
|
|
826
|
+
// Check 1: Has YAML frontmatter with name
|
|
827
|
+
checks.push({
|
|
828
|
+
name: "has-frontmatter-name",
|
|
829
|
+
passed: parsed.name.length > 0 &&
|
|
830
|
+
parsed.name !== path.basename(path.dirname(skillPath)),
|
|
831
|
+
detail: parsed.name.length > 0
|
|
832
|
+
? `name: ${parsed.name}`
|
|
833
|
+
: "No explicit name in frontmatter",
|
|
834
|
+
});
|
|
835
|
+
// Check 2: Has trigger keywords
|
|
836
|
+
checks.push({
|
|
837
|
+
name: "has-triggers",
|
|
838
|
+
passed: parsed.triggers.length > 0,
|
|
839
|
+
detail: parsed.triggers.length > 0
|
|
840
|
+
? `${parsed.triggers.length} trigger(s) found`
|
|
841
|
+
: 'No "Trigger:" in description',
|
|
842
|
+
});
|
|
843
|
+
// Check 3: Has critical rules (>= 3)
|
|
844
|
+
checks.push({
|
|
845
|
+
name: "has-critical-rules",
|
|
846
|
+
passed: parsed.rules.length >= 3,
|
|
847
|
+
detail: `${parsed.rules.length} rule(s) found`,
|
|
848
|
+
});
|
|
849
|
+
// Check 4: Rules are actionable (contain verbs)
|
|
850
|
+
const actionableRules = parsed.rules.filter((rule) => ACTION_VERBS.some((verb) => verb.test(rule)));
|
|
851
|
+
checks.push({
|
|
852
|
+
name: "rules-actionable",
|
|
853
|
+
passed: parsed.rules.length > 0 &&
|
|
854
|
+
actionableRules.length / parsed.rules.length >= 0.5,
|
|
855
|
+
detail: `${actionableRules.length}/${parsed.rules.length} rules have action verbs`,
|
|
856
|
+
});
|
|
857
|
+
// Check 5: Has code examples
|
|
858
|
+
const codeBlocks = (parsed.rawContent.match(/```/g) ?? []).length / 2;
|
|
859
|
+
checks.push({
|
|
860
|
+
name: "has-code-examples",
|
|
861
|
+
passed: codeBlocks >= 1,
|
|
862
|
+
detail: `${Math.floor(codeBlocks)} code block(s)`,
|
|
863
|
+
});
|
|
864
|
+
// Check 6: Has structured sections (headings)
|
|
865
|
+
const headings = (parsed.rawContent.match(/^#+\s/gm) ?? []).length;
|
|
866
|
+
checks.push({
|
|
867
|
+
name: "has-sections",
|
|
868
|
+
passed: headings >= 3,
|
|
869
|
+
detail: `${headings} section heading(s)`,
|
|
870
|
+
});
|
|
871
|
+
// Check 7: Token budget reasonable (< 3000 tokens)
|
|
872
|
+
const tokens = estimateTokens(parsed.rawContent);
|
|
873
|
+
checks.push({
|
|
874
|
+
name: "token-budget-ok",
|
|
875
|
+
passed: tokens <= 3000,
|
|
876
|
+
detail: `~${tokens} tokens`,
|
|
877
|
+
});
|
|
878
|
+
// Check 8: No vague terms in rules
|
|
879
|
+
const vagueRules = parsed.rules.filter((rule) => VAGUE_TERMS.some((vague) => vague.test(rule)));
|
|
880
|
+
checks.push({
|
|
881
|
+
name: "no-vague-rules",
|
|
882
|
+
passed: vagueRules.length === 0,
|
|
883
|
+
detail: vagueRules.length > 0
|
|
884
|
+
? `${vagueRules.length} rule(s) contain vague terms`
|
|
885
|
+
: "All rules are specific",
|
|
886
|
+
});
|
|
887
|
+
const passedCount = checks.filter((c) => c.passed).length;
|
|
888
|
+
const passRate = Math.round((passedCount / checks.length) * 100);
|
|
889
|
+
return {
|
|
890
|
+
skillName: parsed.name,
|
|
891
|
+
checks,
|
|
892
|
+
passRate,
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
//# sourceMappingURL=skills.js.map
|