@codsy/cli 0.0.0-stage → 1.0.0

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.
Files changed (80) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +227 -2
  3. package/dist/check-screen-OZJD7UHU.js +80 -0
  4. package/dist/chunk-46NW6PYM.js +115 -0
  5. package/dist/chunk-52CMOWJF.js +59 -0
  6. package/dist/chunk-6AVERKMZ.js +17 -0
  7. package/dist/chunk-BJYMGFW2.js +14 -0
  8. package/dist/chunk-FVNSMO6G.js +313 -0
  9. package/dist/chunk-JVEULAOD.js +24 -0
  10. package/dist/chunk-KFHC377Y.js +21 -0
  11. package/dist/chunk-MOIYJ6UU.js +48 -0
  12. package/dist/chunk-TDOJ55BG.js +120 -0
  13. package/dist/chunk-UWXPACZ7.js +54 -0
  14. package/dist/cli.d.ts +5 -0
  15. package/dist/cli.js +2032 -0
  16. package/dist/home-screen-UIUS4UOT.js +206 -0
  17. package/dist/init-wizard-MXAFTYCQ.js +54 -0
  18. package/dist/new-schema-wizard-NYIHF47J.js +123 -0
  19. package/dist/new-skill-wizard-AZJ5DLDM.js +121 -0
  20. package/package.json +66 -4
  21. package/templates/blank/.github/PULL_REQUEST_TEMPLATE.md +7 -0
  22. package/templates/blank/.github/workflows/codsy.yml +33 -0
  23. package/templates/blank/AGENTS.md +8 -0
  24. package/templates/blank/CODEOWNERS +4 -0
  25. package/templates/blank/README.md +12 -0
  26. package/templates/blank/codsy.config.yaml +1 -0
  27. package/templates/blank/context/company.md +4 -0
  28. package/templates/blank/context/customers.md +4 -0
  29. package/templates/blank/context/industry.md +4 -0
  30. package/templates/blank/context/operations.md +4 -0
  31. package/templates/blank/context/products.md +4 -0
  32. package/templates/blank/context/strategy.md +4 -0
  33. package/templates/blank/evals/.gitkeep +0 -0
  34. package/templates/blank/schemas/.gitkeep +0 -0
  35. package/templates/blank/skills/.gitkeep +0 -0
  36. package/templates/finance/.github/PULL_REQUEST_TEMPLATE.md +8 -0
  37. package/templates/finance/.github/workflows/codsy.yml +33 -0
  38. package/templates/finance/AGENTS.md +14 -0
  39. package/templates/finance/CODEOWNERS +4 -0
  40. package/templates/finance/README.md +16 -0
  41. package/templates/finance/codsy.config.yaml +1 -0
  42. package/templates/finance/context/company.md +6 -0
  43. package/templates/finance/context/customers.md +5 -0
  44. package/templates/finance/context/industry.md +5 -0
  45. package/templates/finance/context/operations.md +6 -0
  46. package/templates/finance/context/products.md +4 -0
  47. package/templates/finance/context/strategy.md +4 -0
  48. package/templates/finance/evals/collections-classification/cases.yaml +81 -0
  49. package/templates/finance/schemas/account.schema.json +21 -0
  50. package/templates/finance/skills/collections-classification/SKILL.md +49 -0
  51. package/templates/sales/.github/PULL_REQUEST_TEMPLATE.md +7 -0
  52. package/templates/sales/.github/workflows/codsy.yml +33 -0
  53. package/templates/sales/AGENTS.md +11 -0
  54. package/templates/sales/CODEOWNERS +4 -0
  55. package/templates/sales/README.md +13 -0
  56. package/templates/sales/codsy.config.yaml +1 -0
  57. package/templates/sales/context/company.md +6 -0
  58. package/templates/sales/context/customers.md +5 -0
  59. package/templates/sales/context/industry.md +5 -0
  60. package/templates/sales/context/operations.md +5 -0
  61. package/templates/sales/context/products.md +4 -0
  62. package/templates/sales/context/strategy.md +4 -0
  63. package/templates/sales/evals/lead-qualification/cases.yaml +87 -0
  64. package/templates/sales/schemas/lead.schema.json +26 -0
  65. package/templates/sales/skills/lead-qualification/SKILL.md +46 -0
  66. package/templates/support/.github/PULL_REQUEST_TEMPLATE.md +7 -0
  67. package/templates/support/.github/workflows/codsy.yml +33 -0
  68. package/templates/support/AGENTS.md +11 -0
  69. package/templates/support/CODEOWNERS +4 -0
  70. package/templates/support/README.md +13 -0
  71. package/templates/support/codsy.config.yaml +1 -0
  72. package/templates/support/context/company.md +5 -0
  73. package/templates/support/context/customers.md +5 -0
  74. package/templates/support/context/industry.md +5 -0
  75. package/templates/support/context/operations.md +5 -0
  76. package/templates/support/context/products.md +4 -0
  77. package/templates/support/context/strategy.md +4 -0
  78. package/templates/support/evals/ticket-triage/cases.yaml +81 -0
  79. package/templates/support/schemas/ticket.schema.json +22 -0
  80. package/templates/support/skills/ticket-triage/SKILL.md +46 -0
