@vibecheckai/cli 3.3.0 → 3.5.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 (170) hide show
  1. package/bin/registry.js +389 -269
  2. package/bin/runners/cli-utils.js +2 -33
  3. package/bin/runners/context/generators/cursor.js +49 -2
  4. package/bin/runners/lib/agent-firewall/learning/learning-engine.js +849 -0
  5. package/bin/runners/lib/analyzers.js +599 -142
  6. package/bin/runners/lib/audit-logger.js +532 -0
  7. package/bin/runners/lib/authority/authorities/architecture.js +364 -0
  8. package/bin/runners/lib/authority/authorities/compliance.js +341 -0
  9. package/bin/runners/lib/authority/authorities/human.js +343 -0
  10. package/bin/runners/lib/authority/authorities/quality.js +420 -0
  11. package/bin/runners/lib/authority/authorities/security.js +228 -0
  12. package/bin/runners/lib/authority/index.js +293 -0
  13. package/bin/runners/lib/authority-badge.js +425 -425
  14. package/bin/runners/lib/bundle/bundle-intelligence.js +846 -0
  15. package/bin/runners/lib/cli-charts.js +368 -0
  16. package/bin/runners/lib/cli-config-display.js +405 -0
  17. package/bin/runners/lib/cli-demo.js +275 -0
  18. package/bin/runners/lib/cli-errors.js +438 -0
  19. package/bin/runners/lib/cli-help-formatter.js +439 -0
  20. package/bin/runners/lib/cli-interactive-menu.js +509 -0
  21. package/bin/runners/lib/cli-prompts.js +441 -0
  22. package/bin/runners/lib/cli-scan-cards.js +362 -0
  23. package/bin/runners/lib/compliance-reporter.js +710 -0
  24. package/bin/runners/lib/conductor/index.js +671 -0
  25. package/bin/runners/lib/easy/README.md +123 -0
  26. package/bin/runners/lib/easy/index.js +140 -0
  27. package/bin/runners/lib/easy/interactive-wizard.js +788 -0
  28. package/bin/runners/lib/easy/one-click-firewall.js +564 -0
  29. package/bin/runners/lib/easy/zero-config-reality.js +714 -0
  30. package/bin/runners/lib/engines/accessibility-engine.js +218 -18
  31. package/bin/runners/lib/engines/api-consistency-engine.js +335 -30
  32. package/bin/runners/lib/engines/async-patterns-engine.js +444 -0
  33. package/bin/runners/lib/engines/bundle-size-engine.js +433 -0
  34. package/bin/runners/lib/engines/confidence-scoring.js +276 -0
  35. package/bin/runners/lib/engines/context-detection.js +264 -0
  36. package/bin/runners/lib/engines/cross-file-analysis-engine.js +292 -27
  37. package/bin/runners/lib/engines/database-patterns-engine.js +429 -0
  38. package/bin/runners/lib/engines/duplicate-code-engine.js +354 -0
  39. package/bin/runners/lib/engines/empty-catch-engine.js +127 -17
  40. package/bin/runners/lib/engines/env-variables-engine.js +458 -0
  41. package/bin/runners/lib/engines/error-handling-engine.js +437 -0
  42. package/bin/runners/lib/engines/false-positive-prevention.js +630 -0
  43. package/bin/runners/lib/engines/framework-adapters/index.js +607 -0
  44. package/bin/runners/lib/engines/framework-detection.js +508 -0
  45. package/bin/runners/lib/engines/import-order-engine.js +429 -0
  46. package/bin/runners/lib/engines/mock-data-engine.js +53 -10
  47. package/bin/runners/lib/engines/naming-conventions-engine.js +544 -0
  48. package/bin/runners/lib/engines/noise-reduction-engine.js +452 -0
  49. package/bin/runners/lib/engines/orchestrator.js +334 -0
  50. package/bin/runners/lib/engines/performance-issues-engine.js +176 -36
  51. package/bin/runners/lib/engines/react-patterns-engine.js +457 -0
  52. package/bin/runners/lib/engines/security-vulnerabilities-engine.js +382 -54
  53. package/bin/runners/lib/engines/type-aware-engine.js +263 -39
  54. package/bin/runners/lib/engines/vibecheck-engines/index.js +122 -13
  55. package/bin/runners/lib/engines/vibecheck-engines/lib/ai-hallucination-engine.js +806 -0
  56. package/bin/runners/lib/engines/vibecheck-engines/lib/hardcoded-secrets-engine.js +373 -73
  57. package/bin/runners/lib/engines/vibecheck-engines/lib/smart-fix-engine.js +577 -0
  58. package/bin/runners/lib/engines/vibecheck-engines/lib/vibe-score-engine.js +543 -0
  59. package/bin/runners/lib/engines/vibecheck-engines.js +514 -0
  60. package/bin/runners/lib/enhanced-features/index.js +305 -0
  61. package/bin/runners/lib/enhanced-output.js +631 -0
  62. package/bin/runners/lib/enterprise.js +300 -0
  63. package/bin/runners/lib/entitlements-v2.js +161 -478
  64. package/bin/runners/lib/firewall/command-validator.js +351 -0
  65. package/bin/runners/lib/firewall/config.js +341 -0
  66. package/bin/runners/lib/firewall/content-validator.js +519 -0
  67. package/bin/runners/lib/firewall/index.js +101 -0
  68. package/bin/runners/lib/firewall/path-validator.js +256 -0
  69. package/bin/runners/lib/html-proof-report.js +350 -700
  70. package/bin/runners/lib/intelligence/cross-repo-intelligence.js +817 -0
  71. package/bin/runners/lib/mcp-utils.js +425 -0
  72. package/bin/runners/lib/missions/plan.js +46 -6
  73. package/bin/runners/lib/missions/templates.js +232 -0
  74. package/bin/runners/lib/output/index.js +1022 -0
  75. package/bin/runners/lib/policy-engine.js +652 -0
  76. package/bin/runners/lib/polish/autofix/accessibility-fixes.js +333 -0
  77. package/bin/runners/lib/polish/autofix/async-handlers.js +273 -0
  78. package/bin/runners/lib/polish/autofix/dead-code.js +280 -0
  79. package/bin/runners/lib/polish/autofix/imports-optimizer.js +344 -0
  80. package/bin/runners/lib/polish/autofix/index.js +200 -0
  81. package/bin/runners/lib/polish/autofix/remove-consoles.js +209 -0
  82. package/bin/runners/lib/polish/autofix/strengthen-types.js +245 -0
  83. package/bin/runners/lib/polish/backend-checks.js +148 -0
  84. package/bin/runners/lib/polish/documentation-checks.js +111 -0
  85. package/bin/runners/lib/polish/frontend-checks.js +168 -0
  86. package/bin/runners/lib/polish/index.js +71 -0
  87. package/bin/runners/lib/polish/infrastructure-checks.js +131 -0
  88. package/bin/runners/lib/polish/library-detection.js +175 -0
  89. package/bin/runners/lib/polish/performance-checks.js +100 -0
  90. package/bin/runners/lib/polish/security-checks.js +148 -0
  91. package/bin/runners/lib/polish/utils.js +203 -0
  92. package/bin/runners/lib/prompt-builder.js +540 -0
  93. package/bin/runners/lib/proof-certificate.js +634 -0
  94. package/bin/runners/lib/reality/accessibility-audit.js +946 -0
  95. package/bin/runners/lib/reality/api-contract-validator.js +1012 -0
  96. package/bin/runners/lib/reality/chaos-engineering.js +1084 -0
  97. package/bin/runners/lib/reality/performance-tracker.js +1077 -0
  98. package/bin/runners/lib/reality/scenario-generator.js +1404 -0
  99. package/bin/runners/lib/reality/visual-regression.js +852 -0
  100. package/bin/runners/lib/reality-profiler.js +717 -0
  101. package/bin/runners/lib/replay/flight-recorder-viewer.js +1160 -0
  102. package/bin/runners/lib/review/ai-code-review.js +832 -0
  103. package/bin/runners/lib/rules/custom-rule-engine.js +985 -0
  104. package/bin/runners/lib/sbom-generator.js +641 -0
  105. package/bin/runners/lib/scan-output-enhanced.js +512 -0
  106. package/bin/runners/lib/scan-output.js +65 -19
  107. package/bin/runners/lib/security/owasp-scanner.js +939 -0
  108. package/bin/runners/lib/ship-output.js +18 -25
  109. package/bin/runners/lib/terminal-ui.js +113 -1
  110. package/bin/runners/lib/unified-cli-output.js +603 -430
  111. package/bin/runners/lib/upsell.js +90 -338
  112. package/bin/runners/lib/validators/contract-validator.js +283 -0
  113. package/bin/runners/lib/validators/dead-export-detector.js +279 -0
  114. package/bin/runners/lib/validators/dep-audit.js +245 -0
  115. package/bin/runners/lib/validators/env-validator.js +319 -0
  116. package/bin/runners/lib/validators/index.js +120 -0
  117. package/bin/runners/lib/validators/license-checker.js +252 -0
  118. package/bin/runners/lib/validators/route-validator.js +290 -0
  119. package/bin/runners/runAIAgent.js +5 -10
  120. package/bin/runners/runAgent.js +3 -0
  121. package/bin/runners/runApprove.js +1233 -1200
  122. package/bin/runners/runAuth.js +22 -1
  123. package/bin/runners/runAuthority.js +528 -0
  124. package/bin/runners/runCheckpoint.js +4 -24
  125. package/bin/runners/runClassify.js +862 -859
  126. package/bin/runners/runConductor.js +772 -0
  127. package/bin/runners/runContainer.js +366 -0
  128. package/bin/runners/runContext.js +3 -0
  129. package/bin/runners/runDoctor.js +28 -41
  130. package/bin/runners/runEasy.js +410 -0
  131. package/bin/runners/runFirewall.js +3 -0
  132. package/bin/runners/runFirewallHook.js +3 -0
  133. package/bin/runners/runFix.js +76 -66
  134. package/bin/runners/runGuard.js +411 -18
  135. package/bin/runners/runIaC.js +372 -0
  136. package/bin/runners/runInit.js +10 -60
  137. package/bin/runners/runMcp.js +11 -12
  138. package/bin/runners/runPolish.js +240 -64
  139. package/bin/runners/runPromptFirewall.js +5 -12
  140. package/bin/runners/runProve.js +20 -55
  141. package/bin/runners/runReality.js +68 -59
  142. package/bin/runners/runReport.js +31 -5
  143. package/bin/runners/runRuntime.js +5 -8
  144. package/bin/runners/runScan.js +194 -1273
  145. package/bin/runners/runShip.js +695 -47
  146. package/bin/runners/runTruth.js +3 -0
  147. package/bin/runners/runValidate.js +7 -11
  148. package/bin/runners/runVibe.js +791 -0
  149. package/bin/runners/runWatch.js +14 -23
  150. package/bin/vibecheck.js +179 -65
  151. package/mcp-server/index.js +202 -636
  152. package/mcp-server/lib/api-client.cjs +7 -299
  153. package/mcp-server/package.json +1 -1
  154. package/mcp-server/tier-auth.js +175 -574
  155. package/mcp-server/tools-v3.js +800 -505
  156. package/mcp-server/tools.js +495 -0
  157. package/package.json +1 -1
  158. package/bin/runners/lib/engines/vibecheck-engines/lib/ast-cache.js +0 -164
  159. package/bin/runners/lib/engines/vibecheck-engines/lib/code-quality-engine.js +0 -291
  160. package/bin/runners/lib/engines/vibecheck-engines/lib/console-logs-engine.js +0 -83
  161. package/bin/runners/lib/engines/vibecheck-engines/lib/dead-code-engine.js +0 -198
  162. package/bin/runners/lib/engines/vibecheck-engines/lib/deprecated-api-engine.js +0 -275
  163. package/bin/runners/lib/engines/vibecheck-engines/lib/empty-catch-engine.js +0 -167
  164. package/bin/runners/lib/engines/vibecheck-engines/lib/file-filter.js +0 -217
  165. package/bin/runners/lib/engines/vibecheck-engines/lib/mock-data-engine.js +0 -140
  166. package/bin/runners/lib/engines/vibecheck-engines/lib/parallel-processor.js +0 -164
  167. package/bin/runners/lib/engines/vibecheck-engines/lib/performance-issues-engine.js +0 -234
  168. package/bin/runners/lib/engines/vibecheck-engines/lib/type-aware-engine.js +0 -217
  169. package/bin/runners/lib/engines/vibecheck-engines/lib/unsafe-regex-engine.js +0 -78
  170. package/mcp-server/index-v1.js +0 -698
