All files / src scoring.ts

98.3% Statements 58/59
91.83% Branches 45/49
50% Functions 1/2
98.3% Lines 58/59

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 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                            12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x   12x 12x 12x                                                   16x       16x                   16x                 16x                   16x       16x           16x               16x 16x         2x       16x         16x       16x   16x                                       16x 2x             16x 6x             16x 3x             16x 5x             16x   16x 1x       1x             16x 1x       1x             16x 3x       3x             16x 2x             16x 16x         16x       16x   16x                                                          
import {
  calculateMonthlyCost,
  calculateProductivityImpact,
  DEFAULT_COST_CONFIG,
  type CostConfig,
  ToolName,
  getRatingSlug,
} from '@aiready/core';
import type { ToolScoringOutput } from '@aiready/core';
import type { ContextSummary } from './types';
 
/**
 * Thresholds and weights for context efficiency scoring.
 */
const BUDGET_EXCELLENT_THRESHOLD = 8000;
const BUDGET_PENALTY_RATE = 200;
const DEPTH_EXCELLENT_THRESHOLD = 8;
const DEPTH_PENALTY_WEIGHT = 5;
const FRAGMENTATION_EXCELLENT_THRESHOLD = 0.5;
const FRAGMENTATION_PENALTY_WEIGHT = 100;
const MAX_CRITICAL_PENALTY = 20;
const CRITICAL_ISSUE_WEIGHT = 3;
const MAX_MAJOR_PENALTY = 15;
const MAJOR_ISSUE_WEIGHT = 1;
const EXTREME_FILE_THRESHOLD = 15000;
const MAX_EXTREME_PENALTY = 20;
const EXTREME_PENALTY_DIVISOR = 500;
const FRAGMENTATION_BONUS_THRESHOLD = 0.2;
const ORGANIZATION_BONUS = 5;
const MAX_TOTAL_PENALTY = 30;
 
const WEIGHT_BUDGET = 0.35;
const WEIGHT_DEPTH = 0.25;
const WEIGHT_FRAGMENTATION = 0.25;
 
/**
 * Calculate AI Readiness Score for context efficiency (0-100).
 *
 * Evaluates how efficiently an AI model can process the project's code context
 * based on token budgets, import depth, and file fragmentation.
 *
 * @param summary - Consolidated context summary from the scan.
 * @param costConfig - Optional configuration for business value calculations.
 * @returns Standardized scoring output for the context analyzer tool.
 * @lastUpdated 2026-03-18
 */
export function calculateContextScore(
  summary: ContextSummary,
  costConfig?: Partial<CostConfig>
): ToolScoringOutput {
  const {
    avgContextBudget,
    maxContextBudget,
    avgImportDepth,
    maxImportDepth,
    avgFragmentation,
    criticalIssues,
    majorIssues,
    totalFiles,
  } = summary;
 
  // More reasonable thresholds for modern codebases
  const budgetScore =
    avgContextBudget < BUDGET_EXCELLENT_THRESHOLD
      ? 100
      : Math.max(
          0,
          100 -
            (avgContextBudget - BUDGET_EXCELLENT_THRESHOLD) /
              BUDGET_PENALTY_RATE
        );
 
  const depthScore =
    avgImportDepth < DEPTH_EXCELLENT_THRESHOLD
      ? 100
      : Math.max(
          0,
          100 -
            (avgImportDepth - DEPTH_EXCELLENT_THRESHOLD) * DEPTH_PENALTY_WEIGHT
        );
 
  const fragmentationScore =
    avgFragmentation < FRAGMENTATION_EXCELLENT_THRESHOLD
      ? 100
      : Math.max(
          0,
          100 -
            (avgFragmentation - FRAGMENTATION_EXCELLENT_THRESHOLD) *
              FRAGMENTATION_PENALTY_WEIGHT
        );
 
  // Cap penalties to prevent score going to 0
  const criticalPenalty = Math.min(
    MAX_CRITICAL_PENALTY,
    criticalIssues * CRITICAL_ISSUE_WEIGHT
  );
  const majorPenalty = Math.min(
    MAX_MAJOR_PENALTY,
    majorIssues * MAJOR_ISSUE_WEIGHT
  );
 
  const maxBudgetPenalty =
    maxContextBudget > EXTREME_FILE_THRESHOLD
      ? Math.min(
          MAX_EXTREME_PENALTY,
          (maxContextBudget - EXTREME_FILE_THRESHOLD) / EXTREME_PENALTY_DIVISOR
        )
      : 0;
 
  // Add bonus for well-organized codebases
  let bonus = 0;
  if (
    criticalIssues === 0 &&
    majorIssues === 0 &&
    avgFragmentation < FRAGMENTATION_BONUS_THRESHOLD
  ) {
    bonus = ORGANIZATION_BONUS;
  }
 
  const rawScore =
    budgetScore * WEIGHT_BUDGET +
    depthScore * WEIGHT_DEPTH +
    fragmentationScore * WEIGHT_FRAGMENTATION +
    bonus;
  const finalScore =
    rawScore -
    Math.min(MAX_TOTAL_PENALTY, criticalPenalty + majorPenalty) -
    maxBudgetPenalty;
 
  const score = Math.max(0, Math.min(100, Math.round(finalScore)));
 
  const factors = [
    {
      name: 'Context Budget',
      impact: Math.round(budgetScore * WEIGHT_BUDGET - WEIGHT_BUDGET * 100),
      description: `Avg ${Math.round(avgContextBudget)} tokens per file ${avgContextBudget < BUDGET_EXCELLENT_THRESHOLD ? '(excellent)' : avgContextBudget < 12000 ? '(acceptable)' : '(high)'}`,
    },
    {
      name: 'Import Depth',
      impact: Math.round(depthScore * WEIGHT_DEPTH - WEIGHT_DEPTH * 100),
      description: `Avg ${avgImportDepth.toFixed(1)} levels ${avgImportDepth < DEPTH_EXCELLENT_THRESHOLD ? '(excellent)' : avgImportDepth < 12 ? '(acceptable)' : '(deep)'}`,
    },
    {
      name: 'Fragmentation',
      impact: Math.round(
        fragmentationScore * WEIGHT_FRAGMENTATION - WEIGHT_FRAGMENTATION * 100
      ),
      description: `${(avgFragmentation * 100).toFixed(0)}% fragmentation ${avgFragmentation < 0.3 ? '(well-organized)' : avgFragmentation < 0.5 ? '(moderate)' : '(high)'}`,
    },
  ];
 
  if (bonus > 0) {
    factors.push({
      name: 'Well-Organized Codebase',
      impact: bonus,
      description: 'No critical/major issues and low fragmentation',
    });
  }
 
  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 > 12000) {
    const estimatedImpact = Math.min(
      15,
      Math.round((avgContextBudget - 12000) / 1000)
    );
    recommendations.push({
      action: 'Reduce file dependencies to lower context requirements',
      estimatedImpact,
      priority: 'high',
    });
  }
 
  if (avgImportDepth > 10) {
    const estimatedImpact = Math.min(
      10,
      Math.round((avgImportDepth - 10) * 1.5)
    );
    recommendations.push({
      action: 'Flatten import chains to reduce depth',
      estimatedImpact,
      priority: avgImportDepth > 15 ? '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 * (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,
  };
}
 
/**
 * Maps a numerical score to a human-readable rating slug.
 *
 * @param score - The 0-100 readiness score.
 * @returns A formatted rating string (e.g., "excellent", "at risk").
 */
export function mapScoreToRating(score: number): string {
  // Use core implementation to resolve duplication
  return getRatingSlug(score).replace('-', ' ');
}