package/dist/cli.js ADDED
@@ -0,0 +1,2032 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ isRecord,
4
+ listFilesRecursively,
5
+ loadProject,
6
+ readFileIfExists,
7
+ runHomeCommand
8
+ } from "./chunk-FVNSMO6G.js";
9
+ import {
10
+ formatReport
11
+ } from "./chunk-TDOJ55BG.js";
12
+ import {
13
+ buildEvalsStub,
14
+ buildSkillMarkdown
15
+ } from "./chunk-52CMOWJF.js";
16
+ import {
17
+ buildSchemaJson
18
+ } from "./chunk-JVEULAOD.js";
19
+
20
+ // src/cli/index.ts
21
+ import { realpathSync } from "fs";
22
+ import { join as join8 } from "path";
23
+ import { fileURLToPath as fileURLToPath2 } from "url";
24
+
25
+ // package.json
26
+ var package_default = {
27
+ name: "codsy",
28
+ version: "1.0.0",
29
+ description: "Write operational knowledge as versioned files that people and AI agents can both read.",
30
+ type: "module",
31
+ license: "Apache-2.0",
32
+ author: "Codsy Contributors",
33
+ homepage: "https://github.com/faelreis/codsy#readme",
34
+ repository: {
35
+ type: "git",
36
+ url: "git+https://github.com/faelreis/codsy.git"
37
+ },
38
+ bugs: {
39
+ url: "https://github.com/faelreis/codsy/issues"
40
+ },
41
+ keywords: [
42
+ "cli",
43
+ "skills",
44
+ "agent-skills",
45
+ "operational-knowledge",
46
+ "lint",
47
+ "ai-agents"
48
+ ],
49
+ bin: {
50
+ codsy: "./dist/cli.js"
51
+ },
52
+ files: [
53
+ "dist",
54
+ "!dist/**/*.map",
55
+ "templates"
56
+ ],
57
+ engines: {
58
+ node: ">=22.12.0"
59
+ },
60
+ dependencies: {
61
+ ajv: "^8.20.0",
62
+ ink: "^8.0.0",
63
+ "js-yaml": "^5.5.0",
64
+ react: "^19.3.1"
65
+ },
66
+ scripts: {
67
+ build: "tsup",
68
+ dev: "tsup --watch",
69
+ lint: "eslint .",
70
+ "lint:fix": "eslint . --fix",
71
+ format: "prettier --write .",
72
+ "format:check": "prettier --check .",
73
+ typecheck: "tsc --noEmit",
74
+ test: "vitest run",
75
+ "test:watch": "vitest"
76
+ },
77
+ devDependencies: {
78
+ "@eslint/js": "^10.0.1",
79
+ "@types/node": "^22.20.1",
80
+ "@types/react": "^19.3.0",
81
+ eslint: "^10.14.0",
82
+ "eslint-config-prettier": "^10.1.8",
83
+ "ink-testing-library": "^4.0.0",
84
+ prettier: "^3.10.1",
85
+ tsup: "^9.0.0",
86
+ typescript: "5.9.3",
87
+ "typescript-eslint": "^8.72.2",
88
+ vitest: "^5.1.0"
89
+ }
90
+ };
91
+
92
+ // src/core/package-root.ts
93
+ import { existsSync } from "fs";
94
+ import { dirname, join } from "path";
95
+ import { fileURLToPath } from "url";
96
+ var MAX_WALK_UP = 5;
97
+ function findPackageRootFromDir(startDir) {
98
+ let dir = startDir;
99
+ for (let i = 0; i < MAX_WALK_UP; i += 1) {
100
+ if (existsSync(join(dir, "package.json"))) return dir;
101
+ const parent = dirname(dir);
102
+ if (parent === dir) break;
103
+ dir = parent;
104
+ }
105
+ throw new Error(`Unable to locate a package.json above ${startDir}`);
106
+ }
107
+ function findPackageRoot(fromModuleUrl) {
108
+ return findPackageRootFromDir(dirname(fileURLToPath(fromModuleUrl)));
109
+ }
110
+
111
+ // src/cli/init.ts
112
+ import { resolve } from "path";
113
+
114
+ // src/cli/templates/copy-template.ts
115
+ import { mkdir, readFile, writeFile } from "fs/promises";
116
+ import { dirname as dirname2, join as join2 } from "path";
117
+ var TEMPLATE_NAMES = ["blank", "sales", "support", "finance"];
118
+ function isTemplateName(value) {
119
+ return value !== void 0 && TEMPLATE_NAMES.includes(value);
120
+ }
121
+ async function copyTemplate(templatesDir, templateName, destDir) {
122
+ const sourceDir = join2(templatesDir, templateName);
123
+ const relativeFiles = await listFilesRecursively(sourceDir);
124
+ for (const folder of ["schemas", "skills", "context", "evals"]) {
125
+ await mkdir(join2(destDir, folder), { recursive: true });
126
+ }
127
+ for (const relativeFile of relativeFiles) {
128
+ if (relativeFile.endsWith(".gitkeep")) continue;
129
+ const sourcePath = join2(sourceDir, relativeFile);
130
+ const destPath = join2(destDir, relativeFile);
131
+ await mkdir(dirname2(destPath), { recursive: true });
132
+ const content = await readFile(sourcePath);
133
+ await writeFile(destPath, content);
134
+ }
135
+ }
136
+
137
+ // src/cli/init.ts
138
+ function parseInitArgs(argv) {
139
+ let dir = ".";
140
+ let template = null;
141
+ let invalidTemplate = null;
142
+ let dirSet = false;
143
+ for (let i = 0; i < argv.length; i += 1) {
144
+ const arg = argv[i];
145
+ if (arg === "--template") {
146
+ const value = argv[i + 1];
147
+ if (isTemplateName(value)) {
148
+ template = value;
149
+ } else {
150
+ invalidTemplate = value ?? "";
151
+ }
152
+ i += 1;
153
+ } else if (arg !== void 0 && !dirSet) {
154
+ dir = arg;
155
+ dirSet = true;
156
+ }
157
+ }
158
+ return { dir, template, invalidTemplate };
159
+ }
160
+ async function runInit(argv, options) {
161
+ const { dir, template, invalidTemplate } = parseInitArgs(argv);
162
+ if (invalidTemplate !== null) {
163
+ options.write(
164
+ `Unknown template "${invalidTemplate}". Choose one of: blank, sales, support, finance.
165
+ `
166
+ );
167
+ return 1;
168
+ }
169
+ let chosenTemplate = template;
170
+ if (chosenTemplate === null) {
171
+ if (!options.isInteractive || !options.promptForTemplate) {
172
+ options.write(
173
+ "No template was given. Pass --template <blank|sales|support|finance>, or run this command in an interactive terminal.\n"
174
+ );
175
+ return 1;
176
+ }
177
+ chosenTemplate = await options.promptForTemplate();
178
+ if (chosenTemplate === null) {
179
+ options.write("Cancelled. No files were created.\n");
180
+ return 1;
181
+ }
182
+ }
183
+ await copyTemplate(options.templatesDir, chosenTemplate, dir);
184
+ options.write(formatInitSuccess(chosenTemplate, dir, process.cwd()));
185
+ return 0;
186
+ }
187
+ function formatInitSuccess(template, dir, cwd) {
188
+ if (resolve(cwd, dir) === resolve(cwd)) {
189
+ return `Created a ${template} Codsy project in the current folder.
190
+ Next: codsy check
191
+ `;
192
+ }
193
+ return `Created a ${template} Codsy project in ${dir}.
194
+ Next: cd ${dir} && codsy check
195
+ `;
196
+ }
197
+
198
+ // src/cli/check.ts
199
+ import { existsSync as existsSync2 } from "fs";
200
+ import { join as join4 } from "path";
201
+
202
+ // src/rules/schema-rules.ts
203
+ import Ajv2020 from "ajv/dist/2020.js";
204
+
205
+ // src/core/config.ts
206
+ import { join as join3 } from "path";
207
+ import { load as loadYaml } from "js-yaml";
208
+ var DEFAULT_CONFIG = {
209
+ severities: {},
210
+ requiredSections: [
211
+ "Purpose",
212
+ "When to use",
213
+ "Input",
214
+ "Decision logic",
215
+ "Exceptions",
216
+ "Output",
217
+ "Examples"
218
+ ],
219
+ maxSkillBodyLines: 300,
220
+ minWorkedExamples: 2,
221
+ minEvalCases: 10,
222
+ maxRequiredSchemaFields: 8,
223
+ vagueTermsExtensions: [],
224
+ strict: false,
225
+ descriptionSimilarityThreshold: 0.7,
226
+ allowedScripts: [],
227
+ personalDataPatterns: [],
228
+ eval: {
229
+ provider: "command",
230
+ command: null,
231
+ model: null,
232
+ baseUrl: "https://api.openai.com/v1",
233
+ threshold: 0.8
234
+ }
235
+ };
236
+ function mergeConfig(overrides) {
237
+ return {
238
+ severities: isRecord(overrides.severities) ? { ...DEFAULT_CONFIG.severities, ...toSeverityMap(overrides.severities) } : { ...DEFAULT_CONFIG.severities },
239
+ requiredSections: isStringArray(overrides.requiredSections) ? overrides.requiredSections : [...DEFAULT_CONFIG.requiredSections],
240
+ maxSkillBodyLines: isNumber(overrides.maxSkillBodyLines) ? overrides.maxSkillBodyLines : DEFAULT_CONFIG.maxSkillBodyLines,
241
+ minWorkedExamples: isNumber(overrides.minWorkedExamples) ? overrides.minWorkedExamples : DEFAULT_CONFIG.minWorkedExamples,
242
+ minEvalCases: isNumber(overrides.minEvalCases) ? overrides.minEvalCases : DEFAULT_CONFIG.minEvalCases,
243
+ maxRequiredSchemaFields: isNumber(overrides.maxRequiredSchemaFields) ? overrides.maxRequiredSchemaFields : DEFAULT_CONFIG.maxRequiredSchemaFields,
244
+ vagueTermsExtensions: isStringArray(overrides.vagueTermsExtensions) ? [.../* @__PURE__ */ new Set([...DEFAULT_CONFIG.vagueTermsExtensions, ...overrides.vagueTermsExtensions])] : [...DEFAULT_CONFIG.vagueTermsExtensions],
245
+ strict: typeof overrides.strict === "boolean" ? overrides.strict : DEFAULT_CONFIG.strict,
246
+ descriptionSimilarityThreshold: isNumber(overrides.descriptionSimilarityThreshold) ? overrides.descriptionSimilarityThreshold : DEFAULT_CONFIG.descriptionSimilarityThreshold,
247
+ allowedScripts: isStringArray(overrides.allowedScripts) ? overrides.allowedScripts : [...DEFAULT_CONFIG.allowedScripts],
248
+ personalDataPatterns: isStringArray(overrides.personalDataPatterns) ? overrides.personalDataPatterns : [...DEFAULT_CONFIG.personalDataPatterns],
249
+ eval: mergeEvalConfig(overrides.eval)
250
+ };
251
+ }
252
+ function mergeEvalConfig(overrides) {
253
+ if (!isRecord(overrides)) return { ...DEFAULT_CONFIG.eval };
254
+ const provider = overrides.provider === "command" || overrides.provider === "anthropic" || overrides.provider === "openai-compatible" ? overrides.provider : DEFAULT_CONFIG.eval.provider;
255
+ return {
256
+ provider,
257
+ command: typeof overrides.command === "string" ? overrides.command : DEFAULT_CONFIG.eval.command,
258
+ model: typeof overrides.model === "string" ? overrides.model : DEFAULT_CONFIG.eval.model,
259
+ baseUrl: typeof overrides.baseUrl === "string" ? overrides.baseUrl : DEFAULT_CONFIG.eval.baseUrl,
260
+ threshold: isNumber(overrides.threshold) ? overrides.threshold : DEFAULT_CONFIG.eval.threshold
261
+ };
262
+ }
263
+ function resolveSeverity(config, ruleId, defaultSeverity) {
264
+ return config.severities[ruleId] ?? defaultSeverity;
265
+ }
266
+ async function loadConfig(rootDir) {
267
+ const filePath = join3(rootDir, "codsy.config.yaml");
268
+ const raw = await readFileIfExists(filePath);
269
+ if (raw === null) return mergeConfig({});
270
+ const parsed = loadYaml(raw);
271
+ if (!isRecord(parsed)) {
272
+ throw new Error(`${filePath} must contain a YAML mapping at the top level.`);
273
+ }
274
+ return mergeConfig(parsed);
275
+ }
276
+ function isStringArray(value) {
277
+ return Array.isArray(value) && value.every((item) => typeof item === "string");
278
+ }
279
+ function isNumber(value) {
280
+ return typeof value === "number" && Number.isFinite(value);
281
+ }
282
+ function toSeverityMap(value) {
283
+ const result = {};
284
+ for (const [key, val] of Object.entries(value)) {
285
+ if (val === "error" || val === "warn" || val === "off") {
286
+ result[key] = val;
287
+ }
288
+ }
289
+ return result;
290
+ }
291
+
292
+ // src/rules/schema-rules.ts
293
+ function compileOneSchema(schema) {
294
+ if (schema.parseError) return { validate: null, error: schema.parseError };
295
+ const ajv = new Ajv2020({ allErrors: true, strict: false });
296
+ try {
297
+ const validate = ajv.compile(schema.json);
298
+ return { validate, error: null };
299
+ } catch (error) {
300
+ return { validate: null, error: error instanceof Error ? error.message : String(error) };
301
+ }
302
+ }
303
+ function compileSchemasByName(schemas) {
304
+ const map = /* @__PURE__ */ new Map();
305
+ for (const schema of schemas) {
306
+ map.set(schema.name, compileOneSchema(schema));
307
+ }
308
+ return map;
309
+ }
310
+ function walkSchemaNodes(node, path, visit) {
311
+ if (Array.isArray(node)) {
312
+ node.forEach((item, index) => walkSchemaNodes(item, `${path}[${index}]`, visit));
313
+ return;
314
+ }
315
+ if (isRecord(node)) {
316
+ visit(node, path);
317
+ for (const [key, value] of Object.entries(node)) {
318
+ walkSchemaNodes(value, path ? `${path}.${key}` : key, visit);
319
+ }
320
+ }
321
+ }
322
+ function hasNonEmptyDescription(node) {
323
+ return typeof node.description === "string" && node.description.trim().length > 0;
324
+ }
325
+ function findPropertiesWithoutDescription(schema) {
326
+ const missing = [];
327
+ walkSchemaNodes(schema, "", (node, path) => {
328
+ if (!isRecord(node.properties)) return;
329
+ for (const [propName, propSchema] of Object.entries(node.properties)) {
330
+ if (!isRecord(propSchema) || !hasNonEmptyDescription(propSchema)) {
331
+ missing.push(path ? `${path}.properties.${propName}` : `properties.${propName}`);
332
+ }
333
+ }
334
+ });
335
+ return missing;
336
+ }
337
+ function findOversizedRequiredLists(schema, limit) {
338
+ const results = [];
339
+ walkSchemaNodes(schema, "", (node, path) => {
340
+ const required = node.required;
341
+ if (Array.isArray(required) && required.length > limit) {
342
+ results.push({ path: path || "(root)", count: required.length });
343
+ }
344
+ });
345
+ return results;
346
+ }
347
+ function findUndocumentedEnums(schema) {
348
+ const results = [];
349
+ walkSchemaNodes(schema, "", (node, path) => {
350
+ if (Array.isArray(node.enum) && !hasNonEmptyDescription(node)) {
351
+ results.push(path || "(root)");
352
+ }
353
+ });
354
+ return results;
355
+ }
356
+ var codsy100InvalidSchema = (project, config) => {
357
+ const severity = resolveSeverity(config, "CODSY100", "error");
358
+ if (severity === "off") return [];
359
+ const diagnostics = [];
360
+ for (const schema of project.schemas) {
361
+ if (schema.parseError) {
362
+ diagnostics.push({
363
+ ruleId: "CODSY100",
364
+ severity,
365
+ file: schema.relativePath,
366
+ line: 1,
367
+ message: `${schema.relativePath} is not valid JSON: ${schema.parseError}`,
368
+ fixHint: "Fix the JSON syntax so the file can be parsed."
369
+ });
370
+ continue;
371
+ }
372
+ const compiled = compileOneSchema(schema);
373
+ if (compiled.error) {
374
+ diagnostics.push({
375
+ ruleId: "CODSY100",
376
+ severity,
377
+ file: schema.relativePath,
378
+ line: 1,
379
+ message: `${schema.relativePath} is not a valid JSON Schema: ${compiled.error}`,
380
+ fixHint: "Fix the schema so it validates against the JSON Schema 2020-12 meta-schema."
381
+ });
382
+ }
383
+ }
384
+ return diagnostics;
385
+ };
386
+ var codsy101MissingPropertyDescription = (project, config) => {
387
+ const severity = resolveSeverity(config, "CODSY101", "warn");
388
+ if (severity === "off") return [];
389
+ const diagnostics = [];
390
+ for (const schema of project.schemas) {
391
+ if (schema.parseError) continue;
392
+ for (const path of findPropertiesWithoutDescription(schema.json)) {
393
+ diagnostics.push({
394
+ ruleId: "CODSY101",
395
+ severity,
396
+ file: schema.relativePath,
397
+ line: 1,
398
+ message: `Property '${path}' has no description.`,
399
+ fixHint: `Add a "description" field explaining what '${path}' means.`
400
+ });
401
+ }
402
+ }
403
+ return diagnostics;
404
+ };
405
+ var codsy102TooManyRequiredFields = (project, config) => {
406
+ const severity = resolveSeverity(config, "CODSY102", "warn");
407
+ if (severity === "off") return [];
408
+ const diagnostics = [];
409
+ for (const schema of project.schemas) {
410
+ if (schema.parseError) continue;
411
+ for (const { path, count } of findOversizedRequiredLists(
412
+ schema.json,
413
+ config.maxRequiredSchemaFields
414
+ )) {
415
+ diagnostics.push({
416
+ ruleId: "CODSY102",
417
+ severity,
418
+ file: schema.relativePath,
419
+ line: 1,
420
+ message: `'${path}' requires ${count} fields, more than the limit of ${config.maxRequiredSchemaFields}.`,
421
+ fixHint: "Split this schema or make some of these fields optional."
422
+ });
423
+ }
424
+ }
425
+ return diagnostics;
426
+ };
427
+ var codsy103UndocumentedEnum = (project, config) => {
428
+ const severity = resolveSeverity(config, "CODSY103", "warn");
429
+ if (severity === "off") return [];
430
+ const diagnostics = [];
431
+ for (const schema of project.schemas) {
432
+ if (schema.parseError) continue;
433
+ for (const path of findUndocumentedEnums(schema.json)) {
434
+ diagnostics.push({
435
+ ruleId: "CODSY103",
436
+ severity,
437
+ file: schema.relativePath,
438
+ line: 1,
439
+ message: `Enum at '${path}' has no description explaining what its values mean.`,
440
+ fixHint: `Add a "description" to '${path}' that explains each allowed value.`
441
+ });
442
+ }
443
+ }
444
+ return diagnostics;
445
+ };
446
+ var codsy104MissingSchemaReference = (project, config) => {
447
+ const severity = resolveSeverity(config, "CODSY104", "error");
448
+ if (severity === "off") return [];
449
+ const schemaNames = new Set(project.schemas.map((schema) => schema.name));
450
+ const diagnostics = [];
451
+ for (const skill of project.skills) {
452
+ for (const name of referencedSchemaNames(skill.frontmatter.metadata?.schemas)) {
453
+ if (!schemaNames.has(name)) {
454
+ diagnostics.push({
455
+ ruleId: "CODSY104",
456
+ severity,
457
+ file: skill.relativePath,
458
+ line: 1,
459
+ message: `Skill '${skill.dirName}' references schema '${name}', but schemas/${name}.schema.json does not exist.`,
460
+ fixHint: `Create schemas/${name}.schema.json, or fix the schema name in SKILL.md.`
461
+ });
462
+ }
463
+ }
464
+ }
465
+ return diagnostics;
466
+ };
467
+ var codsy105EvalInputFailsSchema = (project, config) => {
468
+ const severity = resolveSeverity(config, "CODSY105", "error");
469
+ if (severity === "off") return [];
470
+ const compiledByName = compileSchemasByName(project.schemas);
471
+ const skillsByName = new Map(project.skills.map((skill) => [skill.dirName, skill]));
472
+ const diagnostics = [];
473
+ for (const suite of project.evalSuites) {
474
+ if (!suite.skill) continue;
475
+ const skill = skillsByName.get(suite.skill);
476
+ if (!skill) continue;
477
+ for (const schemaName of referencedSchemaNames(skill.frontmatter.metadata?.schemas)) {
478
+ const compiled = compiledByName.get(schemaName);
479
+ if (!compiled?.validate) continue;
480
+ const validate = compiled.validate;
481
+ for (const evalCase2 of suite.cases) {
482
+ if (validate(evalCase2.input)) continue;
483
+ const errorText = (validate.errors ?? []).map((error) => `${error.instancePath || "(root)"} ${error.message ?? ""}`.trim()).join("; ");
484
+ diagnostics.push({
485
+ ruleId: "CODSY105",
486
+ severity,
487
+ file: suite.relativePath,
488
+ line: 1,
489
+ message: `Case '${evalCase2.id}' input does not match schema '${schemaName}': ${errorText || "does not match the schema"}.`,
490
+ fixHint: `Fix the input in evals/${skill.dirName}/cases.yaml, or update schemas/${schemaName}.schema.json.`
491
+ });
492
+ }
493
+ }
494
+ }
495
+ return diagnostics;
496
+ };
497
+ function referencedSchemaNames(value) {
498
+ return Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
499
+ }
500
+ var schemaRules = [
501
+ codsy100InvalidSchema,
502
+ codsy101MissingPropertyDescription,
503
+ codsy102TooManyRequiredFields,
504
+ codsy103UndocumentedEnum,
505
+ codsy104MissingSchemaReference,
506
+ codsy105EvalInputFailsSchema
507
+ ];
508
+
509
+ // src/rules/section-utils.ts
510
+ function findSection(sections, heading) {
511
+ const target = heading.trim().toLowerCase();
512
+ for (const section of sections) {
513
+ if (section.heading.trim().toLowerCase() === target) return section;
514
+ const found = findSection(section.children, heading);
515
+ if (found) return found;
516
+ }
517
+ return null;
518
+ }
519
+ function stripComments(text) {
520
+ return text.replace(/<!--[\s\S]*?-->/g, "");
521
+ }
522
+ function collectSectionText(section) {
523
+ const childText = section.children.map(collectSectionText).join("\n");
524
+ return [section.body, childText].filter((part) => part.length > 0).join("\n");
525
+ }
526
+
527
+ // src/rules/skill-structure-rules.ts
528
+ var MIN_DESCRIPTION_LENGTH = 20;
529
+ function isEffectivelyEmpty(body) {
530
+ return stripComments(body).trim().length === 0;
531
+ }
532
+ function countWorkedExamples(section) {
533
+ if (!section) return 0;
534
+ const lines = stripComments(section.body).split("\n");
535
+ const listItemCount = lines.filter((line) => /^\s*(\d+[.)]|[-*])\s+\S/.test(line)).length;
536
+ return listItemCount + section.children.length;
537
+ }
538
+ function isReviewOverdue(lastReviewed, reviewEveryDays) {
539
+ const lastDate = new Date(lastReviewed);
540
+ if (Number.isNaN(lastDate.getTime())) return false;
541
+ const dueAt = lastDate.getTime() + reviewEveryDays * 24 * 60 * 60 * 1e3;
542
+ return Date.now() > dueAt;
543
+ }
544
+ var codsy001InvalidFrontmatter = (project, config) => {
545
+ const severity = resolveSeverity(config, "CODSY001", "error");
546
+ if (severity === "off") return [];
547
+ const diagnostics = [];
548
+ for (const skill of project.skills) {
549
+ if (skill.frontmatterError) {
550
+ diagnostics.push({
551
+ ruleId: "CODSY001",
552
+ severity,
553
+ file: skill.relativePath,
554
+ line: 1,
555
+ message: skill.frontmatterError,
556
+ fixHint: "Fix the YAML frontmatter at the top of SKILL.md."
557
+ });
558
+ continue;
559
+ }
560
+ const name = skill.frontmatter.name;
561
+ if (typeof name !== "string" || name.trim().length === 0) {
562
+ diagnostics.push({
563
+ ruleId: "CODSY001",
564
+ severity,
565
+ file: skill.relativePath,
566
+ line: 1,
567
+ message: `SKILL.md frontmatter for '${skill.dirName}' is missing a "name" field.`,
568
+ fixHint: `Add name: ${skill.dirName} to the frontmatter.`
569
+ });
570
+ continue;
571
+ }
572
+ if (name !== skill.dirName) {
573
+ diagnostics.push({
574
+ ruleId: "CODSY001",
575
+ severity,
576
+ file: skill.relativePath,
577
+ line: 1,
578
+ message: `Frontmatter name '${name}' does not match the folder name '${skill.dirName}'.`,
579
+ fixHint: `Rename the folder to '${name}', or change name to '${skill.dirName}' in the frontmatter.`
580
+ });
581
+ }
582
+ }
583
+ return diagnostics;
584
+ };
585
+ var codsy002WeakDescription = (project, config) => {
586
+ const severity = resolveSeverity(config, "CODSY002", "warn");
587
+ if (severity === "off") return [];
588
+ const diagnostics = [];
589
+ for (const skill of project.skills) {
590
+ const description = skill.frontmatter.description;
591
+ const trimmed = typeof description === "string" ? description.trim() : "";
592
+ let message = null;
593
+ if (trimmed.length === 0) {
594
+ message = `Skill '${skill.dirName}' has no description.`;
595
+ } else if (trimmed.length < MIN_DESCRIPTION_LENGTH) {
596
+ message = `Skill '${skill.dirName}' description is too short to explain what it does.`;
597
+ } else if (!/\bwhen\b/i.test(trimmed)) {
598
+ message = `Skill '${skill.dirName}' description does not say when to use it.`;
599
+ }
600
+ if (message) {
601
+ diagnostics.push({
602
+ ruleId: "CODSY002",
603
+ severity,
604
+ file: skill.relativePath,
605
+ line: 1,
606
+ message,
607
+ fixHint: "Describe what the skill does and when an agent should use it, in one or two sentences."
608
+ });
609
+ }
610
+ }
611
+ return diagnostics;
612
+ };
613
+ var codsy010MissingRequiredSections = (project, config) => {
614
+ const severity = resolveSeverity(config, "CODSY010", "error");
615
+ if (severity === "off") return [];
616
+ const diagnostics = [];
617
+ for (const skill of project.skills) {
618
+ for (const requiredHeading of config.requiredSections) {
619
+ if (!findSection(skill.sections, requiredHeading)) {
620
+ diagnostics.push({
621
+ ruleId: "CODSY010",
622
+ severity,
623
+ file: skill.relativePath,
624
+ line: skill.bodyStartLine,
625
+ message: `Skill '${skill.dirName}' is missing the required '${requiredHeading}' section.`,
626
+ fixHint: `Add a '## ${requiredHeading}' section to SKILL.md.`
627
+ });
628
+ }
629
+ }
630
+ }
631
+ return diagnostics;
632
+ };
633
+ var codsy011TooFewExamples = (project, config) => {
634
+ const severity = resolveSeverity(config, "CODSY011", "error");
635
+ if (severity === "off") return [];
636
+ const diagnostics = [];
637
+ for (const skill of project.skills) {
638
+ const examplesSection = findSection(skill.sections, "Examples");
639
+ const count = countWorkedExamples(examplesSection);
640
+ if (count < config.minWorkedExamples) {
641
+ diagnostics.push({
642
+ ruleId: "CODSY011",
643
+ severity,
644
+ file: skill.relativePath,
645
+ line: examplesSection?.line ?? skill.bodyStartLine,
646
+ message: `Skill '${skill.dirName}' has ${count} worked example(s); it needs at least ${config.minWorkedExamples}.`,
647
+ fixHint: "Add numbered real-world examples showing the input and the resulting decision."
648
+ });
649
+ }
650
+ }
651
+ return diagnostics;
652
+ };
653
+ var codsy012EmptyExceptions = (project, config) => {
654
+ const severity = resolveSeverity(config, "CODSY012", "warn");
655
+ if (severity === "off") return [];
656
+ const diagnostics = [];
657
+ for (const skill of project.skills) {
658
+ const exceptionsSection = findSection(skill.sections, "Exceptions");
659
+ if (exceptionsSection && isEffectivelyEmpty(exceptionsSection.body)) {
660
+ diagnostics.push({
661
+ ruleId: "CODSY012",
662
+ severity,
663
+ file: skill.relativePath,
664
+ line: exceptionsSection.line,
665
+ message: `Skill '${skill.dirName}' has an empty Exceptions section.`,
666
+ fixHint: "List the cases where the normal decision logic does not apply, or write 'None known.' if there truly are none."
667
+ });
668
+ }
669
+ }
670
+ return diagnostics;
671
+ };
672
+ var codsy050OwnerOrReview = (project, config) => {
673
+ const severity = resolveSeverity(config, "CODSY050", "warn");
674
+ if (severity === "off") return [];
675
+ const diagnostics = [];
676
+ for (const skill of project.skills) {
677
+ const metadata = skill.frontmatter.metadata;
678
+ const owner = metadata?.owner;
679
+ if (typeof owner !== "string" || owner.trim().length === 0) {
680
+ diagnostics.push({
681
+ ruleId: "CODSY050",
682
+ severity,
683
+ file: skill.relativePath,
684
+ line: 1,
685
+ message: `Skill '${skill.dirName}' has no owner.`,
686
+ fixHint: "Add metadata.owner to the frontmatter."
687
+ });
688
+ }
689
+ const lastReviewed = metadata?.last_reviewed;
690
+ const reviewEveryDays = metadata?.review_every_days;
691
+ if (typeof lastReviewed === "string" && typeof reviewEveryDays === "number" && isReviewOverdue(lastReviewed, reviewEveryDays)) {
692
+ diagnostics.push({
693
+ ruleId: "CODSY050",
694
+ severity,
695
+ file: skill.relativePath,
696
+ line: 1,
697
+ message: `Skill '${skill.dirName}' was last reviewed on ${lastReviewed} and is overdue for its ${reviewEveryDays}-day review.`,
698
+ fixHint: "Review the skill and update metadata.last_reviewed."
699
+ });
700
+ }
701
+ }
702
+ return diagnostics;
703
+ };
704
+ var codsy070InsufficientEvals = (project, config) => {
705
+ const defaultSeverity = config.strict ? "error" : "warn";
706
+ const severity = config.severities.CODSY070 ?? defaultSeverity;
707
+ if (severity === "off") return [];
708
+ const suitesBySkill = new Map(project.evalSuites.map((suite) => [suite.skill, suite]));
709
+ const diagnostics = [];
710
+ for (const skill of project.skills) {
711
+ const suite = suitesBySkill.get(skill.dirName);
712
+ if (!suite) {
713
+ diagnostics.push({
714
+ ruleId: "CODSY070",
715
+ severity,
716
+ file: skill.relativePath,
717
+ line: 1,
718
+ message: `Skill '${skill.dirName}' has no evals.`,
719
+ fixHint: `Add evals/${skill.dirName}/cases.yaml with at least ${config.minEvalCases} example cases.`
720
+ });
721
+ continue;
722
+ }
723
+ if (suite.cases.length < config.minEvalCases) {
724
+ diagnostics.push({
725
+ ruleId: "CODSY070",
726
+ severity,
727
+ file: suite.relativePath,
728
+ line: 1,
729
+ message: `Skill '${skill.dirName}' has ${suite.cases.length} eval case(s); it needs at least ${config.minEvalCases}.`,
730
+ fixHint: `Add more cases to evals/${skill.dirName}/cases.yaml.`
731
+ });
732
+ }
733
+ }
734
+ return diagnostics;
735
+ };
736
+ var skillStructureRules = [
737
+ codsy001InvalidFrontmatter,
738
+ codsy002WeakDescription,
739
+ codsy010MissingRequiredSections,
740
+ codsy011TooFewExamples,
741
+ codsy012EmptyExceptions,
742
+ codsy050OwnerOrReview,
743
+ codsy070InsufficientEvals
744
+ ];
745
+
746
+ // src/rules/vague-terms/index.ts
747
+ var DEFAULT_VAGUE_TERMS = [
748
+ "when appropriate",
749
+ "as soon as possible",
750
+ "large customer",
751
+ "use good judgment",
752
+ "prioritize"
753
+ ];
754
+
755
+ // src/rules/skill-quality-rules.ts
756
+ function bodyLineCount(skill) {
757
+ return skill.raw.split("\n").length - skill.bodyStartLine + 1;
758
+ }
759
+ function tokenize(text) {
760
+ const words = text.toLowerCase().replace(/[^a-z0-9\s]/g, " ").split(/\s+/).filter((word) => word.length > 0);
761
+ return new Set(words);
762
+ }
763
+ function jaccardSimilarity(a, b) {
764
+ if (a.size === 0 || b.size === 0) return 0;
765
+ let intersectionSize = 0;
766
+ for (const word of a) {
767
+ if (b.has(word)) intersectionSize += 1;
768
+ }
769
+ const unionSize = a.size + b.size - intersectionSize;
770
+ return unionSize === 0 ? 0 : intersectionSize / unionSize;
771
+ }
772
+ function splitSentences(text) {
773
+ return text.split(/\n+/).flatMap((line) => line.split(/(?<=[.!?])\s+/)).map((sentence) => sentence.trim()).filter((sentence) => sentence.length > 0);
774
+ }
775
+ function hasConcreteReference(sentence) {
776
+ return /\d/.test(sentence) || /`[^`]+`/.test(sentence);
777
+ }
778
+ function findVagueTerm(sentence, vagueTerms) {
779
+ const lower = sentence.toLowerCase();
780
+ for (const term of vagueTerms) {
781
+ if (lower.includes(term.toLowerCase())) return term;
782
+ }
783
+ return null;
784
+ }
785
+ var codsy020OversizedBody = (project, config) => {
786
+ const severity = resolveSeverity(config, "CODSY020", "warn");
787
+ if (severity === "off") return [];
788
+ const diagnostics = [];
789
+ for (const skill of project.skills) {
790
+ const lineCount = bodyLineCount(skill);
791
+ if (lineCount > config.maxSkillBodyLines) {
792
+ diagnostics.push({
793
+ ruleId: "CODSY020",
794
+ severity,
795
+ file: skill.relativePath,
796
+ line: 1,
797
+ message: `Skill '${skill.dirName}' body is ${lineCount} lines, over the limit of ${config.maxSkillBodyLines}.`,
798
+ fixHint: "Move background detail into a references/ file and keep SKILL.md focused on the decision."
799
+ });
800
+ }
801
+ }
802
+ return diagnostics;
803
+ };
804
+ var codsy021SimilarDescriptions = (project, config) => {
805
+ const severity = resolveSeverity(config, "CODSY021", "warn");
806
+ if (severity === "off") return [];
807
+ const entries = project.skills.map((skill) => ({
808
+ skill,
809
+ description: typeof skill.frontmatter.description === "string" ? skill.frontmatter.description.trim() : ""
810
+ })).filter((entry) => entry.description.length > 0);
811
+ const diagnostics = [];
812
+ for (let i = 0; i < entries.length; i += 1) {
813
+ for (let j = i + 1; j < entries.length; j += 1) {
814
+ const a = entries[i];
815
+ const b = entries[j];
816
+ if (!a || !b) continue;
817
+ const similarity = jaccardSimilarity(tokenize(a.description), tokenize(b.description));
818
+ if (similarity < config.descriptionSimilarityThreshold) continue;
819
+ const overlapPercent = Math.round(similarity * 100);
820
+ const message = `Skill '${a.skill.dirName}' and skill '${b.skill.dirName}' have highly similar descriptions (${overlapPercent}% word overlap).`;
821
+ diagnostics.push({
822
+ ruleId: "CODSY021",
823
+ severity,
824
+ file: a.skill.relativePath,
825
+ line: 1,
826
+ message,
827
+ fixHint: `Make the description specific about what makes '${a.skill.dirName}' different from '${b.skill.dirName}'.`
828
+ });
829
+ diagnostics.push({
830
+ ruleId: "CODSY021",
831
+ severity,
832
+ file: b.skill.relativePath,
833
+ line: 1,
834
+ message,
835
+ fixHint: `Make the description specific about what makes '${b.skill.dirName}' different from '${a.skill.dirName}'.`
836
+ });
837
+ }
838
+ }
839
+ return diagnostics;
840
+ };
841
+ var codsy030VagueStatement = (project, config) => {
842
+ const severity = resolveSeverity(config, "CODSY030", "warn");
843
+ if (severity === "off") return [];
844
+ const vagueTerms = [...DEFAULT_VAGUE_TERMS, ...config.vagueTermsExtensions];
845
+ const diagnostics = [];
846
+ for (const skill of project.skills) {
847
+ for (const sectionName of ["Decision logic", "Exceptions"]) {
848
+ const section = findSection(skill.sections, sectionName);
849
+ if (!section) continue;
850
+ for (const sentence of splitSentences(collectSectionText(section))) {
851
+ if (hasConcreteReference(sentence)) continue;
852
+ const term = findVagueTerm(sentence, vagueTerms);
853
+ if (!term) continue;
854
+ diagnostics.push({
855
+ ruleId: "CODSY030",
856
+ severity,
857
+ file: skill.relativePath,
858
+ line: section.line,
859
+ message: `Skill '${skill.dirName}' uses the vague phrase "${term}" in ${sectionName} without a number, unit, or concrete reference: "${sentence}"`,
860
+ fixHint: 'Replace it with a concrete rule, e.g. "at-risk accounts go to senior support within 2 hours".'
861
+ });
862
+ }
863
+ }
864
+ }
865
+ return diagnostics;
866
+ };
867
+ var skillQualityRules = [
868
+ codsy020OversizedBody,
869
+ codsy021SimilarDescriptions,
870
+ codsy030VagueStatement
871
+ ];
872
+
873
+ // src/rules/security-rules.ts
874
+ import { extname } from "path";
875
+
876
+ // src/rules/masking.ts
877
+ function maskMatch(value) {
878
+ if (value.length <= 4) return "*".repeat(value.length);
879
+ const visible = 2;
880
+ const starCount = Math.max(value.length - visible * 2, 3);
881
+ return `${value.slice(0, visible)}${"*".repeat(starCount)}${value.slice(-visible)}`;
882
+ }
883
+
884
+ // src/rules/personal-data-patterns.ts
885
+ var EXEMPT_EMAIL_DOMAINS = /* @__PURE__ */ new Set([
886
+ "example.com",
887
+ "example.org",
888
+ "example.net",
889
+ "example.edu",
890
+ "test.com"
891
+ ]);
892
+ var EMAIL_PATTERN_NAME = "email address";
893
+ var DEFAULT_PERSONAL_DATA_PATTERNS = [
894
+ { name: EMAIL_PATTERN_NAME, pattern: /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g },
895
+ {
896
+ name: "US phone number",
897
+ pattern: /\b(?:\+?1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b/g
898
+ },
899
+ { name: "US Social Security number", pattern: /\b\d{3}-\d{2}-\d{4}\b/g }
900
+ ];
901
+ function isExemptEmailMatch(match) {
902
+ const domain = match.split("@")[1]?.toLowerCase();
903
+ return domain !== void 0 && EXEMPT_EMAIL_DOMAINS.has(domain);
904
+ }
905
+
906
+ // src/rules/secret-patterns.ts
907
+ var SECRET_PATTERNS = [
908
+ { name: "AWS access key ID", pattern: /AKIA[0-9A-Z]{16}/g },
909
+ { name: "GitHub token", pattern: /gh[pousr]_[A-Za-z0-9]{36,}/g },
910
+ { name: "Slack token", pattern: /xox[baprs]-[0-9A-Za-z-]{10,}/g },
911
+ {
912
+ name: "private key block",
913
+ pattern: /-----BEGIN (?:RSA |EC |OPENSSH |DSA |)PRIVATE KEY-----/g
914
+ },
915
+ {
916
+ name: "assigned secret value",
917
+ pattern: /(?:api[_-]?key|secret|password|token)\s*[:=]\s*['"]?[A-Za-z0-9_\-./+=]{16,}['"]?/gi
918
+ }
919
+ ];
920
+ var PROMPT_INJECTION_PATTERNS = [
921
+ /ignore (?:all )?previous instructions/gi,
922
+ /disregard (?:the )?system prompt/gi,
923
+ /you are now (?:in )?(?:developer|dan) mode/gi,
924
+ /reveal your (?:system prompt|instructions)/gi,
925
+ /do anything now/gi,
926
+ /act as if you have no (?:restrictions|limitations)/gi
927
+ ];
928
+
929
+ // src/rules/security-rules.ts
930
+ function allScannableFiles(project) {
931
+ return [
932
+ ...project.skills.map((skill) => ({ file: skill.relativePath, raw: skill.raw })),
933
+ ...project.contextFiles.map((file) => ({ file: file.relativePath, raw: file.raw })),
934
+ ...project.schemas.map((schema) => ({ file: schema.relativePath, raw: schema.raw })),
935
+ ...project.evalSuites.map((suite) => ({ file: suite.relativePath, raw: suite.raw }))
936
+ ];
937
+ }
938
+ function skillAndContextFiles(project) {
939
+ return [
940
+ ...project.skills.map((skill) => ({ file: skill.relativePath, raw: skill.raw })),
941
+ ...project.contextFiles.map((file) => ({ file: file.relativePath, raw: file.raw }))
942
+ ];
943
+ }
944
+ function lineNumberAt(text, index) {
945
+ return text.slice(0, index).split("\n").length;
946
+ }
947
+ function findAllMatches(pattern, text) {
948
+ const flags = pattern.flags.includes("g") ? pattern.flags : `${pattern.flags}g`;
949
+ const re = new RegExp(pattern.source, flags);
950
+ const matches = [];
951
+ let match;
952
+ while ((match = re.exec(text)) !== null) {
953
+ matches.push(match);
954
+ if (match[0].length === 0) re.lastIndex += 1;
955
+ }
956
+ return matches;
957
+ }
958
+ function scanFor(files, patterns, build) {
959
+ const diagnostics = [];
960
+ for (const { file, raw } of files) {
961
+ for (const { name, pattern } of patterns) {
962
+ for (const match of findAllMatches(pattern, raw)) {
963
+ diagnostics.push(build(file, lineNumberAt(raw, match.index), name, match[0]));
964
+ }
965
+ }
966
+ }
967
+ return diagnostics;
968
+ }
969
+ var codsy060Secrets = (project, config) => {
970
+ const severity = resolveSeverity(config, "CODSY060", "error");
971
+ if (severity === "off") return [];
972
+ return scanFor(allScannableFiles(project), SECRET_PATTERNS, (file, line, name, matchText) => ({
973
+ ruleId: "CODSY060",
974
+ severity,
975
+ file,
976
+ line,
977
+ message: `Possible ${name} found: ${maskMatch(matchText)}.`,
978
+ fixHint: "Remove the secret from version control and rotate it immediately."
979
+ }));
980
+ };
981
+ var codsy061PromptInjection = (project, config) => {
982
+ const severity = resolveSeverity(config, "CODSY061", "error");
983
+ if (severity === "off") return [];
984
+ const diagnostics = [];
985
+ for (const { file, raw } of skillAndContextFiles(project)) {
986
+ for (const pattern of PROMPT_INJECTION_PATTERNS) {
987
+ for (const match of findAllMatches(pattern, raw)) {
988
+ diagnostics.push({
989
+ ruleId: "CODSY061",
990
+ severity,
991
+ file,
992
+ line: lineNumberAt(raw, match.index),
993
+ message: `Possible prompt-injection text found: "${match[0]}".`,
994
+ fixHint: "Remove or rephrase this text; it reads as an instruction aimed at an AI agent rather than content about the business."
995
+ });
996
+ }
997
+ }
998
+ }
999
+ return diagnostics;
1000
+ };
1001
+ var EXECUTABLE_SCRIPT_EXTENSIONS = /* @__PURE__ */ new Set([
1002
+ ".sh",
1003
+ ".bash",
1004
+ ".zsh",
1005
+ ".py",
1006
+ ".rb",
1007
+ ".pl",
1008
+ ".js",
1009
+ ".mjs",
1010
+ ".cjs",
1011
+ ".ts",
1012
+ ".ps1",
1013
+ ".psm1",
1014
+ ".bat",
1015
+ ".cmd",
1016
+ ".exe"
1017
+ ]);
1018
+ var codsy062DisallowedScripts = (project, config) => {
1019
+ const severity = resolveSeverity(config, "CODSY062", "error");
1020
+ if (severity === "off") return [];
1021
+ const allowed = new Set(config.allowedScripts);
1022
+ const diagnostics = [];
1023
+ for (const skill of project.skills) {
1024
+ for (const relativeFile of skill.files) {
1025
+ if (!EXECUTABLE_SCRIPT_EXTENSIONS.has(extname(relativeFile).toLowerCase())) continue;
1026
+ const allowlistKey = `${skill.dirName}/${relativeFile}`;
1027
+ if (allowed.has(allowlistKey)) continue;
1028
+ diagnostics.push({
1029
+ ruleId: "CODSY062",
1030
+ severity,
1031
+ file: `${skill.relativePath.replace(/SKILL\.md$/, "")}${relativeFile}`,
1032
+ line: 1,
1033
+ message: `Skill '${skill.dirName}' contains an executable script '${relativeFile}' that is not on the allowlist.`,
1034
+ fixHint: `Add '${allowlistKey}' to allowedScripts in codsy.config.yaml if this script is reviewed and trusted, or remove it.`
1035
+ });
1036
+ }
1037
+ }
1038
+ return diagnostics;
1039
+ };
1040
+ var codsy080PersonalData = (project, config) => {
1041
+ const severity = resolveSeverity(config, "CODSY080", "error");
1042
+ if (severity === "off") return [];
1043
+ const customPatterns = config.personalDataPatterns.map((source) => ({
1044
+ name: "custom personal data pattern",
1045
+ pattern: new RegExp(source, "g")
1046
+ }));
1047
+ const patterns = [...DEFAULT_PERSONAL_DATA_PATTERNS, ...customPatterns];
1048
+ const diagnostics = [];
1049
+ for (const { file, raw } of allScannableFiles(project)) {
1050
+ for (const { name, pattern } of patterns) {
1051
+ for (const match of findAllMatches(pattern, raw)) {
1052
+ if (name === EMAIL_PATTERN_NAME && isExemptEmailMatch(match[0])) continue;
1053
+ diagnostics.push({
1054
+ ruleId: "CODSY080",
1055
+ severity,
1056
+ file,
1057
+ line: lineNumberAt(raw, match.index),
1058
+ message: `Possible ${name} found: ${maskMatch(match[0])}.`,
1059
+ fixHint: "Replace real personal data with fictional sample data, or redact it."
1060
+ });
1061
+ }
1062
+ }
1063
+ }
1064
+ return diagnostics;
1065
+ };
1066
+ var securityRules = [
1067
+ codsy060Secrets,
1068
+ codsy061PromptInjection,
1069
+ codsy062DisallowedScripts,
1070
+ codsy080PersonalData
1071
+ ];
1072
+
1073
+ // src/core/all-rules.ts
1074
+ var ALL_RULES = [
1075
+ ...schemaRules,
1076
+ ...skillStructureRules,
1077
+ ...skillQualityRules,
1078
+ ...securityRules
1079
+ ];
1080
+
1081
+ // src/core/ignore.ts
1082
+ var IGNORE_LINE_PATTERN = /<!--\s*codsy-ignore\s+([A-Za-z]+\d+)\s*:\s*(.*?)\s*-->/;
1083
+ function parseIgnoreDirectives(raw) {
1084
+ const directives = [];
1085
+ const lines = raw.split("\n");
1086
+ lines.forEach((lineText, index) => {
1087
+ const match = IGNORE_LINE_PATTERN.exec(lineText);
1088
+ if (!match) return;
1089
+ const ruleId = match[1];
1090
+ const reason = match[2];
1091
+ if (ruleId === void 0 || reason === void 0) return;
1092
+ if (reason.trim().length === 0) return;
1093
+ directives.push({ ruleId, reason: reason.trim(), line: index + 1 });
1094
+ });
1095
+ return directives;
1096
+ }
1097
+ function filterIgnoredDiagnostics(diagnostics, rawContentByFile) {
1098
+ const ignoredRuleIdsByFile = /* @__PURE__ */ new Map();
1099
+ return diagnostics.filter((diagnostic) => {
1100
+ const raw = rawContentByFile.get(diagnostic.file);
1101
+ if (raw === void 0) return true;
1102
+ let ignoredRuleIds = ignoredRuleIdsByFile.get(diagnostic.file);
1103
+ if (!ignoredRuleIds) {
1104
+ ignoredRuleIds = new Set(parseIgnoreDirectives(raw).map((directive) => directive.ruleId));
1105
+ ignoredRuleIdsByFile.set(diagnostic.file, ignoredRuleIds);
1106
+ }
1107
+ return !ignoredRuleIds.has(diagnostic.ruleId);
1108
+ });
1109
+ }
1110
+
1111
+ // src/core/rule-engine.ts
1112
+ function runRules(rules, project, config) {
1113
+ const diagnostics = rules.flatMap((rule) => rule(project, config));
1114
+ const rawContentByFile = /* @__PURE__ */ new Map();
1115
+ for (const skill of project.skills) {
1116
+ rawContentByFile.set(skill.relativePath, skill.raw);
1117
+ }
1118
+ for (const contextFile of project.contextFiles) {
1119
+ rawContentByFile.set(contextFile.relativePath, contextFile.raw);
1120
+ }
1121
+ return filterIgnoredDiagnostics(diagnostics, rawContentByFile);
1122
+ }
1123
+
1124
+ // src/core/check-runner.ts
1125
+ async function runCheck(rootDir, strict) {
1126
+ const baseConfig = await loadConfig(rootDir);
1127
+ const config = strict ? { ...baseConfig, strict: true } : baseConfig;
1128
+ const project = await loadProject(rootDir);
1129
+ const diagnostics = runRules(ALL_RULES, project, config);
1130
+ return {
1131
+ config,
1132
+ project,
1133
+ diagnostics,
1134
+ errorCount: diagnostics.filter((d) => d.severity === "error").length,
1135
+ warnCount: diagnostics.filter((d) => d.severity === "warn").length
1136
+ };
1137
+ }
1138
+
1139
+ // src/cli/check.ts
1140
+ var NO_PROJECT_MESSAGE = "No Codsy project found here. Run codsy init to start one.\n";
1141
+ function parseCheckArgs(argv) {
1142
+ let strict = false;
1143
+ let format = "pretty";
1144
+ let invalidFormat = null;
1145
+ for (let i = 0; i < argv.length; i += 1) {
1146
+ const arg = argv[i];
1147
+ if (arg === "--strict") {
1148
+ strict = true;
1149
+ } else if (arg === "--format") {
1150
+ const value = argv[i + 1];
1151
+ if (value === "pretty" || value === "json" || value === "sarif") {
1152
+ format = value;
1153
+ } else {
1154
+ invalidFormat = value ?? "";
1155
+ }
1156
+ i += 1;
1157
+ }
1158
+ }
1159
+ return { strict, format, invalidFormat };
1160
+ }
1161
+ async function runCheckCommand(argv, options) {
1162
+ const { strict, format, invalidFormat } = parseCheckArgs(argv);
1163
+ if (invalidFormat !== null) {
1164
+ options.write(`Unknown format "${invalidFormat}". Choose one of: pretty, json, sarif.
1165
+ `);
1166
+ return 1;
1167
+ }
1168
+ if (!existsSync2(join4(options.rootDir, "codsy.config.yaml"))) {
1169
+ options.write(NO_PROJECT_MESSAGE);
1170
+ return 1;
1171
+ }
1172
+ const result = await runCheck(options.rootDir, strict);
1173
+ const useRich = format === "pretty" && options.isInteractive && options.renderRichReport;
1174
+ if (useRich && options.renderRichReport) {
1175
+ await options.renderRichReport(result.diagnostics);
1176
+ } else {
1177
+ options.write(formatReport(result.diagnostics, format));
1178
+ }
1179
+ return result.errorCount > 0 ? 1 : 0;
1180
+ }
1181
+
1182
+ // src/core/rule-docs.ts
1183
+ var RULE_DOCS = {
1184
+ CODSY001: {
1185
+ summary: "A skill's frontmatter is invalid, or its name does not match its folder.",
1186
+ why: "Tools and agents load a skill by its folder name; a mismatched or broken name silently breaks that lookup.",
1187
+ fix: "Fix the YAML frontmatter, and make sure name matches the folder the SKILL.md file lives in."
1188
+ },
1189
+ CODSY002: {
1190
+ summary: "A skill description is missing, too short, or does not say when to use the skill.",
1191
+ why: "An agent decides whether to use a skill from its description alone, before reading the rest of it. A weak description means the skill never gets picked when it should.",
1192
+ fix: "Write one or two sentences stating what the skill decides and when an agent should use it."
1193
+ },
1194
+ CODSY010: {
1195
+ summary: "A skill is missing one of its required sections.",
1196
+ why: "Each required section (Purpose, When to use, Input, Decision logic, Exceptions, Output, Examples) answers a question a reader or agent needs answered before trusting the skill.",
1197
+ fix: "Add the missing section with real content."
1198
+ },
1199
+ CODSY011: {
1200
+ summary: "A skill has fewer than the required number of worked examples.",
1201
+ why: "Worked examples are the fastest way to check that the decision logic actually matches real cases, for both a human reviewer and an agent.",
1202
+ fix: "Add numbered real-world examples showing a concrete input and the resulting decision."
1203
+ },
1204
+ CODSY012: {
1205
+ summary: "A skill's Exceptions section is empty.",
1206
+ why: "An empty Exceptions section usually means exceptions were never thought through, not that none exist.",
1207
+ fix: "List the cases where the normal decision logic does not apply, or write 'None known.' if there truly are none."
1208
+ },
1209
+ CODSY050: {
1210
+ summary: "A skill has no owner, or its review is overdue.",
1211
+ why: "Operational knowledge goes stale. Without an owner and a review cadence, nobody notices when a rule stops matching reality.",
1212
+ fix: "Add metadata.owner, and review the skill to update metadata.last_reviewed."
1213
+ },
1214
+ CODSY070: {
1215
+ summary: "A skill has no evals, or fewer than the required number of cases.",
1216
+ why: "Lint only checks that a skill is well-formed; evals are the only thing that checks it actually decides correctly.",
1217
+ fix: "Add more cases to the matching evals/<skill>/cases.yaml file."
1218
+ },
1219
+ CODSY020: {
1220
+ summary: "A skill's body is longer than the configured line limit.",
1221
+ why: "A long SKILL.md is expensive for both a human and an agent to read in full, and usually mixes decision logic with background detail that belongs elsewhere.",
1222
+ fix: "Move background detail into a references/ file and keep SKILL.md focused on the decision."
1223
+ },
1224
+ CODSY021: {
1225
+ summary: "Two skills have highly similar descriptions.",
1226
+ why: "If an agent cannot tell two skills apart from their descriptions, it will pick the wrong one at least some of the time.",
1227
+ fix: "Make each description specific about what makes that skill different from the other."
1228
+ },
1229
+ CODSY030: {
1230
+ summary: "A sentence in Decision logic or Exceptions uses a vague term with no concrete reference.",
1231
+ why: 'A vague rule like "when appropriate" cannot be followed consistently by a human or an agent; it just moves the real decision to whoever reads it.',
1232
+ fix: 'Replace it with a concrete rule, e.g. "at-risk accounts go to senior support within 2 hours".'
1233
+ },
1234
+ CODSY100: {
1235
+ summary: "A schema file does not validate against the JSON Schema 2020-12 meta-schema.",
1236
+ why: "An invalid schema cannot reliably validate anything else, including eval inputs.",
1237
+ fix: "Fix the schema so it validates against the JSON Schema 2020-12 meta-schema."
1238
+ },
1239
+ CODSY101: {
1240
+ summary: "A schema property has no description.",
1241
+ why: "A property with no description leaves both humans and agents guessing at what the field actually means.",
1242
+ fix: 'Add a "description" field explaining what the property means.'
1243
+ },
1244
+ CODSY102: {
1245
+ summary: "A schema requires more fields than the configured limit.",
1246
+ why: "A schema with many required fields is hard to satisfy correctly and often signals it is covering more than one real-world concept.",
1247
+ fix: "Split the schema, or make some of the required fields optional."
1248
+ },
1249
+ CODSY103: {
1250
+ summary: "A schema enum has no description explaining what its values mean.",
1251
+ why: "Enum values are often short codes; without an explanation, their meaning is only in the schema author's head.",
1252
+ fix: 'Add a "description" that explains each allowed value.'
1253
+ },
1254
+ CODSY104: {
1255
+ summary: "A skill references a schema that does not exist.",
1256
+ why: "A skill that points at a missing schema cannot be validated and will fail at the moment it matters.",
1257
+ fix: "Create the missing schemas/<name>.schema.json file, or fix the schema name in the skill's frontmatter."
1258
+ },
1259
+ CODSY105: {
1260
+ summary: "An eval case's input does not match the schema it is supposed to follow.",
1261
+ why: "An eval case with invalid input is not actually testing the skill against realistic data.",
1262
+ fix: "Fix the input in the eval case, or update the schema to match reality."
1263
+ },
1264
+ CODSY060: {
1265
+ summary: "A file appears to contain a secret, such as an API key or credential.",
1266
+ why: "Secrets committed to version control stay there even after they are removed from the current file, and can be found by anyone with repo access.",
1267
+ fix: "Remove the secret from version control and rotate it immediately."
1268
+ },
1269
+ CODSY061: {
1270
+ summary: "A file contains text shaped like a prompt-injection attempt.",
1271
+ why: "Context and skill files are read by AI agents; text written as an instruction to an agent can hijack its behavior.",
1272
+ fix: "Remove or rephrase the text so it describes the business rather than instructing an agent."
1273
+ },
1274
+ CODSY062: {
1275
+ summary: "A skill folder contains an executable script that is not on the allowlist.",
1276
+ why: "An unreviewed script inside a skill folder can run arbitrary code wherever that skill is used.",
1277
+ fix: "Add the script to allowedScripts in codsy.config.yaml once reviewed, or remove it."
1278
+ },
1279
+ CODSY080: {
1280
+ summary: "A file appears to contain real personal data, such as an email or phone number.",
1281
+ why: "Operational knowledge files are versioned and often public; real personal data does not belong there.",
1282
+ fix: "Replace real personal data with fictional sample data, or redact it."
1283
+ }
1284
+ };
1285
+
1286
+ // src/cli/explain.ts
1287
+ function runExplain(argv, write2) {
1288
+ const ruleId = argv[0];
1289
+ if (!ruleId) {
1290
+ write2("Usage: codsy explain <RULE_ID>\n");
1291
+ return 1;
1292
+ }
1293
+ const doc = RULE_DOCS[ruleId.toUpperCase()];
1294
+ if (!doc) {
1295
+ write2(
1296
+ `Unknown rule "${ruleId}". Run codsy check to see which rule ids apply to your project.
1297
+ `
1298
+ );
1299
+ return 1;
1300
+ }
1301
+ write2(
1302
+ `${ruleId.toUpperCase()} \u2014 ${doc.summary}
1303
+
1304
+ Why this matters: ${doc.why}
1305
+
1306
+ How to fix: ${doc.fix}
1307
+ `
1308
+ );
1309
+ return 0;
1310
+ }
1311
+
1312
+ // src/cli/new-skill.ts
1313
+ import { existsSync as existsSync3 } from "fs";
1314
+ import { mkdir as mkdir2, writeFile as writeFile2 } from "fs/promises";
1315
+ import { join as join5 } from "path";
1316
+ var NO_PROJECT_MESSAGE2 = "No Codsy project found here. Run codsy init to start one.\n";
1317
+ function readFlag(argv, flag) {
1318
+ const index = argv.indexOf(flag);
1319
+ if (index === -1) return void 0;
1320
+ return argv[index + 1];
1321
+ }
1322
+ function readFlagList(argv, flag) {
1323
+ const values = [];
1324
+ for (let i = 0; i < argv.length; i += 1) {
1325
+ if (argv[i] === flag) {
1326
+ const value = argv[i + 1];
1327
+ if (value !== void 0) values.push(value);
1328
+ }
1329
+ }
1330
+ return values;
1331
+ }
1332
+ function parseNewSkillFlags(argv) {
1333
+ return {
1334
+ name: readFlag(argv, "--name"),
1335
+ owner: readFlag(argv, "--owner"),
1336
+ purpose: readFlag(argv, "--purpose"),
1337
+ whenToUse: readFlag(argv, "--when-to-use"),
1338
+ input: readFlag(argv, "--input"),
1339
+ decisionLogic: readFlag(argv, "--decision-logic"),
1340
+ exceptions: readFlag(argv, "--exceptions"),
1341
+ output: readFlag(argv, "--output"),
1342
+ examples: readFlagList(argv, "--example")
1343
+ };
1344
+ }
1345
+ var REQUIRED_FLAG_HINT = "--name, --owner, --purpose, --when-to-use, --input, --decision-logic, --exceptions, --output, and at least two --example flags";
1346
+ function answersFromFlags(flags) {
1347
+ const missing = [];
1348
+ const requiredStringFields = [
1349
+ "name",
1350
+ "owner",
1351
+ "purpose",
1352
+ "whenToUse",
1353
+ "input",
1354
+ "decisionLogic",
1355
+ "exceptions",
1356
+ "output"
1357
+ ];
1358
+ for (const field of requiredStringFields) {
1359
+ if (!flags[field]) missing.push(field);
1360
+ }
1361
+ if (flags.examples.length < 2) missing.push("examples");
1362
+ if (missing.length > 0) return { answers: null, missing };
1363
+ const [first, second, ...rest] = flags.examples;
1364
+ return {
1365
+ answers: {
1366
+ name: flags.name,
1367
+ owner: flags.owner,
1368
+ purpose: flags.purpose,
1369
+ whenToUse: flags.whenToUse,
1370
+ input: flags.input,
1371
+ decisionLogic: flags.decisionLogic,
1372
+ exceptions: flags.exceptions,
1373
+ output: flags.output,
1374
+ examples: [first, second, ...rest]
1375
+ },
1376
+ missing: []
1377
+ };
1378
+ }
1379
+ async function writeSkillFiles(rootDir, answers) {
1380
+ const skillDir = join5(rootDir, "skills", answers.name);
1381
+ const evalsDir = join5(rootDir, "evals", answers.name);
1382
+ await mkdir2(skillDir, { recursive: true });
1383
+ await mkdir2(evalsDir, { recursive: true });
1384
+ await writeFile2(join5(skillDir, "SKILL.md"), buildSkillMarkdown(answers));
1385
+ await writeFile2(join5(evalsDir, "cases.yaml"), buildEvalsStub(answers.name));
1386
+ }
1387
+ async function runNewSkill(argv, options) {
1388
+ if (!existsSync3(join5(options.rootDir, "codsy.config.yaml"))) {
1389
+ options.write(NO_PROJECT_MESSAGE2);
1390
+ return 1;
1391
+ }
1392
+ const flags = parseNewSkillFlags(argv);
1393
+ const { answers, missing } = answersFromFlags(flags);
1394
+ let finalAnswers = answers;
1395
+ if (finalAnswers === null) {
1396
+ if (!options.isInteractive || !options.promptForAnswers) {
1397
+ options.write(`Missing: ${missing.join(", ")}. Pass ${REQUIRED_FLAG_HINT}.
1398
+ `);
1399
+ return 1;
1400
+ }
1401
+ finalAnswers = await options.promptForAnswers();
1402
+ if (finalAnswers === null) {
1403
+ options.write("Cancelled. No files were created.\n");
1404
+ return 1;
1405
+ }
1406
+ }
1407
+ await writeSkillFiles(options.rootDir, finalAnswers);
1408
+ options.write(
1409
+ `Created skills/${finalAnswers.name}/SKILL.md and an evals stub.
1410
+ Next: fill in real eval cases, then run codsy check.
1411
+ `
1412
+ );
1413
+ return 0;
1414
+ }
1415
+
1416
+ // src/cli/new-schema.ts
1417
+ import { existsSync as existsSync4 } from "fs";
1418
+ import { mkdir as mkdir3, writeFile as writeFile3 } from "fs/promises";
1419
+ import { join as join6 } from "path";
1420
+ var NO_PROJECT_MESSAGE3 = "No Codsy project found here. Run codsy init to start one.\n";
1421
+ var VALID_TYPES = ["string", "integer", "number", "boolean"];
1422
+ function isSchemaFieldType(value) {
1423
+ return VALID_TYPES.includes(value);
1424
+ }
1425
+ function readFlag2(argv, flag) {
1426
+ const index = argv.indexOf(flag);
1427
+ if (index === -1) return void 0;
1428
+ return argv[index + 1];
1429
+ }
1430
+ function readFields(argv) {
1431
+ const fields = [];
1432
+ for (let i = 0; i < argv.length; i += 1) {
1433
+ if (argv[i] !== "--field") continue;
1434
+ const raw = argv[i + 1];
1435
+ if (raw === void 0) continue;
1436
+ const [name, type, ...descriptionParts] = raw.split(":");
1437
+ if (!name || !type || !isSchemaFieldType(type) || descriptionParts.length === 0) continue;
1438
+ fields.push({ name, type, description: descriptionParts.join(":") });
1439
+ }
1440
+ return fields;
1441
+ }
1442
+ function parseNewSchemaFlags(argv) {
1443
+ return {
1444
+ name: readFlag2(argv, "--name"),
1445
+ description: readFlag2(argv, "--description"),
1446
+ fields: readFields(argv)
1447
+ };
1448
+ }
1449
+ var REQUIRED_FLAG_HINT2 = '--name, --description, and at least one --field "name:type:description" (type is one of string, integer, number, boolean)';
1450
+ function answersFromFlags2(flags) {
1451
+ const missing = [];
1452
+ if (!flags.name) missing.push("name");
1453
+ if (!flags.description) missing.push("description");
1454
+ if (flags.fields.length === 0) missing.push("fields");
1455
+ if (missing.length > 0) return { answers: null, missing };
1456
+ const [first, ...rest] = flags.fields;
1457
+ return {
1458
+ answers: { name: flags.name, description: flags.description, fields: [first, ...rest] },
1459
+ missing: []
1460
+ };
1461
+ }
1462
+ async function writeSchemaFile(rootDir, answers) {
1463
+ const schemasDir = join6(rootDir, "schemas");
1464
+ await mkdir3(schemasDir, { recursive: true });
1465
+ await writeFile3(join6(schemasDir, `${answers.name}.schema.json`), buildSchemaJson(answers));
1466
+ }
1467
+ async function runNewSchema(argv, options) {
1468
+ if (!existsSync4(join6(options.rootDir, "codsy.config.yaml"))) {
1469
+ options.write(NO_PROJECT_MESSAGE3);
1470
+ return 1;
1471
+ }
1472
+ const flags = parseNewSchemaFlags(argv);
1473
+ const { answers, missing } = answersFromFlags2(flags);
1474
+ let finalAnswers = answers;
1475
+ if (finalAnswers === null) {
1476
+ if (!options.isInteractive || !options.promptForAnswers) {
1477
+ options.write(`Missing: ${missing.join(", ")}. Pass ${REQUIRED_FLAG_HINT2}.
1478
+ `);
1479
+ return 1;
1480
+ }
1481
+ finalAnswers = await options.promptForAnswers();
1482
+ if (finalAnswers === null) {
1483
+ options.write("Cancelled. No files were created.\n");
1484
+ return 1;
1485
+ }
1486
+ }
1487
+ await writeSchemaFile(options.rootDir, finalAnswers);
1488
+ options.write(`Created schemas/${finalAnswers.name}.schema.json.
1489
+ `);
1490
+ return 0;
1491
+ }
1492
+
1493
+ // src/cli/eval.ts
1494
+ import { existsSync as existsSync5 } from "fs";
1495
+ import { join as join7 } from "path";
1496
+
1497
+ // src/core/eval/provider.ts
1498
+ var ProviderConfigError = class extends Error {
1499
+ };
1500
+
1501
+ // src/core/eval/anthropic-provider.ts
1502
+ var ANTHROPIC_API_VERSION = "2023-06-01";
1503
+ var ANTHROPIC_MESSAGES_URL = "https://api.anthropic.com/v1/messages";
1504
+ var MAX_TOKENS = 1024;
1505
+ var AnthropicProvider = class {
1506
+ constructor(model, apiKey, fetchImpl = fetch) {
1507
+ this.model = model;
1508
+ this.apiKey = apiKey;
1509
+ this.fetchImpl = fetchImpl;
1510
+ }
1511
+ model;
1512
+ apiKey;
1513
+ fetchImpl;
1514
+ async send(prompt) {
1515
+ if (!this.model) {
1516
+ throw new ProviderConfigError(
1517
+ 'The "anthropic" eval provider needs eval.model set in codsy.config.yaml.'
1518
+ );
1519
+ }
1520
+ if (!this.apiKey) {
1521
+ throw new ProviderConfigError(
1522
+ 'The "anthropic" eval provider needs ANTHROPIC_API_KEY set in the environment.'
1523
+ );
1524
+ }
1525
+ const response = await this.fetchImpl(ANTHROPIC_MESSAGES_URL, {
1526
+ method: "POST",
1527
+ headers: {
1528
+ "content-type": "application/json",
1529
+ "x-api-key": this.apiKey,
1530
+ "anthropic-version": ANTHROPIC_API_VERSION
1531
+ },
1532
+ body: JSON.stringify({
1533
+ model: this.model,
1534
+ max_tokens: MAX_TOKENS,
1535
+ messages: [{ role: "user", content: prompt }]
1536
+ })
1537
+ });
1538
+ if (!response.ok) {
1539
+ throw new Error(`Anthropic API request failed: ${response.status} ${await response.text()}`);
1540
+ }
1541
+ const data = await response.json();
1542
+ const text = data.content?.find((block) => block.type === "text")?.text;
1543
+ if (text === void 0) {
1544
+ throw new Error("Anthropic API response had no text content.");
1545
+ }
1546
+ return text;
1547
+ }
1548
+ };
1549
+
1550
+ // src/core/eval/command-provider.ts
1551
+ import { spawn } from "child_process";
1552
+ var CommandProvider = class {
1553
+ constructor(command) {
1554
+ this.command = command;
1555
+ }
1556
+ command;
1557
+ async send(prompt) {
1558
+ if (!this.command) {
1559
+ throw new ProviderConfigError(
1560
+ 'The "command" eval provider needs eval.command set in codsy.config.yaml.'
1561
+ );
1562
+ }
1563
+ return new Promise((resolve2, reject) => {
1564
+ const child = spawn(this.command, { shell: true });
1565
+ let stdout = "";
1566
+ let stderr = "";
1567
+ child.stdout.on("data", (chunk) => {
1568
+ stdout += chunk.toString("utf8");
1569
+ });
1570
+ child.stderr.on("data", (chunk) => {
1571
+ stderr += chunk.toString("utf8");
1572
+ });
1573
+ child.on("error", reject);
1574
+ child.on("close", (code) => {
1575
+ if (code !== 0) {
1576
+ reject(new Error(`eval.command exited with code ${code}: ${stderr}`));
1577
+ } else {
1578
+ resolve2(stdout);
1579
+ }
1580
+ });
1581
+ child.stdin.write(prompt);
1582
+ child.stdin.end();
1583
+ });
1584
+ }
1585
+ };
1586
+
1587
+ // src/core/eval/openai-compatible-provider.ts
1588
+ var OpenAiCompatibleProvider = class {
1589
+ constructor(model, baseUrl, apiKey, fetchImpl = fetch) {
1590
+ this.model = model;
1591
+ this.baseUrl = baseUrl;
1592
+ this.apiKey = apiKey;
1593
+ this.fetchImpl = fetchImpl;
1594
+ }
1595
+ model;
1596
+ baseUrl;
1597
+ apiKey;
1598
+ fetchImpl;
1599
+ async send(prompt) {
1600
+ if (!this.model) {
1601
+ throw new ProviderConfigError(
1602
+ 'The "openai-compatible" eval provider needs eval.model set in codsy.config.yaml.'
1603
+ );
1604
+ }
1605
+ if (!this.apiKey) {
1606
+ throw new ProviderConfigError(
1607
+ 'The "openai-compatible" eval provider needs OPENAI_API_KEY set in the environment.'
1608
+ );
1609
+ }
1610
+ const response = await this.fetchImpl(`${this.baseUrl}/chat/completions`, {
1611
+ method: "POST",
1612
+ headers: {
1613
+ "content-type": "application/json",
1614
+ authorization: `Bearer ${this.apiKey}`
1615
+ },
1616
+ body: JSON.stringify({
1617
+ model: this.model,
1618
+ messages: [{ role: "user", content: prompt }]
1619
+ })
1620
+ });
1621
+ if (!response.ok) {
1622
+ throw new Error(
1623
+ `OpenAI-compatible API request failed: ${response.status} ${await response.text()}`
1624
+ );
1625
+ }
1626
+ const data = await response.json();
1627
+ const text = data.choices?.[0]?.message?.content;
1628
+ if (text === void 0) {
1629
+ throw new Error("OpenAI-compatible API response had no message content.");
1630
+ }
1631
+ return text;
1632
+ }
1633
+ };
1634
+
1635
+ // src/core/eval/create-provider.ts
1636
+ function createProvider(config, env = process.env) {
1637
+ if (config.provider === "anthropic") {
1638
+ return new AnthropicProvider(config.model, env.ANTHROPIC_API_KEY);
1639
+ }
1640
+ if (config.provider === "openai-compatible") {
1641
+ return new OpenAiCompatibleProvider(config.model, config.baseUrl, env.OPENAI_API_KEY);
1642
+ }
1643
+ return new CommandProvider(config.command);
1644
+ }
1645
+
1646
+ // src/core/eval/build-prompt.ts
1647
+ function buildPrompt(skill, schemas, contextFiles, input) {
1648
+ const parts = [];
1649
+ parts.push("You are deciding a real business decision using the rules below.");
1650
+ parts.push("Respond with a single JSON object and nothing else: no prose, no markdown fences.");
1651
+ parts.push("");
1652
+ for (const contextFile of contextFiles) {
1653
+ parts.push(`## Context: ${contextFile.name}`, "", contextFile.raw.trim(), "");
1654
+ }
1655
+ for (const schema of schemas) {
1656
+ parts.push(`## Schema: ${schema.name}`, "", schema.raw.trim(), "");
1657
+ }
1658
+ parts.push("## Skill", "", skill.raw.trim(), "");
1659
+ parts.push("## Input", "", JSON.stringify(input, null, 2), "");
1660
+ parts.push("## Your answer (JSON object only)");
1661
+ return parts.join("\n");
1662
+ }
1663
+
1664
+ // src/core/eval/parse-answer.ts
1665
+ var FENCED_JSON_PATTERN = /```(?:json)?\s*([\s\S]*?)```/i;
1666
+ function parseAnswer(rawResponse) {
1667
+ const fenced = FENCED_JSON_PATTERN.exec(rawResponse);
1668
+ const candidate = fenced ? fenced[1] : rawResponse;
1669
+ if (candidate === void 0) return null;
1670
+ try {
1671
+ const parsed = JSON.parse(candidate.trim());
1672
+ return isRecord(parsed) ? parsed : null;
1673
+ } catch {
1674
+ return null;
1675
+ }
1676
+ }
1677
+
1678
+ // src/core/eval/run-eval.ts
1679
+ function deepEqual(a, b) {
1680
+ if (a === b) return true;
1681
+ if (typeof a !== typeof b) return false;
1682
+ if (a === null || b === null) return false;
1683
+ if (typeof a !== "object") return false;
1684
+ if (Array.isArray(a) || Array.isArray(b)) {
1685
+ if (!Array.isArray(a) || !Array.isArray(b)) return false;
1686
+ return a.length === b.length && a.every((item, index) => deepEqual(item, b[index]));
1687
+ }
1688
+ const aRecord = a;
1689
+ const bRecord = b;
1690
+ const keys = Object.keys(aRecord);
1691
+ return keys.length === Object.keys(bRecord).length && keys.every((key) => deepEqual(aRecord[key], bRecord[key]));
1692
+ }
1693
+ function matchesExpected(expected, actual) {
1694
+ if (expected === null || typeof expected !== "object" || Array.isArray(expected)) {
1695
+ return false;
1696
+ }
1697
+ return Object.entries(expected).every(
1698
+ ([key, value]) => deepEqual(value, actual[key])
1699
+ );
1700
+ }
1701
+ async function evalCase(skill, schemas, contextFiles, evalCaseData, provider) {
1702
+ const prompt = buildPrompt(skill, schemas, contextFiles, evalCaseData.input);
1703
+ try {
1704
+ const rawResponse = await provider.send(prompt);
1705
+ const actual = parseAnswer(rawResponse);
1706
+ const passed = actual !== null && matchesExpected(evalCaseData.expected, actual);
1707
+ return {
1708
+ id: evalCaseData.id,
1709
+ prompt,
1710
+ passed,
1711
+ expected: evalCaseData.expected,
1712
+ actual,
1713
+ rawResponse,
1714
+ error: null
1715
+ };
1716
+ } catch (error) {
1717
+ return {
1718
+ id: evalCaseData.id,
1719
+ prompt,
1720
+ passed: false,
1721
+ expected: evalCaseData.expected,
1722
+ actual: null,
1723
+ rawResponse: null,
1724
+ error: error instanceof Error ? error.message : String(error)
1725
+ };
1726
+ }
1727
+ }
1728
+ async function evalSkill(skill, schemas, contextFiles, cases, provider) {
1729
+ const caseResults = await Promise.all(
1730
+ cases.map((evalCaseData) => evalCase(skill, schemas, contextFiles, evalCaseData, provider))
1731
+ );
1732
+ const passedCount = caseResults.filter((result) => result.passed).length;
1733
+ const accuracy = cases.length === 0 ? 1 : passedCount / cases.length;
1734
+ return { skillName: skill.dirName, caseResults, accuracy };
1735
+ }
1736
+
1737
+ // src/cli/eval.ts
1738
+ var NO_PROJECT_MESSAGE4 = "No Codsy project found here. Run codsy init to start one.\n";
1739
+ function parseEvalArgs(argv) {
1740
+ let skillName = null;
1741
+ let threshold = null;
1742
+ let dryRun = false;
1743
+ let invalidThreshold = null;
1744
+ for (let i = 0; i < argv.length; i += 1) {
1745
+ const arg = argv[i];
1746
+ if (arg === "--dry-run") {
1747
+ dryRun = true;
1748
+ } else if (arg === "--threshold") {
1749
+ const value = argv[i + 1];
1750
+ const parsed = value === void 0 ? NaN : Number(value);
1751
+ if (Number.isFinite(parsed)) {
1752
+ threshold = parsed;
1753
+ } else {
1754
+ invalidThreshold = value ?? "";
1755
+ }
1756
+ i += 1;
1757
+ } else if (arg !== void 0 && !arg.startsWith("--") && skillName === null) {
1758
+ skillName = arg;
1759
+ }
1760
+ }
1761
+ return { skillName, threshold, dryRun, invalidThreshold };
1762
+ }
1763
+ function schemasForSkill(skill, project) {
1764
+ const names = new Set(
1765
+ Array.isArray(skill.frontmatter.metadata?.schemas) ? skill.frontmatter.metadata.schemas : []
1766
+ );
1767
+ return project.schemas.filter((schema) => names.has(schema.name));
1768
+ }
1769
+ function skillsToEvaluate(project, skillName) {
1770
+ if (skillName) {
1771
+ return project.skills.filter((skill) => skill.dirName === skillName);
1772
+ }
1773
+ return project.skills.filter(
1774
+ (skill) => project.evalSuites.some((suite) => suite.skill === skill.dirName && suite.cases.length > 0)
1775
+ );
1776
+ }
1777
+ function accuracyBar(accuracy, width = 20) {
1778
+ const filled = Math.round(accuracy * width);
1779
+ return "\u2588".repeat(filled) + "\u2591".repeat(width - filled);
1780
+ }
1781
+ function formatEvalReport(results, threshold) {
1782
+ if (results.length === 0) {
1783
+ return "No skills with eval cases were found.\n";
1784
+ }
1785
+ const lines = [];
1786
+ for (const result of results) {
1787
+ const percent = Math.round(result.accuracy * 100);
1788
+ lines.push(`${result.skillName}: ${accuracyBar(result.accuracy)} ${percent}%`);
1789
+ for (const caseResult of result.caseResults.filter((c) => !c.passed)) {
1790
+ lines.push(` FAIL ${caseResult.id}`);
1791
+ lines.push(` expected: ${JSON.stringify(caseResult.expected)}`);
1792
+ lines.push(` actual: ${JSON.stringify(caseResult.actual)}`);
1793
+ if (caseResult.error) lines.push(` error: ${caseResult.error}`);
1794
+ }
1795
+ }
1796
+ lines.push("");
1797
+ const belowThreshold = results.filter((r) => r.accuracy < threshold);
1798
+ if (belowThreshold.length > 0) {
1799
+ lines.push(
1800
+ `${belowThreshold.length} skill(s) scored below the ${Math.round(threshold * 100)}% threshold.`
1801
+ );
1802
+ } else {
1803
+ lines.push(`Every skill meets the ${Math.round(threshold * 100)}% threshold.`);
1804
+ }
1805
+ return `${lines.join("\n")}
1806
+ `;
1807
+ }
1808
+ function formatDryRunPrompts(skills, project) {
1809
+ const lines = [];
1810
+ for (const skill of skills) {
1811
+ const suite = project.evalSuites.find((s) => s.skill === skill.dirName);
1812
+ const schemas = schemasForSkill(skill, project);
1813
+ for (const caseData of suite?.cases ?? []) {
1814
+ lines.push(`--- ${skill.dirName} / ${caseData.id} ---`);
1815
+ lines.push(buildPrompt(skill, schemas, project.contextFiles, caseData.input));
1816
+ lines.push("");
1817
+ }
1818
+ }
1819
+ return `${lines.join("\n")}
1820
+ `;
1821
+ }
1822
+ async function runEvalCommand(argv, options) {
1823
+ const { skillName, threshold, dryRun, invalidThreshold } = parseEvalArgs(argv);
1824
+ if (invalidThreshold !== null) {
1825
+ options.write(`Invalid threshold "${invalidThreshold}". Pass a number like 0.8.
1826
+ `);
1827
+ return 1;
1828
+ }
1829
+ if (!existsSync5(join7(options.rootDir, "codsy.config.yaml"))) {
1830
+ options.write(NO_PROJECT_MESSAGE4);
1831
+ return 1;
1832
+ }
1833
+ const config = await loadConfig(options.rootDir);
1834
+ const project = await loadProject(options.rootDir);
1835
+ const effectiveThreshold = threshold ?? config.eval.threshold;
1836
+ const skills = skillsToEvaluate(project, skillName);
1837
+ if (skills.length === 0) {
1838
+ options.write(
1839
+ skillName ? `No skill named "${skillName}" with eval cases was found.
1840
+ ` : "No skills with eval cases were found.\n"
1841
+ );
1842
+ return 1;
1843
+ }
1844
+ if (dryRun) {
1845
+ options.write(formatDryRunPrompts(skills, project));
1846
+ return 0;
1847
+ }
1848
+ const provider = createProvider(config.eval);
1849
+ const results = [];
1850
+ for (const skill of skills) {
1851
+ const suite = project.evalSuites.find((s) => s.skill === skill.dirName);
1852
+ const schemas = schemasForSkill(skill, project);
1853
+ results.push(
1854
+ await evalSkill(skill, schemas, project.contextFiles, suite?.cases ?? [], provider)
1855
+ );
1856
+ }
1857
+ options.write(formatEvalReport(results, effectiveThreshold));
1858
+ return results.some((r) => r.accuracy < effectiveThreshold) ? 1 : 0;
1859
+ }
1860
+
1861
+ // src/cli/friendly-error.ts
1862
+ function formatFriendlyError(error, retryCommand) {
1863
+ const message = error instanceof Error ? error.message : String(error);
1864
+ return [
1865
+ "Codsy ran into a problem and stopped.",
1866
+ "",
1867
+ `What happened: ${message}`,
1868
+ "",
1869
+ "How to fix: check the message above, fix the underlying file or configuration, then try again.",
1870
+ "",
1871
+ `Retry with: ${retryCommand}`,
1872
+ ""
1873
+ ].join("\n");
1874
+ }
1875
+
1876
+ // src/cli/help.ts
1877
+ var HELP_FLAGS = ["--help", "-h"];
1878
+ function isHelpRequest(argv) {
1879
+ return argv[0] === "help" || argv.some((arg) => HELP_FLAGS.includes(arg));
1880
+ }
1881
+ var HELP_TEXT = `Codsy helps you write operational knowledge as versioned files people and AI agents can both read.
1882
+
1883
+ Usage: codsy <command> [options]
1884
+
1885
+ Commands:
1886
+ codsy Show the home screen
1887
+ codsy init [folder] Start a new project
1888
+ --template <name> Use a template: ${TEMPLATE_NAMES.join(", ")}
1889
+ codsy new skill Add a skill (asks questions, or pass --name, --owner, --purpose, ...)
1890
+ codsy new schema Add a schema (asks questions, or pass --name, --description, --field)
1891
+ codsy check Check your project for problems
1892
+ --strict Treat skills without enough evals as errors
1893
+ --format <format> Output as pretty, json, or sarif
1894
+ codsy explain <RULE_ID> Explain a rule, for example codsy explain CODSY010
1895
+ codsy eval [skill] Test skills against their examples with an AI model
1896
+ --dry-run Show what would run without calling the model
1897
+ --threshold <number> Minimum passing score
1898
+
1899
+ Options:
1900
+ --plain Plain text, no colors or animation
1901
+ -v, --version Show the installed version
1902
+ -h, --help Show this help
1903
+ `;
1904
+
1905
+ // src/cli/index.ts
1906
+ var HELP_MESSAGE = "Codsy is set up. Run codsy --version to see the installed version.";
1907
+ var KNOWN_COMMANDS = ["init", "check", "new", "explain", "eval"];
1908
+ function run(argv) {
1909
+ if (argv.includes("--version") || argv.includes("-v")) {
1910
+ return package_default.version;
1911
+ }
1912
+ return HELP_MESSAGE;
1913
+ }
1914
+ function isInteractive(argv) {
1915
+ return Boolean(process.stdout.isTTY) && !argv.includes("--plain");
1916
+ }
1917
+ function write(text) {
1918
+ process.stdout.write(text);
1919
+ }
1920
+ async function main() {
1921
+ const argv = process.argv.slice(2);
1922
+ try {
1923
+ await dispatch(argv);
1924
+ } catch (error) {
1925
+ process.stderr.write(formatFriendlyError(error, `codsy ${argv.join(" ")}`.trim()));
1926
+ process.exitCode = 1;
1927
+ }
1928
+ }
1929
+ async function dispatch(argv) {
1930
+ const rootDir = process.cwd();
1931
+ if (isHelpRequest(argv)) {
1932
+ write(HELP_TEXT);
1933
+ return;
1934
+ }
1935
+ if (argv.length === 0) {
1936
+ const interactive = isInteractive(argv);
1937
+ process.exitCode = await runHomeCommand({
1938
+ rootDir,
1939
+ write,
1940
+ isInteractive: interactive,
1941
+ renderRichHome: interactive ? async (status) => {
1942
+ const { renderHomeScreen } = await import("./home-screen-UIUS4UOT.js");
1943
+ return renderHomeScreen(status);
1944
+ } : void 0
1945
+ });
1946
+ return;
1947
+ }
1948
+ if (argv[0] === "init") {
1949
+ const templatesDir = join8(findPackageRoot(import.meta.url), "templates");
1950
+ const interactive = isInteractive(argv);
1951
+ process.exitCode = await runInit(argv.slice(1), {
1952
+ templatesDir,
1953
+ write,
1954
+ isInteractive: interactive,
1955
+ promptForTemplate: interactive ? async () => {
1956
+ const { promptForTemplate } = await import("./init-wizard-MXAFTYCQ.js");
1957
+ return promptForTemplate();
1958
+ } : void 0
1959
+ });
1960
+ return;
1961
+ }
1962
+ if (argv[0] === "check") {
1963
+ const interactive = isInteractive(argv);
1964
+ process.exitCode = await runCheckCommand(argv.slice(1), {
1965
+ rootDir,
1966
+ write,
1967
+ isInteractive: interactive,
1968
+ renderRichReport: interactive ? async (diagnostics) => {
1969
+ const { renderCheckScreen } = await import("./check-screen-OZJD7UHU.js");
1970
+ renderCheckScreen(diagnostics);
1971
+ } : void 0
1972
+ });
1973
+ return;
1974
+ }
1975
+ if (argv[0] === "explain") {
1976
+ process.exitCode = runExplain(argv.slice(1), write);
1977
+ return;
1978
+ }
1979
+ if (argv[0] === "eval") {
1980
+ process.exitCode = await runEvalCommand(argv.slice(1), { rootDir, write });
1981
+ return;
1982
+ }
1983
+ if (argv[0] === "new") {
1984
+ const interactive = isInteractive(argv);
1985
+ const subcommand = argv[1];
1986
+ const rest = argv.slice(2);
1987
+ if (subcommand === "skill") {
1988
+ process.exitCode = await runNewSkill(rest, {
1989
+ rootDir,
1990
+ write,
1991
+ isInteractive: interactive,
1992
+ promptForAnswers: interactive ? async () => {
1993
+ const { promptForSkillAnswers } = await import("./new-skill-wizard-AZJ5DLDM.js");
1994
+ return promptForSkillAnswers();
1995
+ } : void 0
1996
+ });
1997
+ } else if (subcommand === "schema") {
1998
+ process.exitCode = await runNewSchema(rest, {
1999
+ rootDir,
2000
+ write,
2001
+ isInteractive: interactive,
2002
+ promptForAnswers: interactive ? async () => {
2003
+ const { promptForSchemaAnswers } = await import("./new-schema-wizard-NYIHF47J.js");
2004
+ return promptForSchemaAnswers();
2005
+ } : void 0
2006
+ });
2007
+ } else {
2008
+ write("Usage: codsy new <skill|schema>\n");
2009
+ process.exitCode = 1;
2010
+ }
2011
+ return;
2012
+ }
2013
+ if (!argv.includes("--version") && !argv.includes("-v") && !KNOWN_COMMANDS.includes(argv[0] ?? "")) {
2014
+ write(`Unknown command "${argv[0]}". Run codsy --help to see what you can do.
2015
+ `);
2016
+ process.exitCode = 1;
2017
+ return;
2018
+ }
2019
+ write(`${run(argv)}
2020
+ `);
2021
+ }
2022
+ function isDirectInvocation(argv1, moduleUrl, resolveRealPath = realpathSync) {
2023
+ return argv1 !== void 0 && resolveRealPath(argv1) === fileURLToPath2(moduleUrl);
2024
+ }
2025
+ if (isDirectInvocation(process.argv[1], import.meta.url)) {
2026
+ await main();
2027
+ }
2028
+ export {
2029
+ isDirectInvocation,
2030
+ run
2031
+ };
2032
+ //# sourceMappingURL=cli.js.map