@@ -0,0 +1,364 @@
1
+ /**
2
+ * Architecture Authority - Automated architecture review
3
+ *
4
+ * Checks for:
5
+ * - Import/export patterns
6
+ * - Circular dependencies
7
+ * - Layer violations
8
+ * - Naming conventions
9
+ * - File organization
10
+ */
11
+
12
+ "use strict";
13
+
14
+ const crypto = require("crypto");
15
+ const path = require("path");
16
+
17
+ class ArchitectureAuthority {
18
+ static description = "Architecture review (patterns, structure)";
19
+ static automated = true;
20
+ static tier = "pro";
21
+
22
+ constructor(options = {}) {
23
+ this.options = options;
24
+ this.rules = this._initializeRules();
25
+ }
26
+
27
+ _initializeRules() {
28
+ return {
29
+ layerViolations: [
30
+ {
31
+ name: "ui-imports-db",
32
+ pattern: /from\s+['"].*\/(database|db|prisma|models)\//,
33
+ context: /(components|pages|views|ui)\//,
34
+ message: "UI layer importing directly from database layer",
35
+ severity: "high",
36
+ },
37
+ {
38
+ name: "model-imports-ui",
39
+ pattern: /from\s+['"].*\/(components|views|pages)\//,
40
+ context: /(models|entities|database)\//,
41
+ message: "Data layer importing from UI layer",
42
+ severity: "high",
43
+ },
44
+ ],
45
+ circularDependencies: {
46
+ enabled: true,
47
+ severity: "medium",
48
+ },
49
+ namingConventions: [
50
+ {
51
+ name: "component-naming",
52
+ pattern: /^[A-Z][a-zA-Z0-9]*\.(tsx|jsx)$/,
53
+ context: /components\//,
54
+ message: "Component files should use PascalCase",
55
+ severity: "low",
56
+ },
57
+ {
58
+ name: "hook-naming",
59
+ pattern: /^use[A-Z][a-zA-Z0-9]*\.(ts|tsx)$/,
60
+ context: /hooks\//,
61
+ message: "Hook files should start with 'use' prefix",
62
+ severity: "low",
63
+ },
64
+ {
65
+ name: "util-naming",
66
+ pattern: /^[a-z][a-zA-Z0-9-]*\.(ts|js)$/,
67
+ context: /(utils?|lib|helpers)\//,
68
+ message: "Utility files should use camelCase or kebab-case",
69
+ severity: "low",
70
+ },
71
+ ],
72
+ importPatterns: [
73
+ {
74
+ name: "relative-import-depth",
75
+ pattern: /from\s+['"]\.\.\/\.\.\/\.\.\/\.\.\//,
76
+ message: "Import depth exceeds 3 levels - consider using path aliases",
77
+ severity: "medium",
78
+ },
79
+ {
80
+ name: "index-barrel-bloat",
81
+ pattern: /export\s+\*\s+from/,
82
+ message: "Barrel exports can cause bundle bloat - verify tree-shaking",
83
+ severity: "low",
84
+ },
85
+ {
86
+ name: "default-export-missing",
87
+ pattern: /^(?!.*export\s+default).*\.tsx$/m,
88
+ context: /(pages|app)\//,
89
+ message: "Page/App files typically need default export",
90
+ severity: "medium",
91
+ },
92
+ ],
93
+ fileOrganization: [
94
+ {
95
+ name: "test-colocation",
96
+ check: (files) => {
97
+ const srcFiles = files.filter(f => f.path.match(/src\/.*\.(ts|tsx|js|jsx)$/));
98
+ const testFiles = files.filter(f => f.path.match(/\.(test|spec)\.(ts|tsx|js|jsx)$/));
99
+ const colocated = testFiles.filter(t => {
100
+ const basePath = t.path.replace(/\.(test|spec)\.(ts|tsx|js|jsx)$/, "");
101
+ return srcFiles.some(s => s.path.startsWith(basePath));
102
+ });
103
+ return {
104
+ pass: colocated.length === testFiles.length,
105
+ message: "Tests should be colocated with source files",
106
+ severity: "low",
107
+ };
108
+ },
109
+ },
110
+ ],
111
+ };
112
+ }
113
+
114
+ async review(payload) {
115
+ const { files = [], diff = "", context = {} } = payload;
116
+ const findings = [];
117
+ const startTime = Date.now();
118
+
119
+ // Analyze imports and exports in diff
120
+ if (diff) {
121
+ findings.push(...this._analyzeDiffPatterns(diff));
122
+ }
123
+
124
+ // Analyze file structure
125
+ if (files.length > 0) {
126
+ findings.push(...this._analyzeFileStructure(files));
127
+ findings.push(...this._detectCircularDependencies(files));
128
+ }
129
+
130
+ // Check naming conventions
131
+ findings.push(...this._checkNamingConventions(files));
132
+
133
+ // Determine approval status
134
+ const hasHigh = findings.some(f => f.severity === "high");
135
+ const hasMedium = findings.some(f => f.severity === "medium");
136
+
137
+ return {
138
+ approved: !hasHigh,
139
+ reason: hasHigh
140
+ ? "Architecture violations found - review required"
141
+ : hasMedium
142
+ ? "Approved with architecture warnings"
143
+ : findings.length > 0
144
+ ? "Approved with minor suggestions"
145
+ : "Architecture patterns look good",
146
+ findings,
147
+ signature: this._sign(findings),
148
+ metadata: {
149
+ analysisTimeMs: Date.now() - startTime,
150
+ filesAnalyzed: files.length,
151
+ rulesChecked: this._countRules(),
152
+ },
153
+ };
154
+ }
155
+
156
+ _analyzeDiffPatterns(diff) {
157
+ const findings = [];
158
+ const lines = diff.split("\n");
159
+ let currentFile = "unknown";
160
+ let lineNumber = 0;
161
+
162
+ for (const line of lines) {
163
+ // Track file context
164
+ if (line.startsWith("+++") || line.startsWith("---")) {
165
+ const match = line.match(/[ab]\/(.+)/);
166
+ if (match) currentFile = match[1];
167
+ continue;
168
+ }
169
+
170
+ if (line.startsWith("@@")) {
171
+ const match = line.match(/@@ -\d+(?:,\d+)? \+(\d+)/);
172
+ if (match) lineNumber = parseInt(match[1], 10);
173
+ continue;
174
+ }
175
+
176
+ // Only check added lines
177
+ if (line.startsWith("+") && !line.startsWith("+++")) {
178
+ const addedContent = line.slice(1);
179
+
180
+ // Check layer violations
181
+ for (const rule of this.rules.layerViolations) {
182
+ if (rule.context.test(currentFile) && rule.pattern.test(addedContent)) {
183
+ findings.push({
184
+ type: "layer-violation",
185
+ rule: rule.name,
186
+ severity: rule.severity,
187
+ message: rule.message,
188
+ file: currentFile,
189
+ line: lineNumber,
190
+ evidence: addedContent.trim().slice(0, 100),
191
+ });
192
+ }
193
+ }
194
+
195
+ // Check import patterns
196
+ for (const rule of this.rules.importPatterns) {
197
+ if (rule.pattern.test(addedContent)) {
198
+ if (!rule.context || rule.context.test(currentFile)) {
199
+ findings.push({
200
+ type: "import-pattern",
201
+ rule: rule.name,
202
+ severity: rule.severity,
203
+ message: rule.message,
204
+ file: currentFile,
205
+ line: lineNumber,
206
+ evidence: addedContent.trim().slice(0, 100),
207
+ });
208
+ }
209
+ }
210
+ }
211
+
212
+ lineNumber++;
213
+ } else if (!line.startsWith("-")) {
214
+ lineNumber++;
215
+ }
216
+ }
217
+
218
+ return findings;
219
+ }
220
+
221
+ _analyzeFileStructure(files) {
222
+ const findings = [];
223
+
224
+ for (const rule of this.rules.fileOrganization) {
225
+ if (typeof rule.check === "function") {
226
+ const result = rule.check(files);
227
+ if (!result.pass) {
228
+ findings.push({
229
+ type: "file-organization",
230
+ rule: rule.name,
231
+ severity: result.severity || "low",
232
+ message: result.message,
233
+ });
234
+ }
235
+ }
236
+ }
237
+
238
+ return findings;
239
+ }
240
+
241
+ _detectCircularDependencies(files) {
242
+ const findings = [];
243
+ const graph = new Map();
244
+
245
+ // Build dependency graph
246
+ for (const file of files) {
247
+ if (!file.content) continue;
248
+
249
+ const imports = [];
250
+ const importRegex = /import\s+(?:.*?\s+from\s+)?['"]([^'"]+)['"]/g;
251
+ let match;
252
+
253
+ while ((match = importRegex.exec(file.content)) !== null) {
254
+ const importPath = match[1];
255
+ // Only track relative imports
256
+ if (importPath.startsWith(".")) {
257
+ const resolvedPath = this._resolveImportPath(file.path, importPath);
258
+ imports.push(resolvedPath);
259
+ }
260
+ }
261
+
262
+ graph.set(file.path, imports);
263
+ }
264
+
265
+ // Detect cycles using DFS
266
+ const visited = new Set();
267
+ const recursionStack = new Set();
268
+
269
+ const detectCycle = (node, path = []) => {
270
+ if (recursionStack.has(node)) {
271
+ const cycleStart = path.indexOf(node);
272
+ const cycle = path.slice(cycleStart);
273
+ return { hasCycle: true, cycle: [...cycle, node] };
274
+ }
275
+
276
+ if (visited.has(node)) {
277
+ return { hasCycle: false };
278
+ }
279
+
280
+ visited.add(node);
281
+ recursionStack.add(node);
282
+
283
+ const neighbors = graph.get(node) || [];
284
+ for (const neighbor of neighbors) {
285
+ const result = detectCycle(neighbor, [...path, node]);
286
+ if (result.hasCycle) {
287
+ return result;
288
+ }
289
+ }
290
+
291
+ recursionStack.delete(node);
292
+ return { hasCycle: false };
293
+ };
294
+
295
+ for (const node of graph.keys()) {
296
+ visited.clear();
297
+ recursionStack.clear();
298
+ const result = detectCycle(node);
299
+
300
+ if (result.hasCycle) {
301
+ findings.push({
302
+ type: "circular-dependency",
303
+ severity: this.rules.circularDependencies.severity,
304
+ message: "Circular dependency detected",
305
+ evidence: result.cycle.join(" → "),
306
+ });
307
+ break; // Report only first cycle found
308
+ }
309
+ }
310
+
311
+ return findings;
312
+ }
313
+
314
+ _checkNamingConventions(files) {
315
+ const findings = [];
316
+
317
+ for (const file of files) {
318
+ const fileName = path.basename(file.path);
319
+
320
+ for (const rule of this.rules.namingConventions) {
321
+ if (rule.context.test(file.path)) {
322
+ if (!rule.pattern.test(fileName)) {
323
+ findings.push({
324
+ type: "naming-convention",
325
+ rule: rule.name,
326
+ severity: rule.severity,
327
+ message: rule.message,
328
+ file: file.path,
329
+ });
330
+ }
331
+ }
332
+ }
333
+ }
334
+
335
+ return findings;
336
+ }
337
+
338
+ _resolveImportPath(fromPath, importPath) {
339
+ const fromDir = path.dirname(fromPath);
340
+ return path.normalize(path.join(fromDir, importPath));
341
+ }
342
+
343
+ _countRules() {
344
+ return (
345
+ this.rules.layerViolations.length +
346
+ this.rules.namingConventions.length +
347
+ this.rules.importPatterns.length +
348
+ this.rules.fileOrganization.length +
349
+ 1 // circular dependencies
350
+ );
351
+ }
352
+
353
+ _sign(findings) {
354
+ const timestamp = Date.now();
355
+ const hash = crypto
356
+ .createHash("sha256")
357
+ .update(JSON.stringify({ findings: findings.length, timestamp }))
358
+ .digest("hex")
359
+ .slice(0, 16);
360
+ return `arch_${timestamp}_${hash}_${findings.length}`;
361
+ }
362
+ }
363
+
364
+ module.exports = { ArchitectureAuthority };
@@ -0,0 +1,341 @@
1
+ /**
2
+ * Compliance Authority - Automated compliance review
3
+ *
4
+ * Checks for:
5
+ * - SOC2 compliance issues
6
+ * - GDPR data handling
7
+ * - HIPAA PHI handling
8
+ * - PCI-DSS requirements
9
+ * - License compliance
10
+ */
11
+
12
+ "use strict";
13
+
14
+ const crypto = require("crypto");
15
+
16
+ class ComplianceAuthority {
17
+ static description = "Compliance check (SOC2, GDPR, HIPAA, etc.)";
18
+ static automated = true;
19
+ static tier = "pro";
20
+
21
+ constructor(options = {}) {
22
+ this.options = options;
23
+ this.frameworks = options.frameworks || ["soc2", "gdpr"];
24
+ this.rules = this._initializeRules();
25
+ }
26
+
27
+ _initializeRules() {
28
+ return {
29
+ soc2: [
30
+ {
31
+ name: "logging-required",
32
+ pattern: /(authentication|login|logout|password|permission|access)/i,
33
+ checkAuditLog: true,
34
+ message: "Security events must be logged for SOC2 compliance",
35
+ severity: "high",
36
+ },
37
+ {
38
+ name: "encryption-at-rest",
39
+ pattern: /\.encrypt|crypto\.|bcrypt|argon2/,
40
+ inverse: true,
41
+ context: /(password|secret|token|key)/i,
42
+ message: "Sensitive data should be encrypted at rest",
43
+ severity: "high",
44
+ },
45
+ {
46
+ name: "access-control",
47
+ pattern: /(isAdmin|isAuthenticated|hasPermission|authorize|checkRole)/i,
48
+ context: /(route|endpoint|api|handler)/i,
49
+ message: "API endpoints should have access control checks",
50
+ severity: "medium",
51
+ },
52
+ ],
53
+ gdpr: [
54
+ {
55
+ name: "pii-handling",
56
+ pattern: /(email|phone|address|ssn|social[_-]?security|passport|license)/i,
57
+ message: "Personal Identifiable Information detected - ensure GDPR compliance",
58
+ severity: "medium",
59
+ },
60
+ {
61
+ name: "consent-tracking",
62
+ pattern: /(consent|gdpr|optIn|optOut|dataSubject)/i,
63
+ context: /(user|customer|profile)/i,
64
+ inverse: true,
65
+ message: "User data handling should include consent management",
66
+ severity: "high",
67
+ },
68
+ {
69
+ name: "data-retention",
70
+ pattern: /(delete|purge|anonymize|retention)/i,
71
+ context: /(user|customer|pii)/i,
72
+ inverse: true,
73
+ message: "Consider data retention and right-to-be-forgotten requirements",
74
+ severity: "medium",
75
+ },
76
+ {
77
+ name: "data-export",
78
+ pattern: /(export|download|portability)/i,
79
+ context: /(user|customer|data)/i,
80
+ inverse: true,
81
+ message: "GDPR requires data portability capability",
82
+ severity: "low",
83
+ },
84
+ ],
85
+ hipaa: [
86
+ {
87
+ name: "phi-logging",
88
+ pattern: /(patient|medical|health|diagnosis|prescription|treatment)/i,
89
+ message: "Protected Health Information detected - ensure HIPAA compliance",
90
+ severity: "critical",
91
+ },
92
+ {
93
+ name: "phi-encryption",
94
+ pattern: /\.encrypt|crypto\./,
95
+ context: /(patient|medical|health)/i,
96
+ inverse: true,
97
+ message: "PHI must be encrypted in transit and at rest",
98
+ severity: "critical",
99
+ },
100
+ {
101
+ name: "access-audit",
102
+ pattern: /(audit|log|track)/i,
103
+ context: /(patient|medical|record)/i,
104
+ inverse: true,
105
+ message: "Access to PHI must be logged for HIPAA audit requirements",
106
+ severity: "high",
107
+ },
108
+ ],
109
+ pciDss: [
110
+ {
111
+ name: "card-data",
112
+ pattern: /(cardNumber|cvv|expiry|pan|primaryAccountNumber)/i,
113
+ message: "Payment card data detected - ensure PCI-DSS compliance",
114
+ severity: "critical",
115
+ },
116
+ {
117
+ name: "card-storage",
118
+ pattern: /(save|store|persist).*card/i,
119
+ message: "Storing payment card data requires PCI-DSS compliance",
120
+ severity: "critical",
121
+ },
122
+ {
123
+ name: "card-logging",
124
+ pattern: /console\.(log|info|debug).*card/i,
125
+ message: "Never log payment card data",
126
+ severity: "critical",
127
+ },
128
+ ],
129
+ licenses: [
130
+ {
131
+ name: "gpl-viral",
132
+ pattern: /GPL|GNU General Public/i,
133
+ context: /LICENSE|package\.json/,
134
+ message: "GPL license detected - may require source disclosure",
135
+ severity: "medium",
136
+ },
137
+ {
138
+ name: "agpl-network",
139
+ pattern: /AGPL|Affero/i,
140
+ context: /LICENSE|package\.json/,
141
+ message: "AGPL license detected - network use triggers disclosure",
142
+ severity: "high",
143
+ },
144
+ ],
145
+ };
146
+ }
147
+
148
+ async review(payload) {
149
+ const { files = [], diff = "", context = {} } = payload;
150
+ const findings = [];
151
+ const startTime = Date.now();
152
+
153
+ // Determine which frameworks to check
154
+ const activeFrameworks = context.frameworks || this.frameworks;
155
+
156
+ // Analyze diff content
157
+ if (diff) {
158
+ findings.push(...this._analyzeDiff(diff, activeFrameworks));
159
+ }
160
+
161
+ // Analyze files
162
+ for (const file of files) {
163
+ if (file.content) {
164
+ findings.push(...this._analyzeFile(file.path || "unknown", file.content, activeFrameworks));
165
+ }
166
+ }
167
+
168
+ // Check for missing compliance requirements
169
+ findings.push(...this._checkMissingRequirements(files, activeFrameworks));
170
+
171
+ // Determine approval status
172
+ const hasCritical = findings.some(f => f.severity === "critical");
173
+ const hasHigh = findings.some(f => f.severity === "high");
174
+
175
+ return {
176
+ approved: !hasCritical,
177
+ reason: hasCritical
178
+ ? "Critical compliance violations found - immediate action required"
179
+ : hasHigh
180
+ ? "Approved with compliance warnings - review required"
181
+ : findings.length > 0
182
+ ? "Approved with minor compliance notes"
183
+ : "Compliance checks passed",
184
+ findings,
185
+ signature: this._sign(findings),
186
+ metadata: {
187
+ analysisTimeMs: Date.now() - startTime,
188
+ filesAnalyzed: files.length,
189
+ frameworksChecked: activeFrameworks,
190
+ rulesChecked: this._countRules(activeFrameworks),
191
+ },
192
+ };
193
+ }
194
+
195
+ _analyzeDiff(diff, frameworks) {
196
+ const findings = [];
197
+ const lines = diff.split("\n");
198
+ let currentFile = "unknown";
199
+ let lineNumber = 0;
200
+
201
+ for (const line of lines) {
202
+ if (line.startsWith("+++") || line.startsWith("---")) {
203
+ const match = line.match(/[ab]\/(.+)/);
204
+ if (match) currentFile = match[1];
205
+ continue;
206
+ }
207
+
208
+ if (line.startsWith("@@")) {
209
+ const match = line.match(/@@ -\d+(?:,\d+)? \+(\d+)/);
210
+ if (match) lineNumber = parseInt(match[1], 10);
211
+ continue;
212
+ }
213
+
214
+ if (line.startsWith("+") && !line.startsWith("+++")) {
215
+ const addedContent = line.slice(1);
216
+
217
+ for (const framework of frameworks) {
218
+ const rules = this.rules[framework] || [];
219
+ for (const rule of rules) {
220
+ if (this._matchesRule(addedContent, currentFile, rule)) {
221
+ findings.push({
222
+ type: "compliance-violation",
223
+ framework,
224
+ rule: rule.name,
225
+ severity: rule.severity,
226
+ message: rule.message,
227
+ file: currentFile,
228
+ line: lineNumber,
229
+ evidence: addedContent.trim().slice(0, 100),
230
+ });
231
+ }
232
+ }
233
+ }
234
+
235
+ lineNumber++;
236
+ } else if (!line.startsWith("-")) {
237
+ lineNumber++;
238
+ }
239
+ }
240
+
241
+ return findings;
242
+ }
243
+
244
+ _analyzeFile(filePath, content, frameworks) {
245
+ const findings = [];
246
+ const lines = content.split("\n");
247
+
248
+ for (let i = 0; i < lines.length; i++) {
249
+ const line = lines[i];
250
+
251
+ for (const framework of frameworks) {
252
+ const rules = this.rules[framework] || [];
253
+ for (const rule of rules) {
254
+ if (this._matchesRule(line, filePath, rule)) {
255
+ findings.push({
256
+ type: "compliance-violation",
257
+ framework,
258
+ rule: rule.name,
259
+ severity: rule.severity,
260
+ message: rule.message,
261
+ file: filePath,
262
+ line: i + 1,
263
+ evidence: line.trim().slice(0, 100),
264
+ });
265
+ }
266
+ }
267
+ }
268
+ }
269
+
270
+ return findings;
271
+ }
272
+
273
+ _matchesRule(content, filePath, rule) {
274
+ // Check context filter first
275
+ if (rule.context && !rule.context.test(content) && !rule.context.test(filePath)) {
276
+ return false;
277
+ }
278
+
279
+ const matches = rule.pattern.test(content);
280
+
281
+ // Handle inverse rules (checking for absence of something)
282
+ if (rule.inverse) {
283
+ return !matches;
284
+ }
285
+
286
+ return matches;
287
+ }
288
+
289
+ _checkMissingRequirements(files, frameworks) {
290
+ const findings = [];
291
+ const allContent = files.map(f => f.content || "").join("\n");
292
+
293
+ // Check for missing privacy policy reference (GDPR)
294
+ if (frameworks.includes("gdpr")) {
295
+ if (!/(privacy[_-]?policy|gdpr|data[_-]?protection)/i.test(allContent)) {
296
+ findings.push({
297
+ type: "compliance-missing",
298
+ framework: "gdpr",
299
+ rule: "privacy-policy-reference",
300
+ severity: "medium",
301
+ message: "Consider adding privacy policy references for GDPR compliance",
302
+ });
303
+ }
304
+ }
305
+
306
+ // Check for missing audit logging setup (SOC2)
307
+ if (frameworks.includes("soc2")) {
308
+ if (!/(auditLog|securityLog|eventLog|audit\.log)/i.test(allContent)) {
309
+ findings.push({
310
+ type: "compliance-missing",
311
+ framework: "soc2",
312
+ rule: "audit-logging-setup",
313
+ severity: "medium",
314
+ message: "SOC2 requires audit logging infrastructure",
315
+ });
316
+ }
317
+ }
318
+
319
+ return findings;
320
+ }
321
+
322
+ _countRules(frameworks) {
323
+ let count = 0;
324
+ for (const framework of frameworks) {
325
+ count += (this.rules[framework] || []).length;
326
+ }
327
+ return count;
328
+ }
329
+
330
+ _sign(findings) {
331
+ const timestamp = Date.now();
332
+ const hash = crypto
333
+ .createHash("sha256")
334
+ .update(JSON.stringify({ findings: findings.length, timestamp }))
335
+ .digest("hex")
336
+ .slice(0, 16);
337
+ return `compl_${timestamp}_${hash}_${findings.length}`;
338
+ }
339
+ }
340
+
341
+ module.exports = { ComplianceAuthority };