tribunal-kit 4.0.1 → 4.3.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 (196) hide show
  1. package/.agent/ARCHITECTURE.md +21 -14
  2. package/.agent/GEMINI.md +4 -2
  3. package/.agent/agents/api-architect.md +66 -0
  4. package/.agent/agents/db-latency-auditor.md +216 -0
  5. package/.agent/agents/precedence-reviewer.md +41 -4
  6. package/.agent/agents/resilience-reviewer.md +88 -0
  7. package/.agent/agents/schema-reviewer.md +67 -0
  8. package/.agent/agents/swarm-worker-contracts.md +5 -5
  9. package/.agent/agents/throughput-optimizer.md +299 -0
  10. package/.agent/agents/ui-ux-auditor.md +292 -0
  11. package/.agent/agents/vitals-reviewer.md +223 -0
  12. package/.agent/history/case-law/cases/case-0001.json +33 -0
  13. package/.agent/history/case-law/index.json +35 -0
  14. package/.agent/rules/GEMINI.md +28 -11
  15. package/.agent/scripts/__pycache__/_colors.cpython-311.pyc +0 -0
  16. package/.agent/scripts/__pycache__/_utils.cpython-311.pyc +0 -0
  17. package/.agent/scripts/__pycache__/case_law_manager.cpython-311.pyc +0 -0
  18. package/.agent/scripts/_colors.js +18 -0
  19. package/.agent/scripts/_utils.js +42 -0
  20. package/.agent/scripts/auto_preview.js +197 -0
  21. package/.agent/scripts/bundle_analyzer.js +290 -0
  22. package/.agent/scripts/case_law_manager.js +684 -0
  23. package/.agent/scripts/checklist.js +266 -0
  24. package/.agent/scripts/colors.js +17 -0
  25. package/.agent/scripts/compress_skills.js +141 -0
  26. package/.agent/scripts/consolidate_skills.js +149 -0
  27. package/.agent/scripts/context_broker.js +609 -0
  28. package/.agent/scripts/deep_compress.js +150 -0
  29. package/.agent/scripts/dependency_analyzer.js +272 -0
  30. package/.agent/scripts/inner_loop_validator.js +465 -0
  31. package/.agent/scripts/lint_runner.js +187 -0
  32. package/.agent/scripts/minify_context.js +100 -0
  33. package/.agent/scripts/patch_skills_meta.js +156 -0
  34. package/.agent/scripts/patch_skills_output.js +244 -0
  35. package/.agent/scripts/schema_validator.js +297 -0
  36. package/.agent/scripts/security_scan.js +303 -0
  37. package/.agent/scripts/session_manager.js +276 -0
  38. package/.agent/scripts/skill_evolution.js +644 -0
  39. package/.agent/scripts/skill_integrator.js +313 -0
  40. package/.agent/scripts/strengthen_skills.js +193 -0
  41. package/.agent/scripts/strip_tribunal.js +47 -0
  42. package/.agent/scripts/swarm_dispatcher.js +360 -0
  43. package/.agent/scripts/test_runner.js +193 -0
  44. package/.agent/scripts/utils.js +32 -0
  45. package/.agent/scripts/verify_all.js +256 -0
  46. package/.agent/skills/agent-organizer/SKILL.md +42 -0
  47. package/.agent/skills/agentic-patterns/SKILL.md +42 -0
  48. package/.agent/skills/ai-prompt-injection-defense/SKILL.md +42 -0
  49. package/.agent/skills/api-patterns/SKILL.md +42 -0
  50. package/.agent/skills/api-security-auditor/SKILL.md +42 -0
  51. package/.agent/skills/app-builder/SKILL.md +42 -0
  52. package/.agent/skills/app-builder/templates/SKILL.md +70 -0
  53. package/.agent/skills/app-builder/templates/astro-static/TEMPLATE.md +1 -1
  54. package/.agent/skills/app-builder/templates/chrome-extension/TEMPLATE.md +1 -1
  55. package/.agent/skills/app-builder/templates/cli-tool/TEMPLATE.md +1 -1
  56. package/.agent/skills/app-builder/templates/electron-desktop/TEMPLATE.md +1 -1
  57. package/.agent/skills/app-builder/templates/express-api/TEMPLATE.md +1 -1
  58. package/.agent/skills/app-builder/templates/flutter-app/TEMPLATE.md +1 -1
  59. package/.agent/skills/app-builder/templates/monorepo-turborepo/TEMPLATE.md +1 -1
  60. package/.agent/skills/app-builder/templates/nextjs-fullstack/TEMPLATE.md +1 -1
  61. package/.agent/skills/app-builder/templates/nextjs-saas/TEMPLATE.md +1 -1
  62. package/.agent/skills/app-builder/templates/nextjs-static/TEMPLATE.md +1 -1
  63. package/.agent/skills/app-builder/templates/nuxt-app/TEMPLATE.md +1 -1
  64. package/.agent/skills/app-builder/templates/python-fastapi/TEMPLATE.md +1 -1
  65. package/.agent/skills/app-builder/templates/react-native-app/TEMPLATE.md +1 -1
  66. package/.agent/skills/appflow-wireframe/SKILL.md +42 -0
  67. package/.agent/skills/architecture/SKILL.md +42 -0
  68. package/.agent/skills/authentication-best-practices/SKILL.md +42 -0
  69. package/.agent/skills/bash-linux/SKILL.md +42 -0
  70. package/.agent/skills/behavioral-modes/SKILL.md +42 -0
  71. package/.agent/skills/brainstorming/SKILL.md +42 -0
  72. package/.agent/skills/building-native-ui/SKILL.md +42 -0
  73. package/.agent/skills/clean-code/SKILL.md +42 -0
  74. package/.agent/skills/code-review-checklist/SKILL.md +42 -0
  75. package/.agent/skills/config-validator/SKILL.md +42 -0
  76. package/.agent/skills/csharp-developer/SKILL.md +42 -0
  77. package/.agent/skills/data-validation-schemas/SKILL.md +320 -0
  78. package/.agent/skills/database-design/SKILL.md +42 -0
  79. package/.agent/skills/deployment-procedures/SKILL.md +42 -0
  80. package/.agent/skills/devops-engineer/SKILL.md +42 -0
  81. package/.agent/skills/devops-incident-responder/SKILL.md +42 -0
  82. package/.agent/skills/doc.md +1 -1
  83. package/.agent/skills/documentation-templates/SKILL.md +42 -0
  84. package/.agent/skills/edge-computing/SKILL.md +42 -0
  85. package/.agent/skills/error-resilience/SKILL.md +420 -0
  86. package/.agent/skills/extract-design-system/SKILL.md +42 -0
  87. package/.agent/skills/framer-motion-expert/SKILL.md +42 -1
  88. package/.agent/skills/frontend-design/SKILL.md +42 -0
  89. package/.agent/skills/game-design-expert/SKILL.md +42 -0
  90. package/.agent/skills/game-engineering-expert/SKILL.md +42 -0
  91. package/.agent/skills/geo-fundamentals/SKILL.md +42 -0
  92. package/.agent/skills/github-operations/SKILL.md +42 -0
  93. package/.agent/skills/gsap-core/SKILL.md +300 -0
  94. package/.agent/skills/gsap-frameworks/SKILL.md +199 -0
  95. package/.agent/skills/gsap-performance/SKILL.md +125 -0
  96. package/.agent/skills/gsap-plugins/SKILL.md +472 -0
  97. package/.agent/skills/gsap-react/SKILL.md +181 -0
  98. package/.agent/skills/gsap-scrolltrigger/SKILL.md +342 -0
  99. package/.agent/skills/gsap-timeline/SKILL.md +153 -0
  100. package/.agent/skills/gsap-utils/SKILL.md +330 -0
  101. package/.agent/skills/i18n-localization/SKILL.md +42 -0
  102. package/.agent/skills/intelligent-routing/SKILL.md +72 -1
  103. package/.agent/skills/lint-and-validate/SKILL.md +42 -0
  104. package/.agent/skills/llm-engineering/SKILL.md +42 -0
  105. package/.agent/skills/local-first/SKILL.md +42 -0
  106. package/.agent/skills/mcp-builder/SKILL.md +42 -0
  107. package/.agent/skills/mobile-design/SKILL.md +42 -0
  108. package/.agent/skills/monorepo-management/SKILL.md +326 -0
  109. package/.agent/skills/motion-engineering/SKILL.md +42 -0
  110. package/.agent/skills/nextjs-react-expert/SKILL.md +42 -0
  111. package/.agent/skills/nodejs-best-practices/SKILL.md +42 -0
  112. package/.agent/skills/observability/SKILL.md +42 -0
  113. package/.agent/skills/parallel-agents/SKILL.md +42 -0
  114. package/.agent/skills/performance-profiling/SKILL.md +42 -0
  115. package/.agent/skills/plan-writing/SKILL.md +42 -0
  116. package/.agent/skills/platform-engineer/SKILL.md +42 -0
  117. package/.agent/skills/playwright-best-practices/SKILL.md +42 -0
  118. package/.agent/skills/powershell-windows/SKILL.md +42 -0
  119. package/.agent/skills/project-idioms/SKILL.md +42 -0
  120. package/.agent/skills/python-patterns/SKILL.md +42 -0
  121. package/.agent/skills/python-pro/SKILL.md +42 -0
  122. package/.agent/skills/react-specialist/SKILL.md +42 -0
  123. package/.agent/skills/readme-builder/SKILL.md +42 -0
  124. package/.agent/skills/realtime-patterns/SKILL.md +42 -0
  125. package/.agent/skills/red-team-tactics/SKILL.md +42 -0
  126. package/.agent/skills/rust-pro/SKILL.md +42 -0
  127. package/.agent/skills/seo-fundamentals/SKILL.md +42 -0
  128. package/.agent/skills/server-management/SKILL.md +42 -0
  129. package/.agent/skills/shadcn-ui-expert/SKILL.md +42 -0
  130. package/.agent/skills/skill-creator/SKILL.md +42 -0
  131. package/.agent/skills/sql-pro/SKILL.md +42 -0
  132. package/.agent/skills/supabase-postgres-best-practices/SKILL.md +42 -0
  133. package/.agent/skills/swiftui-expert/SKILL.md +42 -0
  134. package/.agent/skills/systematic-debugging/SKILL.md +42 -0
  135. package/.agent/skills/tailwind-patterns/SKILL.md +42 -0
  136. package/.agent/skills/tdd-workflow/SKILL.md +42 -0
  137. package/.agent/skills/test-result-analyzer/SKILL.md +42 -0
  138. package/.agent/skills/testing-patterns/SKILL.md +42 -0
  139. package/.agent/skills/trend-researcher/SKILL.md +42 -0
  140. package/.agent/skills/typescript-advanced/SKILL.md +327 -0
  141. package/.agent/skills/ui-ux-pro-max/SKILL.md +42 -0
  142. package/.agent/skills/ui-ux-researcher/SKILL.md +42 -0
  143. package/.agent/skills/vue-expert/SKILL.md +42 -0
  144. package/.agent/skills/vulnerability-scanner/SKILL.md +42 -0
  145. package/.agent/skills/web-accessibility-auditor/SKILL.md +42 -0
  146. package/.agent/skills/web-design-guidelines/SKILL.md +42 -0
  147. package/.agent/skills/webapp-testing/SKILL.md +42 -0
  148. package/.agent/skills/whimsy-injector/SKILL.md +42 -0
  149. package/.agent/skills/workflow-optimizer/SKILL.md +42 -0
  150. package/.agent/workflows/audit.md +6 -6
  151. package/.agent/workflows/deploy.md +1 -1
  152. package/.agent/workflows/generate.md +23 -6
  153. package/.agent/workflows/session.md +5 -5
  154. package/.agent/workflows/swarm.md +2 -2
  155. package/.agent/workflows/tribunal-backend.md +13 -2
  156. package/.agent/workflows/tribunal-full.md +15 -8
  157. package/.agent/workflows/tribunal-speed.md +183 -0
  158. package/README.md +64 -8
  159. package/bin/tribunal-kit.js +281 -41
  160. package/package.json +9 -6
  161. package/scripts/changelog.js +167 -0
  162. package/scripts/sync-version.js +81 -0
  163. package/.agent/scripts/__pycache__/auto_preview.cpython-311.pyc +0 -0
  164. package/.agent/scripts/__pycache__/bundle_analyzer.cpython-311.pyc +0 -0
  165. package/.agent/scripts/__pycache__/checklist.cpython-311.pyc +0 -0
  166. package/.agent/scripts/__pycache__/dependency_analyzer.cpython-311.pyc +0 -0
  167. package/.agent/scripts/__pycache__/security_scan.cpython-311.pyc +0 -0
  168. package/.agent/scripts/__pycache__/session_manager.cpython-311.pyc +0 -0
  169. package/.agent/scripts/__pycache__/skill_integrator.cpython-311.pyc +0 -0
  170. package/.agent/scripts/__pycache__/swarm_dispatcher.cpython-311.pyc +0 -0
  171. package/.agent/scripts/__pycache__/test_runner.cpython-311.pyc +0 -0
  172. package/.agent/scripts/__pycache__/verify_all.cpython-311.pyc +0 -0
  173. package/.agent/scripts/auto_preview.py +0 -180
  174. package/.agent/scripts/bundle_analyzer.py +0 -259
  175. package/.agent/scripts/case_law_manager.py +0 -525
  176. package/.agent/scripts/checklist.py +0 -209
  177. package/.agent/scripts/compress_skills.py +0 -167
  178. package/.agent/scripts/consolidate_skills.py +0 -173
  179. package/.agent/scripts/deep_compress.py +0 -202
  180. package/.agent/scripts/dependency_analyzer.py +0 -247
  181. package/.agent/scripts/lint_runner.py +0 -188
  182. package/.agent/scripts/minify_context.py +0 -80
  183. package/.agent/scripts/patch_skills_meta.py +0 -177
  184. package/.agent/scripts/patch_skills_output.py +0 -285
  185. package/.agent/scripts/schema_validator.py +0 -279
  186. package/.agent/scripts/security_scan.py +0 -224
  187. package/.agent/scripts/session_manager.py +0 -261
  188. package/.agent/scripts/skill_evolution.py +0 -563
  189. package/.agent/scripts/skill_integrator.py +0 -234
  190. package/.agent/scripts/strengthen_skills.py +0 -220
  191. package/.agent/scripts/strip_tribunal.py +0 -41
  192. package/.agent/scripts/swarm_dispatcher.py +0 -350
  193. package/.agent/scripts/test_runner.py +0 -192
  194. package/.agent/scripts/test_swarm_dispatcher.py +0 -163
  195. package/.agent/scripts/verify_all.py +0 -195
  196. package/.agent/skills/gsap-expert/SKILL.md +0 -194
