File size: 3,476 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
import { runJavaScriptTests } from "./harness";
import { isExecutableLanguage } from "./types";
import type { CodingProblem } from "./types";

// ---------------------------------------------------------------------------
// Problem validation — guarantees every generated problem has a known,
// verifiable answer before it reaches the user.
//
//   - executable (code-creation / debugging): the reference solution must pass
//     all tests; debugging additionally requires the buggy code to fail at
//     least one test (otherwise there is nothing to fix).
//   - code-review: issue ids must be unique and the code non-empty.
//   - architecture: the rubric must be non-empty with unique ids and positive
//     weights.
//
// For non-executable languages (python, go) we can't run the reference, so we
// fall back to structural checks and trust the generator's self-consistency.
// ---------------------------------------------------------------------------

export interface ValidationResult {
  valid: boolean;
  reason?: string;
}

const ok: ValidationResult = { valid: true };
function fail(reason: string): ValidationResult {
  return { valid: false, reason };
}

export function validateProblem(problem: CodingProblem): ValidationResult {
  switch (problem.type) {
    case "code-creation":
      return validateExecutable(
        problem.language,
        problem.entryFunction,
        problem.referenceSolution,
        problem.tests,
      );

    case "debugging": {
      const refResult = validateExecutable(
        problem.language,
        problem.entryFunction,
        problem.referenceSolution,
        problem.tests,
      );
      if (!refResult.valid) return refResult;

      // The buggy code must actually be broken against the tests.
      if (isExecutableLanguage(problem.language)) {
        const buggyRun = runJavaScriptTests(
          problem.buggyCode,
          problem.entryFunction,
          problem.tests,
        );
        if (buggyRun.allPassed) {
          return fail("buggy code passes every test — nothing to fix");
        }
      }
      return ok;
    }

    case "code-review": {
      if (problem.code.trim().length === 0) return fail("empty code under review");
      const ids = problem.issues.map((i) => i.id);
      if (new Set(ids).size !== ids.length) return fail("duplicate issue ids");
      return ok;
    }

    case "architecture": {
      if (problem.rubric.length === 0) return fail("empty rubric");
      const ids = problem.rubric.map((r) => r.id);
      if (new Set(ids).size !== ids.length) return fail("duplicate rubric ids");
      if (problem.rubric.some((r) => r.weight <= 0)) {
        return fail("rubric weights must be positive");
      }
      return ok;
    }
  }
}

function validateExecutable(
  language: string,
  entryFunction: string,
  referenceSolution: string,
  tests: { name: string; input: unknown[]; expected: unknown }[],
): ValidationResult {
  if (!tests || tests.length === 0) return fail("no tests provided");

  // We can only execute JS-family languages; trust structural shape otherwise.
  if (!isExecutableLanguage(language)) return ok;

  const run = runJavaScriptTests(referenceSolution, entryFunction, tests);
  if (run.compileError) {
    return fail(`reference solution did not compile: ${run.compileError}`);
  }
  if (!run.allPassed) {
    return fail(
      `reference solution failed ${run.total - run.passed}/${run.total} tests`,
    );
  }
  return ok;
}