# All files / src issue-analyzer.ts

100% Statements 56/56
80% Branches 24/30
100% Functions 1/1
100% Lines 52/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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

    17x

    17x
    17x
    17x
    17x

    17x
    1x
    1x
    1x

    1x

    17x
    1x
    1x
    1x
    1x
    16x
    1x
    1x

    1x
    1x

    17x
    1x
    1x

    1x

    1x
    16x
    1x
    1x

    1x
    1x

    17x
    1x
    1x

    1x

    1x
    16x
    1x
    1x
    1x
    1x

    17x
    1x
    1x
    1x

    1x
    1x

    17x
    1x
    1x
    1x
    1x

    17x

     import { Severity, isBuildArtifact } from '@aiready/core';

    // Re-export for testing
    export { isBuildArtifact };

    /**
     * Internal issue analysis logic
     */
    /**
     * Analyzes architectural issues related to AI context windows.
     * Detects problems like high import depth, large context budgets,
     * circular dependencies, and low cohesion.
     *
     * @param params - Combined metrics and graph data for a module
     * @returns List of identified issues with severity and suggestions
     */
    export function analyzeIssues(params: {
      file: string;
      importDepth: number;
      contextBudget: number;
      cohesionScore: number;
      fragmentationScore: number;
      maxDepth: number;
      maxContextBudget: number;
      minCohesion: number;
      maxFragmentation: number;
      circularDeps: string[][];
    }): {
      severity: Severity;
      issues: string[];
      recommendations: string[];
      potentialSavings: number;
    } {
      const {
        file,
        importDepth,
        contextBudget,
        cohesionScore,
        fragmentationScore,
        maxDepth,
        maxContextBudget,
        minCohesion,
        maxFragmentation,
        circularDeps,
      } = params;

      const issues: string[] = [];
      const recommendations: string[] = [];
      let severity: Severity = Severity.Info;
      let potentialSavings = 0;

      // Check circular dependencies (CRITICAL)
      if (circularDeps.length > 0) {
        severity = Severity.Critical;
        issues.push(`Part of ${circularDeps.length} circular dependency chain(s)`);
        recommendations.push(
          'Break circular dependencies by extracting interfaces or using dependency injection'
        );
        potentialSavings += contextBudget * 0.2;
      }

      // Check import depth
      if (importDepth > maxDepth * 1.5) {
        severity = Severity.Critical;
        issues.push(`Import depth ${importDepth} exceeds limit by 50%`);
        recommendations.push('Flatten dependency tree or use facade pattern');
        potentialSavings += contextBudget * 0.3;
      } else if (importDepth > maxDepth) {
        Eif (severity !== Severity.Critical) severity = Severity.Major;
        issues.push(
          `Import depth ${importDepth} exceeds recommended maximum ${maxDepth}`
        );
        recommendations.push('Consider reducing dependency depth');
        potentialSavings += contextBudget * 0.15;
      }

      // Check context budget
      if (contextBudget > maxContextBudget * 1.5) {
        severity = Severity.Critical;
        issues.push(
          `Context budget ${contextBudget.toLocaleString()} tokens is 50% over limit`
        );
        recommendations.push(
          'Split into smaller modules or reduce dependency tree'
        );
        potentialSavings += contextBudget * 0.4;
      } else if (contextBudget > maxContextBudget) {
        Eif (severity !== Severity.Critical) severity = Severity.Major;
        issues.push(
          `Context budget ${contextBudget.toLocaleString()} exceeds ${maxContextBudget.toLocaleString()}`
        );
        recommendations.push('Reduce file size or dependencies');
        potentialSavings += contextBudget * 0.2;
      }

      // Check cohesion
      if (cohesionScore < minCohesion * 0.5) {
        Eif (severity !== Severity.Critical) severity = Severity.Major;
        issues.push(
          `Very low cohesion (${(cohesionScore * 100).toFixed(0)}%) - mixed concerns`
        );
        recommendations.push(
          'Split file by domain - separate unrelated functionality'
        );
        potentialSavings += contextBudget * 0.25;
      } else if (cohesionScore < minCohesion) {
        Eif (severity === Severity.Info) severity = Severity.Minor;
        issues.push(`Low cohesion (${(cohesionScore * 100).toFixed(0)}%)`);
        recommendations.push('Consider grouping related exports together');
        potentialSavings += contextBudget * 0.1;
      }

      // Check fragmentation
      if (fragmentationScore > maxFragmentation) {
        Eif (severity === Severity.Info || severity === Severity.Minor)
          severity = Severity.Minor;
        issues.push(
          `High fragmentation (${(fragmentationScore * 100).toFixed(0)}%) - scattered implementation`
        );
        recommendations.push('Consolidate with related files in same domain');
        potentialSavings += contextBudget * 0.3;
      }

      // Don't create issues when there are no real problems
      // This prevents false positives that artificially lower scores

      // Detect build artifacts
      if (isBuildArtifact(file)) {
        issues.push('Detected build artifact (bundled/output file)');
        recommendations.push('Exclude build outputs from analysis');
        severity = Severity.Info;
        potentialSavings = 0;
      }

      return {
        severity,
        issues,
        recommendations,
        potentialSavings: Math.floor(potentialSavings),
      };
    }