Repository navigation
Expand file tree
/
Copy pathvalidate-agent-specs.js
More file actions
369 lines (322 loc) · 9.73 KB
/
Copy pathvalidate-agent-specs.js
File metadata and controls
369 lines (322 loc) · 9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env node
/**
* Agent Specification Validation Script - Phase 3
*
* Validates cross-references between agent .agent.md spec files and
* their implementation folders, ensuring consistency and completeness.
*/
import fs from "fs";
import path from "path";
import url from "url";
import * as YAML from "js-yaml";
import { resolveImplementationDir } from "./implementation-path.js";
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
// Configuration
const REPO_ROOT = path.join(__dirname, "../..");
const AGENTS_DIR = path.join(REPO_ROOT, "agents");
const SPEC_EXTENSION = ".agent.md";
// Results collection
const results = {
agents: [],
summary: {
totalDirectories: 0,
totalSpecFiles: 0,
specsWithValidReferences: 0,
specsMissingReferences: 0,
directoryMissingSpec: 0,
consistencyIssues: [],
metadataIssues: [],
},
};
/**
* Parse frontmatter from markdown file
*/
function parseFrontmatter(content) {
const match = content.match(/^---\n([\s\S]*?)\n---/);
if (!match) return null;
try {
return YAML.load(match[1]);
} catch {
return null;
}
}
/**
* Normalize agent names for comparison
*/
function normalizeAgentName(name) {
return name
.replace(/^agents\//, "")
.replace(/\.agent\.md$/, "")
.replace(/-agent$/, "")
.toLowerCase()
.replace(/-/g, "_");
}
/**
* Check if a directory exists
*/
function directoryExists(dir) {
try {
return fs.statSync(dir).isDirectory();
} catch {
return false;
}
}
/**
* Check if entry point files exist in implementation directory
*/
function checkEntryPoints(implementationDir) {
const entryPoints = [
"AGENT.md",
"README.md",
"SKILL.md",
"index.js",
"index.ts",
];
const found = [];
for (const entry of entryPoints) {
const fullPath = path.join(implementationDir, entry);
if (fs.existsSync(fullPath)) {
found.push(entry);
}
}
return found;
}
/**
* Validate spec file frontmatter
*/
function validateFrontmatter(frontmatter) {
const issues = [];
const required = [
"name",
"description",
"file_type",
"category",
"status",
"version",
"created_date",
"last_updated",
"author",
"implementation",
];
for (const field of required) {
if (!frontmatter[field]) {
issues.push(`Missing required field: ${field}`);
}
}
// Check date formats
if (frontmatter.created_date && !isValidDate(frontmatter.created_date)) {
issues.push(`Invalid created_date format: ${frontmatter.created_date}`);
}
if (frontmatter.last_updated && !isValidDate(frontmatter.last_updated)) {
issues.push(`Invalid last_updated format: ${frontmatter.last_updated}`);
}
return issues;
}
/**
* Check if string is valid date (YYYY-MM-DD)
*/
function isValidDate(dateStr) {
if (typeof dateStr !== "string") return false;
return /^\d{4}-\d{2}-\d{2}$/.test(dateStr);
}
/**
* Get all agent directories
*/
function getAgentDirectories() {
const entries = fs.readdirSync(AGENTS_DIR);
const dirs = {};
for (const entry of entries) {
const fullPath = path.join(AGENTS_DIR, entry);
if (directoryExists(fullPath) && !entry.startsWith(".")) {
dirs[entry] = fullPath;
}
}
return dirs;
}
/**
* Get all spec files
*/
function getSpecFiles() {
const entries = fs.readdirSync(AGENTS_DIR);
const specs = {};
for (const entry of entries) {
if (entry.endsWith(SPEC_EXTENSION)) {
const name = entry.replace(SPEC_EXTENSION, "");
specs[name] = path.join(AGENTS_DIR, entry);
}
}
return specs;
}
/**
* Main validation function
*/
function validate() {
console.log("Starting Agent Specification Validation...\n");
const directories = getAgentDirectories();
const specs = getSpecFiles();
results.summary.totalDirectories = Object.keys(directories).length;
results.summary.totalSpecFiles = Object.keys(specs).length;
console.log(`Found ${results.summary.totalDirectories} agent directories`);
console.log(`Found ${results.summary.totalSpecFiles} spec files\n`);
const processedSpecs = new Set();
// Process each spec file
console.log("Validating spec files...\n");
for (const [specName, specPath] of Object.entries(specs)) {
processedSpecs.add(specName);
const specContent = fs.readFileSync(specPath, "utf8");
const frontmatter = parseFrontmatter(specContent);
const agentRecord = {
specFile: specName + SPEC_EXTENSION,
specPath: specPath,
implementationPath: null,
implementationExists: false,
entryPoints: [],
frontmatterValid: false,
frontmatterIssues: [],
referenceResolved: false,
};
// Validate frontmatter
if (frontmatter) {
agentRecord.frontmatterValid = true;
agentRecord.frontmatterIssues = validateFrontmatter(frontmatter);
// Check implementation reference
if (frontmatter.implementation) {
const implPath = resolveImplementationDir(
frontmatter.implementation,
REPO_ROOT,
);
if (directoryExists(implPath)) {
agentRecord.implementationPath = frontmatter.implementation;
agentRecord.implementationExists = true;
agentRecord.entryPoints = checkEntryPoints(implPath);
agentRecord.referenceResolved = true;
results.summary.specsWithValidReferences++;
} else {
agentRecord.implementationPath = frontmatter.implementation;
agentRecord.referenceResolved = false;
results.summary.specsMissingReferences++;
}
} else {
results.summary.specsMissingReferences++;
agentRecord.frontmatterIssues.push("Missing implementation field");
}
} else {
agentRecord.frontmatterIssues.push("Could not parse frontmatter");
results.summary.specsMissingReferences++;
}
if (agentRecord.frontmatterIssues.length > 0) {
results.summary.metadataIssues.push({
file: agentRecord.specFile,
issues: agentRecord.frontmatterIssues,
});
}
results.agents.push(agentRecord);
}
// Check for directories without specs
console.log("Checking for directories without spec files...\n");
for (const [dirName, dirPath] of Object.entries(directories)) {
// Try to find matching spec
let foundSpec = false;
// Direct match
if (specs[dirName]) {
foundSpec = true;
} else {
// Try normalized matches
const normalized = normalizeAgentName(dirName);
for (const specName of Object.keys(specs)) {
if (normalizeAgentName(specName) === normalized) {
foundSpec = true;
break;
}
}
}
if (!foundSpec) {
results.summary.directoryMissingSpec++;
results.agents.push({
directory: dirName,
directoryPath: dirPath,
specFile: null,
issue: "No corresponding spec file found",
entryPoints: checkEntryPoints(dirPath),
});
}
}
// Report results
reportResults();
}
/**
* Generate report
*/
function reportResults() {
console.log(
"\n═══════════════════════════════════════════════════════════\n",
);
console.log("VALIDATION SUMMARY");
console.log("═══════════════════════════════════════════════════════════\n");
console.log(`Total Agent Directories: ${results.summary.totalDirectories}`);
console.log(`Total Spec Files: ${results.summary.totalSpecFiles}`);
console.log(
`Specs with Valid References: ${results.summary.specsWithValidReferences}`,
);
console.log(
`Specs Missing References: ${results.summary.specsMissingReferences}`,
);
console.log(
`Directories Missing Specs: ${results.summary.directoryMissingSpec}\n`,
);
// Detailed findings
if (results.summary.specsMissingReferences > 0) {
console.log("\n⚠️ MISSING IMPLEMENTATION REFERENCES:\n");
const missing = results.agents.filter(
(a) => !a.referenceResolved && a.specFile,
);
for (const agent of missing) {
console.log(` • ${agent.specFile}`);
if (agent.frontmatterIssues.length > 0) {
console.log(` Issues: ${agent.frontmatterIssues.join(", ")}`);
}
}
}
if (results.summary.directoryMissingSpec > 0) {
console.log("\n⚠️ DIRECTORIES WITHOUT SPEC FILES:\n");
const missingSpecs = results.agents.filter(
(a) => a.directory && !a.specFile,
);
for (const agent of missingSpecs) {
console.log(` • ${agent.directory}/`);
if (agent.entryPoints.length > 0) {
console.log(` Entry points: ${agent.entryPoints.join(", ")}`);
}
}
}
if (results.summary.metadataIssues.length > 0) {
console.log("\n⚠️ METADATA ISSUES:\n");
for (const issue of results.summary.metadataIssues) {
console.log(` • ${issue.file}`);
for (const problem of issue.issues) {
console.log(` - ${problem}`);
}
}
}
console.log(
"\n═══════════════════════════════════════════════════════════\n",
);
// Output JSON for further processing
const outputPath = path.join(
__dirname,
"../../.github/reports/audit/agent-specs-phase3-validation.json",
);
const outputDir = path.dirname(outputPath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
fs.writeFileSync(outputPath, JSON.stringify(results, null, 2));
console.log(`✅ Validation results saved to: ${outputPath}\n`);
}
// Run validation
try {
validate();
} catch (error) {
console.error("Validation failed:", error);
process.exit(1);
}