File size: 4,062 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { CodingDifficulty, RubricPoint } from "./types";

// ---------------------------------------------------------------------------
// Pure grading — maps raw correctness signals to a quality score (0–1) and a
// boolean "correct" verdict. No DB, no LLM, no I/O. The impure parts (running
// code, calling the judge) live elsewhere and feed their results in here so
// the scoring math stays deterministic and unit-tested.
// ---------------------------------------------------------------------------

export interface Grade {
  /** 0–1, feeds the XP quality bonus. */
  qualityScore: number;
  /** Whether the submission counts as a correct solve (gates the "solved" UX). */
  isCorrect: boolean;
}

/** A submission counts as correct once it covers at least this fraction. */
const CORRECT_THRESHOLD = 0.5;

/** Each false positive in a review costs this much quality. */
const FALSE_POSITIVE_PENALTY = 0.2;

function clamp01(n: number): number {
  return Math.max(0, Math.min(1, n));
}

// ---------------------------------------------------------------------------
// Executable problems (code-creation, debugging)
// ---------------------------------------------------------------------------

export function gradeCodeRun(run: {
  passed: number;
  total: number;
  allPassed: boolean;
}): Grade {
  if (run.total <= 0) {
    return { qualityScore: 0, isCorrect: false };
  }
  const qualityScore = clamp01(run.passed / run.total);
  return { qualityScore, isCorrect: run.allPassed && run.passed === run.total };
}

// ---------------------------------------------------------------------------
// Code-review problems
// ---------------------------------------------------------------------------

export interface CodeReviewVerdict {
  /** Known-issue ids the user correctly identified (judge-resolved). */
  identifiedIssueIds: string[];
  /** Count of plausible-but-wrong issues the user claimed. */
  falsePositives: number;
}

export function gradeCodeReview(
  totalKnownIssues: number,
  verdict: CodeReviewVerdict,
): Grade {
  const falsePositives = Math.max(0, verdict.falsePositives);

  // Clean code: the correct answer is "no issues". Inventing issues is wrong.
  if (totalKnownIssues <= 0) {
    const qualityScore = clamp01(1 - FALSE_POSITIVE_PENALTY * falsePositives);
    return { qualityScore, isCorrect: falsePositives === 0 };
  }

  const uniqueFound = new Set(verdict.identifiedIssueIds).size;
  const recall = clamp01(uniqueFound / totalKnownIssues);
  const qualityScore = clamp01(recall - FALSE_POSITIVE_PENALTY * falsePositives);

  return {
    qualityScore,
    isCorrect: recall >= CORRECT_THRESHOLD && falsePositives <= 1,
  };
}

// ---------------------------------------------------------------------------
// Architecture problems
// ---------------------------------------------------------------------------

export interface ArchitectureVerdict {
  /** Rubric point ids the answer adequately addressed (judge-resolved). */
  coveredPointIds: string[];
}

export function gradeArchitecture(
  rubric: RubricPoint[],
  verdict: ArchitectureVerdict,
): Grade {
  const totalWeight = rubric.reduce((sum, p) => sum + Math.max(0, p.weight), 0);
  if (totalWeight <= 0) {
    return { qualityScore: 0, isCorrect: false };
  }

  const covered = new Set(verdict.coveredPointIds);
  const coveredWeight = rubric
    .filter((p) => covered.has(p.id))
    .reduce((sum, p) => sum + Math.max(0, p.weight), 0);

  const qualityScore = clamp01(coveredWeight / totalWeight);
  return { qualityScore, isCorrect: qualityScore >= CORRECT_THRESHOLD };
}

// ---------------------------------------------------------------------------
// Difficulty weighting (used by routes to scale XP toward harder problems)
// ---------------------------------------------------------------------------

const DIFFICULTY_WEIGHT: Record<CodingDifficulty, number> = {
  easy: 0.4,
  medium: 0.7,
  hard: 1.0,
};

export function difficultyWeight(difficulty: CodingDifficulty): number {
  return DIFFICULTY_WEIGHT[difficulty] ?? 0.7;
}