@@ -0,0 +1,297 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * schema_validator.js — Database schema validator for the Tribunal Agent Kit.
4
+ *
5
+ * Usage:
6
+ * node .agent/scripts/schema_validator.js .
7
+ * node .agent/scripts/schema_validator.js . --type prisma
8
+ * node .agent/scripts/schema_validator.js . --file prisma/schema.prisma
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ const fs = require('fs');
14
+ const path = require('path');
15
+
16
+ const { RED, GREEN, YELLOW, BLUE, BOLD, RESET } = require('./colors.js');
17
+
18
+ function header(title) {
19
+ console.log(`\n${BOLD}${BLUE}━━━ ${title} ━━━${RESET}`);
20
+ }
21
+
22
+ function ok(msg) {
23
+ console.log(` ${GREEN}✅ ${msg}${RESET}`);
24
+ }
25
+
26
+ function fail(msg) {
27
+ console.log(` ${RED}❌ ${msg}${RESET}`);
28
+ }
29
+
30
+ function warn(msg) {
31
+ console.log(` ${YELLOW}⚠️ ${msg}${RESET}`);
32
+ }
33
+
34
+ function skip(msg) {
35
+ console.log(` ${YELLOW}⏭️ ${msg}${RESET}`);
36
+ }
37
+
38
+ function detectOrm(projectRoot) {
39
+ if (fs.existsSync(path.join(projectRoot, "prisma", "schema.prisma"))) {
40
+ return "prisma";
41
+ }
42
+
43
+ function searchFor(dir, patterns) {
44
+ let items;
45
+ try {
46
+ items = fs.readdirSync(dir, { withFileTypes: true });
47
+ } catch {
48
+ return false;
49
+ }
50
+
51
+ for (const item of items) {
52
+ if (item.isDirectory() && !["node_modules", ".git"].includes(item.name)) {
53
+ if (searchFor(path.join(dir, item.name), patterns)) return true;
54
+ if (item.name === "migrations") {
55
+ try {
56
+ const mFiles = fs.readdirSync(path.join(dir, item.name));
57
+ if (mFiles.some(f => f.endsWith(".sql"))) return "sql";
58
+ } catch {}
59
+ }
60
+ } else {
61
+ for (const p of patterns) {
62
+ if (p.test(item.name)) return p.type;
63
+ }
64
+ }
65
+ }
66
+ return null;
67
+ }
68
+
69
+ const type = searchFor(projectRoot, [{ test: name => name.startsWith("drizzle.config."), type: "drizzle" }]);
70
+ if (type) return type;
71
+
72
+ if (fs.existsSync(path.join(projectRoot, "knexfile.js")) || fs.existsSync(path.join(projectRoot, "knexfile.ts"))) {
73
+ return "knex";
74
+ }
75
+
76
+ return null;
77
+ }
78
+
79
+ function validatePrisma(filepath) {
80
+ const issues = [];
81
+ let lines;
82
+ try {
83
+ lines = fs.readFileSync(filepath, 'utf8').split('\n');
84
+ } catch {
85
+ return [["error", `Cannot read file: ${filepath}`, 0]];
86
+ }
87
+
88
+ let currentModel = "";
89
+ let hasCreatedAt = false;
90
+ let hasUpdatedAt = false;
91
+ let modelStartLine = 0;
92
+ let fieldsWithRelation = [];
93
+ let indexedFields = new Set();
94
+ let hasIdField = false;
95
+
96
+ for (let i = 0; i < lines.length; i++) {
97
+ const lineNum = i + 1;
98
+ const line = lines[i];
99
+ const stripped = line.trim();
100
+
101
+ const modelMatch = stripped.match(/^model\s+(\w+)\s*\{/);
102
+ if (modelMatch) {
103
+ if (currentModel) {
104
+ if (!hasCreatedAt) issues.push(["warn", `Model '${currentModel}' missing createdAt timestamp`, modelStartLine]);
105
+ if (!hasUpdatedAt) issues.push(["warn", `Model '${currentModel}' missing updatedAt timestamp`, modelStartLine]);
106
+ if (!hasIdField) issues.push(["warn", `Model '${currentModel}' has no @id field`, modelStartLine]);
107
+ for (const [fieldName, fieldLine] of fieldsWithRelation) {
108
+ if (!indexedFields.has(fieldName)) {
109
+ issues.push(["warn", `Model '${currentModel}': foreign key '${fieldName}' has no @@index`, fieldLine]);
110
+ }
111
+ }
112
+ }
113
+
114
+ currentModel = modelMatch[1];
115
+ modelStartLine = lineNum;
116
+ hasCreatedAt = false;
117
+ hasUpdatedAt = false;
118
+ hasIdField = false;
119
+ fieldsWithRelation = [];
120
+ indexedFields.clear();
121
+
122
+ if (currentModel[0] !== currentModel[0].toUpperCase()) {
123
+ issues.push(["warn", `Model '${currentModel}' should use PascalCase`, lineNum]);
124
+ }
125
+ }
126
+
127
+ if (currentModel) {
128
+ if (stripped.includes("createdAt") || stripped.includes("created_at")) hasCreatedAt = true;
129
+ if (stripped.includes("updatedAt") || stripped.includes("updated_at")) hasUpdatedAt = true;
130
+ if (stripped.includes("@id")) hasIdField = true;
131
+
132
+ const fkMatch = stripped.match(/^\s*(\w+Id)\s+(Int|String|BigInt)/);
133
+ if (fkMatch && !stripped.includes("@relation")) {
134
+ fieldsWithRelation.push([fkMatch[1], lineNum]);
135
+ }
136
+
137
+ const indexMatch = stripped.match(/@@index\(\[([^\]]+)\]/);
138
+ if (indexMatch) {
139
+ for (const field of indexMatch[1].split(",")) {
140
+ indexedFields.add(field.trim());
141
+ }
142
+ }
143
+ }
144
+ }
145
+
146
+ if (currentModel) {
147
+ if (!hasCreatedAt) issues.push(["warn", `Model '${currentModel}' missing createdAt timestamp`, modelStartLine]);
148
+ if (!hasUpdatedAt) issues.push(["warn", `Model '${currentModel}' missing updatedAt timestamp`, modelStartLine]);
149
+ if (!hasIdField) issues.push(["warn", `Model '${currentModel}' has no @id field`, modelStartLine]);
150
+ for (const [fieldName, fieldLine] of fieldsWithRelation) {
151
+ if (!indexedFields.has(fieldName)) {
152
+ issues.push(["warn", `Model '${currentModel}': foreign key '${fieldName}' may need @@index`, fieldLine]);
153
+ }
154
+ }
155
+ }
156
+
157
+ return issues;
158
+ }
159
+
160
+ function validateSqlMigration(filepath) {
161
+ const issues = [];
162
+ let lines;
163
+ try {
164
+ lines = fs.readFileSync(filepath, 'utf8').split('\n');
165
+ } catch {
166
+ return [["error", `Cannot read file: ${filepath}`, 0]];
167
+ }
168
+
169
+ for (let i = 0; i < lines.length; i++) {
170
+ const lineNum = i + 1;
171
+ const stripped = lines[i].trim().toUpperCase();
172
+
173
+ if (stripped.includes("DROP TABLE") && !stripped.includes("IF EXISTS")) {
174
+ issues.push(["warn", "DROP TABLE without IF EXISTS — may fail on clean databases", lineNum]);
175
+ }
176
+ if (stripped.includes("REFERENCES") && !stripped.includes("NOT NULL") && !stripped.includes("NULL")) {
177
+ issues.push(["warn", "Foreign key without explicit NULL/NOT NULL constraint", lineNum]);
178
+ }
179
+ if (stripped.includes("CREATE TABLE")) {
180
+ issues.push(["info", "Verify this table includes created_at / updated_at columns", lineNum]);
181
+ }
182
+ }
183
+
184
+ return issues;
185
+ }
186
+
187
+ function main() {
188
+ const args = process.argv.slice(2);
189
+ let targetPath = null;
190
+ let typeArg = "auto";
191
+ let fileArg = null;
192
+
193
+ let i = 0;
194
+ while (i < args.length) {
195
+ if (args[i] === '--type' && i + 1 < args.length) typeArg = args[++i];
196
+ else if (args[i] === '--file' && i + 1 < args.length) fileArg = args[++i];
197
+ else if (!targetPath && !args[i].startsWith('-')) targetPath = args[i];
198
+ i++;
199
+ }
200
+
201
+ if (!targetPath) {
202
+ console.log("Usage: node schema_validator.js <path> [--type <prisma|drizzle|sql>] [--file <filepath>]");
203
+ process.exit(1);
204
+ }
205
+
206
+ const projectRoot = path.resolve(targetPath);
207
+ if (!fs.existsSync(projectRoot) || !fs.statSync(projectRoot).isDirectory()) {
208
+ fail(`Directory not found: ${projectRoot}`);
209
+ process.exit(1);
210
+ }
211
+
212
+ console.log(`${BOLD}Tribunal — schema_validator.js${RESET}`);
213
+ console.log(`Project: ${projectRoot}`);
214
+
215
+ const ormType = typeArg !== "auto" ? typeArg : detectOrm(projectRoot);
216
+ if (!ormType && !fileArg) {
217
+ skip("No schema files detected — skipping validation");
218
+ process.exit(0);
219
+ }
220
+
221
+ let issuesCount = 0;
222
+
223
+ if (fileArg) {
224
+ header(`Validating: ${fileArg}`);
225
+ const filepath = path.isAbsolute(fileArg) ? fileArg : path.join(projectRoot, fileArg);
226
+ let issues = [];
227
+ if (filepath.endsWith(".prisma")) issues = validatePrisma(filepath);
228
+ else if (filepath.endsWith(".sql")) issues = validateSqlMigration(filepath);
229
+ else {
230
+ skip(`Unknown schema file type: ${fileArg}`);
231
+ process.exit(0);
232
+ }
233
+
234
+ for (const [severity, message, line] of issues) {
235
+ if (severity === "error") { fail(`L${line}: ${message}`); issuesCount++; }
236
+ else if (severity === "warn") { warn(`L${line}: ${message}`); issuesCount++; }
237
+ else console.log(` ${BLUE}ℹ️ L${line}: ${message}${RESET}`);
238
+ }
239
+ } else if (ormType === "prisma") {
240
+ const schemaPath = path.join(projectRoot, "prisma", "schema.prisma");
241
+ if (fs.existsSync(schemaPath)) {
242
+ header("Prisma Schema Validation");
243
+ const issues = validatePrisma(schemaPath);
244
+ for (const [severity, message, line] of issues) {
245
+ if (severity === "error") { fail(`L${line}: ${message}`); issuesCount++; }
246
+ else if (severity === "warn") { warn(`L${line}: ${message}`); issuesCount++; }
247
+ else console.log(` ${BLUE}ℹ️ L${line}: ${message}${RESET}`);
248
+ }
249
+ } else {
250
+ skip(`Prisma schema not found at ${schemaPath}`);
251
+ }
252
+ } else if (ormType === "sql") {
253
+ header("SQL Migration Validation");
254
+ // Very basic recursion for migrations dir
255
+ function findMigrations(dir) {
256
+ let res = [];
257
+ try {
258
+ const items = fs.readdirSync(dir, { withFileTypes: true });
259
+ for (const item of items) {
260
+ if (item.isDirectory() && !["node_modules", ".git"].includes(item.name)) {
261
+ if (item.name === "migrations") {
262
+ const sqls = fs.readdirSync(path.join(dir, item.name)).filter(f => f.endsWith(".sql")).map(f => path.join(dir, item.name, f));
263
+ res.push(...sqls);
264
+ } else {
265
+ res.push(...findMigrations(path.join(dir, item.name)));
266
+ }
267
+ }
268
+ }
269
+ } catch {}
270
+ return res;
271
+ }
272
+
273
+ const mFiles = findMigrations(projectRoot).sort();
274
+ for (const sqlFile of mFiles) {
275
+ console.log(`\n 📄 ${path.basename(sqlFile)}`);
276
+ const issues = validateSqlMigration(sqlFile);
277
+ for (const [severity, message, line] of issues) {
278
+ if (severity === "error") { fail(` L${line}: ${message}`); issuesCount++; }
279
+ else if (severity === "warn") { warn(` L${line}: ${message}`); issuesCount++; }
280
+ else console.log(` ${BLUE}ℹ️ L${line}: ${message}${RESET}`);
281
+ }
282
+ }
283
+ } else if (ormType === "drizzle") {
284
+ header("Drizzle Schema");
285
+ skip("Drizzle validation not yet implemented — validate manually");
286
+ }
287
+
288
+ console.log(`\n${BOLD}━━━ Schema Validation Summary ━━━${RESET}`);
289
+ if (issuesCount === 0) ok("No schema issues found");
290
+ else warn(`${issuesCount} issue(s) found — review above`);
291
+
292
+ process.exit(0);
293
+ }
294
+
295
+ if (require.main === module) {
296
+ main();
297
+ }
@@ -0,0 +1,303 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * security_scan.js — Deep security scanner for the Tribunal Agent Kit.
4
+ *
5
+ * Checks for OWASP Top 10 patterns in source code:
6
+ * - Hardcoded secrets and credentials
7
+ * - SQL injection patterns (string concatenation in queries)
8
+ * - XSS-prone code (innerHTML, dangerouslySetInnerHTML)
9
+ * - Insecure eval() usage
10
+ * - Missing auth patterns
11
+ * - Insecure crypto usage
12
+ *
13
+ * ⚠️ DISCLAIMER: This is a heuristic regex-based pattern scanner, NOT a
14
+ * full static analysis tool. It may produce false positives (e.g., test
15
+ * fixtures containing 'password') and cannot detect indirect vulnerabilities
16
+ * (e.g., SQL injection via variable indirection). For production security
17
+ * auditing, supplement with dedicated tools like Semgrep, CodeQL, or Snyk.
18
+ *
19
+ * Usage:
20
+ * node .agent/scripts/security_scan.js .
21
+ * node .agent/scripts/security_scan.js . --severity high
22
+ * node .agent/scripts/security_scan.js . --files src/auth.ts src/db.ts
23
+ */
24
+
25
+ 'use strict';
26
+
27
+ const fs = require('fs');
28
+ const path = require('path');
29
+
30
+ // ━━━ ANSI colors ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
31
+ const RED = '\x1b[91m';
32
+ const GREEN = '\x1b[92m';
33
+ const YELLOW = '\x1b[93m';
34
+ const BLUE = '\x1b[94m';
35
+ const MAGENTA = '\x1b[95m';
36
+ const BOLD = '\x1b[1m';
37
+ const RESET = '\x1b[0m';
38
+
39
+ const SOURCE_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx', '.py', '.go', '.java', '.rb']);
40
+ const SKIP_DIRS = new Set([
41
+ 'node_modules', '.git', 'dist', 'build', '__pycache__', '.agent',
42
+ '.next', 'vendor', 'coverage', 'lcov-report', '.nyc_output',
43
+ 'test-results', '.jest-cache',
44
+ ]);
45
+
46
+ const SEVERITY_COLORS = {
47
+ critical: RED + BOLD,
48
+ high: RED,
49
+ medium: YELLOW,
50
+ low: BLUE,
51
+ };
52
+
53
+ const SEVERITY_RANK = { critical: 0, high: 1, medium: 2, low: 3 };
54
+
55
+ // Pattern definitions: [regex, severity, category, message]
56
+ const PATTERNS = [
57
+ // Secrets
58
+ [/(?:password|passwd|pwd)\s*=\s*["'][^"']+["']/i, 'critical', 'Hardcoded Secret', 'Hardcoded password detected'],
59
+ [/(?:api_key|apikey|api_secret)\s*=\s*["'][^"']+["']/i, 'critical', 'Hardcoded Secret', 'Hardcoded API key detected'],
60
+ [/(?:secret|token|auth_token)\s*=\s*["'][A-Za-z0-9+/=]{16,}["']/i, 'critical', 'Hardcoded Secret', 'Hardcoded secret/token detected'],
61
+ [/(?:PRIVATE_KEY|private_key)\s*=\s*["']/i, 'critical', 'Hardcoded Secret', 'Hardcoded private key detected'],
62
+
63
+ // SQL Injection
64
+ [/(?:query|execute|raw)\s*\(\s*[`"'].*\$\{/i, 'high', 'SQL Injection', 'String interpolation in SQL query — use parameterized queries'],
65
+ [/(?:query|execute|raw)\s*\(\s*["'].*\+\s*(?:req|input|params|body)/i, 'high', 'SQL Injection', 'String concatenation with user input in SQL'],
66
+ [/\.raw\s*\(\s*`/, 'medium', 'SQL Injection', 'Raw query with template literal — verify inputs are sanitized'],
67
+
68
+ // XSS
69
+ [/\.innerHTML\s*=/, 'high', 'XSS', 'Direct innerHTML assignment — use textContent or a sanitizer'],
70
+ [/dangerouslySetInnerHTML/, 'medium', 'XSS', 'dangerouslySetInnerHTML used — ensure input is sanitized'],
71
+ [/document\.write\s*\(/, 'high', 'XSS', 'document.write() is an XSS vector'],
72
+
73
+ // Insecure Functions
74
+ [/\beval\s*\(/, 'high', 'Code Injection', 'eval() is a code injection vector — avoid entirely'],
75
+ [/new\s+Function\s*\(/, 'high', 'Code Injection', 'new Function() is equivalent to eval()'],
76
+ [/child_process\.exec\s*\(/, 'medium', 'Command Injection', 'exec() with unsanitized input is a command injection vector'],
77
+ [/subprocess\.call\s*\(\s*[^,\]]*\bshell\s*=\s*True/, 'high', 'Command Injection', 'subprocess with shell=True — use shell=False and pass args as list'],
78
+
79
+ // Crypto
80
+ [/createHash\s*\(\s*["']md5["']/, 'medium', 'Weak Crypto', 'MD5 is cryptographically broken — use SHA-256+'],
81
+ [/createHash\s*\(\s*["']sha1["']/, 'medium', 'Weak Crypto', 'SHA-1 is deprecated — use SHA-256+'],
82
+ [/Math\.random\s*\(/, 'low', 'Weak Randomness', 'Math.random() is not cryptographically secure — use crypto.randomBytes()'],
83
+
84
+ // Auth Issues
85
+ [/algorithms\s*:\s*\[\s*["']none["']/, 'critical', 'Auth Bypass', "JWT 'none' algorithm allows auth bypass"],
86
+ [/verify\s*:\s*false/, 'high', 'Auth Bypass', 'SSL/TLS verification disabled'],
87
+ [/rejectUnauthorized\s*:\s*false/, 'high', 'Auth Bypass', 'TLS certificate validation disabled'],
88
+
89
+ // Information Disclosure
90
+ [/console\.log\s*\(.*(?:password|secret|token|key)/i, 'medium', 'Info Disclosure', 'Sensitive data logged to console'],
91
+ [/\.env(?:\.local|\.production)/, 'low', 'Info Disclosure', 'Env file reference — ensure not committed to git'],
92
+ ];
93
+
94
+
95
+ /**
96
+ * Scan a single file for security patterns.
97
+ * @param {string} filepath - Absolute path to the file.
98
+ * @param {string} projectRoot - Project root for relative path computation.
99
+ * @returns {Array<{severity:string, category:string, file:string, line:number, message:string, snippet:string}>}
100
+ */
101
+ function scanFile(filepath, projectRoot) {
102
+ const findings = [];
103
+ const relPath = path.relative(projectRoot, filepath);
104
+
105
+ let content;
106
+ try {
107
+ content = fs.readFileSync(filepath, 'utf8');
108
+ } catch {
109
+ return findings;
110
+ }
111
+
112
+ const lines = content.split('\n');
113
+ for (let i = 0; i < lines.length; i++) {
114
+ const stripped = lines[i].trim();
115
+ // Skip comments
116
+ if (stripped.startsWith('//') || stripped.startsWith('#') || stripped.startsWith('*')) {
117
+ continue;
118
+ }
119
+
120
+ for (const [pattern, severity, category, message] of PATTERNS) {
121
+ if (pattern.test(stripped)) {
122
+ findings.push({
123
+ severity,
124
+ category,
125
+ file: relPath,
126
+ line: i + 1,
127
+ message,
128
+ snippet: stripped.slice(0, 120),
129
+ });
130
+ }
131
+ }
132
+ }
133
+
134
+ return findings;
135
+ }
136
+
137
+
138
+ /**
139
+ * Recursively walk a directory yield file paths.
140
+ * @param {string} dir - Directory to walk.
141
+ * @param {Set<string>} skipDirs - Directory names to skip.
142
+ * @returns {string[]} Array of file paths.
143
+ */
144
+ function walkDir(dir, skipDirs) {
145
+ const results = [];
146
+ let entries;
147
+ try {
148
+ entries = fs.readdirSync(dir, { withFileTypes: true });
149
+ } catch {
150
+ return results;
151
+ }
152
+
153
+ for (const entry of entries) {
154
+ if (entry.isDirectory()) {
155
+ if (!skipDirs.has(entry.name)) {
156
+ results.push(...walkDir(path.join(dir, entry.name), skipDirs));
157
+ }
158
+ } else if (entry.isFile()) {
159
+ results.push(path.join(dir, entry.name));
160
+ }
161
+ }
162
+ return results;
163
+ }
164
+
165
+
166
+ /**
167
+ * Scan all source files in a directory.
168
+ * @param {string} projectRoot - Root directory to scan.
169
+ * @param {string[]|null} targetFiles - Specific files to scan, or null for full scan.
170
+ * @returns {Array} Array of finding objects.
171
+ */
172
+ function scanDirectory(projectRoot, targetFiles) {
173
+ const allFindings = [];
174
+
175
+ if (targetFiles && targetFiles.length > 0) {
176
+ for (const fpath of targetFiles) {
177
+ const absPath = path.isAbsolute(fpath) ? fpath : path.join(projectRoot, fpath);
178
+ if (fs.existsSync(absPath) && fs.statSync(absPath).isFile()) {
179
+ allFindings.push(...scanFile(absPath, projectRoot));
180
+ }
181
+ }
182
+ return allFindings;
183
+ }
184
+
185
+ const files = walkDir(projectRoot, SKIP_DIRS);
186
+ for (const filepath of files) {
187
+ const ext = path.extname(filepath);
188
+ if (!SOURCE_EXTENSIONS.has(ext)) continue;
189
+ allFindings.push(...scanFile(filepath, projectRoot));
190
+ }
191
+
192
+ return allFindings;
193
+ }
194
+
195
+
196
+ /**
197
+ * Print findings filtered by minimum severity. Returns count of displayed findings.
198
+ */
199
+ function printFindings(findings, minSeverity) {
200
+ const minRank = SEVERITY_RANK[minSeverity] ?? 3;
201
+ const filtered = findings
202
+ .filter(f => (SEVERITY_RANK[f.severity] ?? 3) <= minRank)
203
+ .sort((a, b) => (SEVERITY_RANK[a.severity] ?? 3) - (SEVERITY_RANK[b.severity] ?? 3));
204
+
205
+ if (filtered.length === 0) {
206
+ console.log(`\n ${GREEN}✅ No security issues found at severity '${minSeverity}' or above${RESET}`);
207
+ return 0;
208
+ }
209
+
210
+ let currentCategory = '';
211
+ for (const finding of filtered) {
212
+ if (finding.category !== currentCategory) {
213
+ currentCategory = finding.category;
214
+ console.log(`\n ${BOLD}${currentCategory}${RESET}`);
215
+ }
216
+ const color = SEVERITY_COLORS[finding.severity] || '';
217
+ console.log(` ${color}[${finding.severity.toUpperCase()}]${RESET} ${finding.file}:${finding.line}`);
218
+ console.log(` ${finding.message}`);
219
+ console.log(` ${MAGENTA}→ ${finding.snippet}${RESET}`);
220
+ }
221
+
222
+ return filtered.length;
223
+ }
224
+
225
+
226
+ /**
227
+ * Parse CLI arguments manually (no external dependencies).
228
+ */
229
+ function parseArgs(argv) {
230
+ const args = { path: null, severity: 'low', files: null };
231
+ const raw = argv.slice(2);
232
+
233
+ for (let i = 0; i < raw.length; i++) {
234
+ if (raw[i] === '--severity' && raw[i + 1]) {
235
+ args.severity = raw[++i];
236
+ } else if (raw[i] === '--files') {
237
+ args.files = [];
238
+ while (i + 1 < raw.length && !raw[i + 1].startsWith('--')) {
239
+ args.files.push(raw[++i]);
240
+ }
241
+ } else if (!raw[i].startsWith('--') && !args.path) {
242
+ args.path = raw[i];
243
+ }
244
+ }
245
+ return args;
246
+ }
247
+
248
+
249
+ function main() {
250
+ const args = parseArgs(process.argv);
251
+
252
+ if (!args.path) {
253
+ console.error(`Usage: node security_scan.js <path> [--severity critical|high|medium|low] [--files ...]`);
254
+ process.exit(1);
255
+ }
256
+
257
+ const projectRoot = path.resolve(args.path);
258
+ if (!fs.existsSync(projectRoot) || !fs.statSync(projectRoot).isDirectory()) {
259
+ console.error(` ${RED}❌ Directory not found: ${projectRoot}${RESET}`);
260
+ process.exit(1);
261
+ }
262
+
263
+ console.log(`${BOLD}Tribunal — security_scan.js${RESET}`);
264
+ console.log(`Project: ${projectRoot}`);
265
+ console.log(`Severity filter: ${args.severity}+`);
266
+
267
+ const findings = scanDirectory(projectRoot, args.files);
268
+ const count = printFindings(findings, args.severity);
269
+
270
+ // Summary
271
+ console.log(`\n${BOLD}━━━ Security Scan Summary ━━━${RESET}`);
272
+ const bySeverity = {};
273
+ for (const f of findings) {
274
+ bySeverity[f.severity] = (bySeverity[f.severity] || 0) + 1;
275
+ }
276
+
277
+ for (const sev of ['critical', 'high', 'medium', 'low']) {
278
+ const c = bySeverity[sev] || 0;
279
+ if (c > 0) {
280
+ const color = SEVERITY_COLORS[sev] || '';
281
+ console.log(` ${color}${sev.toUpperCase()}: ${c}${RESET}`);
282
+ }
283
+ }
284
+
285
+ if (count === 0) {
286
+ console.log(` ${GREEN}✅ No issues found — scan passed${RESET}`);
287
+ } else {
288
+ const criticalHigh = (bySeverity.critical || 0) + (bySeverity.high || 0);
289
+ if (criticalHigh > 0) {
290
+ console.log(`\n ${RED}${BOLD}⚠️ ${criticalHigh} critical/high issue(s) require immediate attention${RESET}`);
291
+ }
292
+ }
293
+
294
+ process.exit((bySeverity.critical || 0) > 0 ? 1 : 0);
295
+ }
296
+
297
+
298
+ // ━━━ Exports for testing & programmatic use ━━━
299
+ module.exports = { scanFile, scanDirectory, printFindings, PATTERNS, SEVERITY_RANK };
300
+
301
+ if (require.main === module) {
302
+ main();
303
+ }