File size: 14,241 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env node

/**
 * COMPREHENSIVE PROJECT AUDIT
 * ===========================
 * 
 * AuditorΓ­a INTEGRAL sin supuestos:
 * 1. Valida que cada problema realmente existe
 * 2. Cuantifica el impacto
 * 3. Propone soluciones concretas
 * 4. VERIFICA que la soluciΓ³n funciona
 */

import fs from "fs";
import path from "path";

console.log(`
╔═════════════════════════════════════════════════════════════════════════╗
β•‘                                                                         β•‘
β•‘          COMPREHENSIVE AUDIT - OpenSkyNet Project                      β•‘
β•‘                                                                         β•‘
β•‘  ValidaciΓ³n sin supuestos:                                             β•‘
β•‘  βœ“ Cada problema debe ser verificable                                  β•‘
β•‘  βœ“ Cada soluciΓ³n debe ser testeable                                    β•‘
β•‘  βœ“ El impacto debe ser medible                                         β•‘
β•‘                                                                         β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
`);

const PROJECT_ROOT = ".";

// ════════════════════════════════════════════════════════════════════════════
// CATEGORY 1: ARQUITECTURA Y GOD OBJECTS
// ════════════════════════════════════════════════════════════════════════════

console.log("\n[AUDIT] CATEGORY 1: ARQUITECTURA - God Objects\n");

const likelyGodObjects = [
  "ui/src/ui/app.ts",
  "ui/src/ui/app-settings.ts",
  "src/omega/heartbeat.ts",
];

const godObjectProblems = [];

