# All files scoring.ts

87.32% Statements 62/71
81.81% Branches 72/88
66.66% Functions 2/3
91.66% Lines 55/60

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
    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

    7x

    8x

    8x

    7x

    8x

    8x

    7x

    8x
    8x

    8x

    7x

    8x

    15x
    7x
    8x

    15x

    15x
    15x

    15x

    7x

    15x
    9x

    8x
    1x

    7x
    2x

    8x
    2x

    7x
    1x

    9x

    8x
    15x
    10x

    2x

    15x

    15x
    9x
    4x

    15x
    9x
    10x

    2x

    7x
    10x
    2x
    8x
    1x

    7x
    1x
    9x
    15x

    15x

    15x

    8x

     import {
      calculateMonthlyCost,
      calculateProductivityImpact,
      DEFAULT_COST_CONFIG,
      type CostConfig,
      ToolName,
    } from '@aiready/core';
    import type { ToolScoringOutput } from '@aiready/core';
    import type { ContextSummary } from './types';

    /**
     * Calculate AI Readiness Score for context efficiency (0-100)
     */
    export function calculateContextScore(
      summary: ContextSummary,
      costConfig?: Partial<CostConfig>
    ): ToolScoringOutput {
      const {
        avgContextBudget,
        maxContextBudget,
        avgImportDepth,
        maxImportDepth,
        avgFragmentation,
        criticalIssues,
        majorIssues,
      } = summary;

      const budgetScore =
        avgContextBudget < 5000
          ? 100
          : Math.max(0, 100 - (avgContextBudget - 5000) / 150);

      const depthScore =
        avgImportDepth < 5 ? 100 : Math.max(0, 100 - (avgImportDepth - 5) * 10);

      const fragmentationScore =
        avgFragmentation < 0.3
          ? 100
          : Math.max(0, 100 - (avgFragmentation - 0.3) * 200);

      const criticalPenalty = criticalIssues * 10;
      const majorPenalty = majorIssues * 3;

      const maxBudgetPenalty =
        maxContextBudget > 15000
          ? Math.min(20, (maxContextBudget - 15000) / 500)
          : 0;

      const rawScore =
        budgetScore * 0.4 + depthScore * 0.3 + fragmentationScore * 0.3;
      const finalScore =
        rawScore - criticalPenalty - majorPenalty - maxBudgetPenalty;

      const score = Math.max(0, Math.min(100, Math.round(finalScore)));

      const factors = [
        {
          name: 'Context Budget',
          impact: Math.round(budgetScore * 0.4 - 40),
          description: `Avg ${Math.round(avgContextBudget)} tokens per file ${avgContextBudget < 5000 ? '(excellent)' : avgContextBudget < 10000 ? '(acceptable)' : '(high)'}`,
        },
        {
          name: 'Import Depth',
          impact: Math.round(depthScore * 0.3 - 30),
          description: `Avg ${avgImportDepth.toFixed(1)} levels ${avgImportDepth < 5 ? '(excellent)' : avgImportDepth < 8 ? '(acceptable)' : '(deep)'}`,
        },
        {
          name: 'Fragmentation',
          impact: Math.round(fragmentationScore * 0.3 - 30),
          description: `${(avgFragmentation * 100).toFixed(0)}% fragmentation ${avgFragmentation < 0.3 ? '(well-organized)' : avgFragmentation < 0.5 ? '(moderate)' : '(high)'}`,
        },
      ];

      if (criticalIssues > 0) {
        factors.push({
          name: 'Critical Issues',
          impact: -criticalPenalty,
          description: `${criticalIssues} critical context issue${criticalIssues > 1 ? 's' : ''}`,
        });
      }

      if (majorIssues > 0) {
        factors.push({
          name: 'Major Issues',
          impact: -majorPenalty,
          description: `${majorIssues} major context issue${majorIssues > 1 ? 's' : ''}`,
        });
      }

      if (maxBudgetPenalty > 0) {
        factors.push({
          name: 'Extreme File Detected',
          impact: -Math.round(maxBudgetPenalty),
          description: `One file requires ${Math.round(maxContextBudget)} tokens (very high)`,
        });
      }

      const recommendations: ToolScoringOutput['recommendations'] = [];

      if (avgContextBudget > 10000) {
        const estimatedImpact = Math.min(
          15,
          Math.round((avgContextBudget - 10000) / 1000)
        );
        recommendations.push({
          action: 'Reduce file dependencies to lower context requirements',
          estimatedImpact,
          priority: 'high',
        });
      }

      if (avgImportDepth > 8) {
        const estimatedImpact = Math.min(10, Math.round((avgImportDepth - 8) * 2));
        recommendations.push({
          action: 'Flatten import chains to reduce depth',
          estimatedImpact,
          priority: avgImportDepth > 10 ? 'high' : 'medium',
        });
      }

      if (avgFragmentation > 0.5) {
        const estimatedImpact = Math.min(
          12,
          Math.round((avgFragmentation - 0.5) * 40)
        );
        recommendations.push({
          action: 'Consolidate related code into cohesive modules',
          estimatedImpact,
          priority: 'medium',
        });
      }

      if (maxContextBudget > 20000) {
        recommendations.push({
          action: `Split large file (${Math.round(maxContextBudget)} tokens) into smaller modules`,
          estimatedImpact: 8,
          priority: 'high',
        });
      }

      const cfg = { ...DEFAULT_COST_CONFIG, ...costConfig };
      const estimatedMonthlyCost = calculateMonthlyCost(
        avgContextBudget * (summary.totalFiles || 1),
        cfg
      );

      const issues = [
        ...Array(criticalIssues).fill({ severity: 'critical' as const }),
        ...Array(majorIssues).fill({ severity: 'major' as const }),
      ];
      const productivityImpact = calculateProductivityImpact(issues);

      return {
        toolName: ToolName.ContextAnalyzer,
        score,
        rawMetrics: {
          avgContextBudget: Math.round(avgContextBudget),
          maxContextBudget: Math.round(maxContextBudget),
          avgImportDepth: Math.round(avgImportDepth * 10) / 10,
          maxImportDepth,
          avgFragmentation: Math.round(avgFragmentation * 100) / 100,
          criticalIssues,
          majorIssues,
          estimatedMonthlyCost,
          estimatedDeveloperHours: productivityImpact.totalHours,
        },
        factors,
        recommendations,
      };
    }

    export function mapScoreToRating(score: number): string {
      if (score >= 90) return 'excellent';
      if (score >= 75) return 'good';
      if (score >= 60) return 'fair';
      if (score >= 40) return 'needs work';
      return 'critical';
    }