Download src/lib/coding/grade.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 4.06 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/coding/grade.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/coding/grade.ts
-
curl -L -o grade.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/coding/grade.ts
4.06 kB
| 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; | |
| } | |