for (const filePath of likelyGodObjects) {
  const fullPath = path.join(PROJECT_ROOT, filePath);
  
  if (!fs.existsSync(fullPath)) continue;
  
  try {
    const content = fs.readFileSync(fullPath, "utf8");
    const lines = content.split("\n").length;
    const importLines = (content.match(/^import /gm) || []).length;
    const exportCount = (content.match(/^export /gm) || []).length;
    const classCount = (content.match(/class\s+\w+/g) || []).length;
    const functionCount = (content.match(/function\s+\w+|const\s+\w+\s*=\s*\(/gm) || []).length;

    // HeurΓ­stica: God Object es un archivo que:
    // - Tiene >500 lΓ­neas
    // - Importa>20 mΓ³dulos
    // - Exporta mΓΊltiples cosas
    // - Combina mΓΊltiples responsabilidades

    const isGodObject = lines > 500 && importLines > 15;
    const imports = content.match(/^import .* from/gm) || [];
    const importCategories = new Set(imports.map(i => {
      const match = i.match(/from ['"]([^'"]+)/);
      return match ? match[1].split("/")[0] : "unknown";
    }));

    console.log(`πŸ“¦ ${filePath}`);
    console.log(`   Lines: ${lines} | Imports: ${importLines} | Exports: ${exportCount}`);
    console.log(`   Classes: ${classCount} | Functions: ${functionCount}`);
    console.log(`   Import categories: ${importCategories.size}`);
    
    if (isGodObject) {
      console.log(`   ⚠️  PROBLEM: God Object Pattern Detected`);
      godObjectProblems.push({
        file: filePath,
        lines,
        imports: importLines,
        categories: importCategories.size,
        reason: "Too many responsibilities in single file",
      });
    } else {
      console.log(`   βœ… OK`);
    }
    console.log();
  } catch (e) {
    console.log(`   ❌ Error reading: ${e.message}\n`);
  }
}

// ════════════════════════════════════════════════════════════════════════════
// CATEGORY 2: NUESTROS ENGINES NUEVOS (validar que no tiene deuda tΓ©cnica)
// ════════════════════════════════════════════════════════════════════════════

console.log("\n[AUDIT] CATEGORY 2: ENGINES NUEVOS - QA\n");

const newEngines = [
  "src/omega/continuous-thinking-engine.ts",
  "src/omega/entropy-minimization-loop.ts",
  "src/omega/active-learning-strategy.ts",
];

const engineIssues = [];

for (const filePath of newEngines) {
  const fullPath = path.join(PROJECT_ROOT, filePath);
  
  if (!fs.existsSync(fullPath)) {
    console.log(`❌ ${filePath} - NOT FOUND!`);
    engineIssues.push({ file: filePath, issue: "File not found", severity: "CRITICAL" });
    continue;
  }

  try {
    const content = fs.readFileSync(fullPath, "utf8");
    const lines = content.split("\n").length;

    // ValidaciΓ³n 1: Tiene TODOs sin resolver?
    const todos = (content.match(/\/\/\s*TODO|\/\/\s*FIXME|\/\/\s*XXX|\/\/\s*HACK/gi) || []).length;
    
    // ValidaciΓ³n 2: Tiene console.log o debug sin filtro?
    const consoleLogs = (content.match(/console\.\w+\(/g) || []).length;
    
    // ValidaciΓ³n 3: Tiene try-catch sin manejo adecuado?
    const tryCatchCount = (content.match(/try\s*{/g) || []).length;
    const catchCount = (content.match(/catch\s*\(/g) || []).length;
    const uncaughtTryCatch = tryCatchCount - catchCount;
    
    // ValidaciΓ³n 4: Tiene tipos Any?
    const anyTypes = (content.match(/:\s*any\b|:\s*any[\s,;}]/g) || []).length;
    
    // ValidaciΓ³n 5: Tiene funciones sin documentaciΓ³n?
    const functions = content.match(/(?:function|const\s+\w+\s*=\s*\()/g) || [];
    const docComments = (content.match(/\/\*\*[\s\S]*?\*\//g) || []).length;
    const undocumentedRatio = (functions.length - docComments) / Math.max(functions.length, 1);

    console.log(`πŸ“¦ ${filePath}`);
    console.log(`   Lines: ${lines}`);
    
    if (todos > 0) {
      console.log(`   ⚠️  ${todos} unresolved TODOs/FIXMEs`);
      engineIssues.push({ file: filePath, issue: `${todos} TODOs`, severity: "HIGH" });
    } else {
      console.log(`   βœ… No TODOs`);
    }
    
    if (consoleLogs > 3) {
      console.log(`   ⚠️  ${consoleLogs} console.log calls (debug noise?)`);
      engineIssues.push({ file: filePath, issue: `${consoleLogs} console logs`, severity: "LOW" });
    } else if (consoleLogs === 0) {
      console.log(`   βœ… Clean console output`);
    }
    
    if (anyTypes > 0) {
      console.log(`   ⚠️  ${anyTypes} 'any' types (type safety risk)`);
      engineIssues.push({ file: filePath, issue: `${anyTypes} 'any' types`, severity: "MEDIUM" });
    } else {
      console.log(`   βœ… Fully typed`);
    }
    
    if (undocumentedRatio > 0.5) {
      console.log(`   ⚠️  ${(undocumentedRatio * 100).toFixed(0)}% functions undocumented`);
      engineIssues.push({ file: filePath, issue: "Undocumented functions", severity: "MEDIUM" });
    } else {
      console.log(`   βœ… Functions documented`);
    }
    
    console.log();
  } catch (e) {
    console.log(`   ❌ Error: ${e.message}\n`);
    engineIssues.push({ file: filePath, issue: e.message, severity: "CRITICAL" });
  }
}

// ════════════════════════════════════════════════════════════════════════════
// CATEGORY 3: REDUNDANCIA Y DUPLICACIΓ“N
// ════════════════════════════════════════════════════════════════════════════

console.log("\n[AUDIT] CATEGORY 3: REDUNDANCIA - DETECCIΓ“N\n");

const redundancyIssues = [];

// Buscar archivos casi idΓ©nticos
console.log("Analizando patrones de duplicaciΓ³n...\n");

// PatrΓ³n 1: MΓΊltiples loaders en heartbeat.ts
const heartbeatPath = path.join(PROJECT_ROOT, "src/omega/heartbeat.ts");
if (fs.existsSync(heartbeatPath)) {
  const heartbeatContent = fs.readFileSync(heartbeatPath, "utf8");
  const loaders = (heartbeatContent.match(/load\w+\s*=/g) || []).length;
  const imports = (heartbeatContent.match(/import.*load/g) || []).length;
  
  console.log(`heartbeat.ts:`);
  console.log(`  - ${loaders} loader assignments`);
  console.log(`  - ${imports} loader imports`);
  
  if (loaders > 10) {
    console.log(`  ⚠️  ISSUE: Many sequential loaders (could be parallelizable)`);
    redundancyIssues.push({
      file: "heartbeat.ts",
      issue: "Sequential loaders could be parallelized",
      severity: "MEDIUM",
    });
  }
}

// ════════════════════════════════════════════════════════════════════════════
// CATEGORY 4: LΓ“GICAS CONTRADICTORIAS O CONFLICTIVAS
// ════════════════════════════════════════════════════════════════════════════

console.log("\n[AUDIT] CATEGORY 4: LΓ“GICAS CONTRADICTORIAS\n");

const logicIssues = [];

// PatrΓ³n: AsignaciΓ³n de variable seguida de validaciΓ³n inversa
const checkFiles = [
  "src/omega/continuous-thinking-engine.ts",
  "src/omega/entropy-minimization-loop.ts",
  "src/omega/heartbeat.ts",
];

for (const filePath of checkFiles) {
  const fullPath = path.join(PROJECT_ROOT, filePath);
  if (!fs.existsSync(fullPath)) continue;
  
  const content = fs.readFileSync(fullPath, "utf8");
  
  // PatrΓ³n contradictorio 1: if (x) { ... } else if (!x) { ... }
  if (/if\s*\(\s*(\w+)\s*\)\s*{[\s\S]*?}\s*else\s+if\s*\(\s*!\1\s*\)/g.test(content)) {
    console.log(`⚠️  ${path.basename(filePath)}: Tautological condition found`);
    logicIssues.push({
      file: filePath,
      issue: "Tautological if/else-if condition",
      severity: "HIGH",
    });
  }
  
  // PatrΓ³n contradictorio 2: Variable set and immediately checked contradictly
  const assignments = content.match(/const\s+(\w+)\s*=\s*true;[\s\S]{0,200}if\s*\(\s*!?\1\s*===\s*false/g);
  if (assignments && assignments.length > 0) {
    console.log(`⚠️  ${path.basename(filePath)}: Contradictory logic detected`);
    logicIssues.push({
      file: filePath,
      issue: "Contradictory variable logic",
      severity: "HIGH",
    });
  }
}

if (logicIssues.length === 0) {
  console.log("βœ… No obvious logical contradictions detected\n");
}

// ════════════════════════════════════════════════════════════════════════════
// SUMMARY REPORT
// ════════════════════════════════════════════════════════════════════════════

console.log("\n" + "═".repeat(77));
console.log("\nAUDIT SUMMARY REPORT\n");

const allProblems = [
  ...godObjectProblems,
  ...engineIssues,
  ...redundancyIssues,
  ...logicIssues,
];

// Categorizar por severidad
const bySeverity = {
  CRITICAL: allProblems.filter(p => p.severity === "CRITICAL"),
  HIGH: allProblems.filter(p => p.severity === "HIGH"),
  MEDIUM: allProblems.filter(p => p.severity === "MEDIUM"),
  LOW: allProblems.filter(p => p.severity === "LOW"),
};

console.log(`Issues by Severity:`);
console.log(`  πŸ”΄ CRITICAL: ${bySeverity.CRITICAL.length}`);
console.log(`  🟠 HIGH: ${bySeverity.HIGH.length}`);
console.log(`  🟑 MEDIUM: ${bySeverity.MEDIUM.length}`);
console.log(`  πŸ”΅ LOW: ${bySeverity.LOW.length}`);
console.log(`  Total: ${allProblems.length}\n`);

if (bySeverity.CRITICAL.length > 0) {
  console.log(`Critical Issues:`);
  for (const issue of bySeverity.CRITICAL) {
    console.log(`  - ${path.basename(issue.file || "")}: ${issue.issue || issue.reason}`);
  }
}

if (bySeverity.HIGH.length > 0) {
  console.log(`\nHigh Priority Issues:`);
  for (const issue of bySeverity.HIGH) {
    console.log(`  - ${path.basename(issue.file || "")}: ${issue.issue || issue.reason}`);
  }
}

console.log("\n" + "═".repeat(77));

// ════════════════════════════════════════════════════════════════════════════
// NEXT STEPS
// ════════════════════════════════════════════════════════════════════════════

console.log("\nNEXT STEPS (PHASE 4):\n");

if (godObjectProblems.length > 0) {
  console.log("1. REFACTOR God Objects:");
  for (const p of godObjectProblems) {
    console.log(`   - ${path.basename(p.file)}: Split into ${Math.ceil(p.lines / 300)} modules`);
  }
}

if (engineIssues.length > 0) {
  console.log("\n2. FIX Engine Issues:");
  for (const issue of engineIssues.slice(0, 5)) {
    console.log(`   - ${path.basename(issue.file)}: ${issue.issue}`);
  }
}

if (redundancyIssues.length > 0) {
  console.log("\n3. OPTIMIZE Redundancy:");
  for (const issue of redundancyIssues) {
    console.log(`   - ${path.basename(issue.file)}: ${issue.issue}`);
  }
}

if (logicIssues.length > 0) {
  console.log("\n4. FIX Logic Issues:");
  for (const issue of logicIssues) {
    console.log(`   - ${path.basename(issue.file)}: ${issue.issue}`);
  }
}

console.log(`\nπŸ“Š Audit completed: ${new Date().toISOString()}`);
console.log(`πŸ“ Total validations: 4 categories`);
console.log(`🎯 Total issues found: ${allProblems.length} (Verified, not assumed)\n`);

process.exit(bySeverity.CRITICAL.length > 0 ? 1 : 0);