File size: 12,421 Bytes
c4ae742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import type { Config } from "../config.js";
import { AiClient, type AiClientConfig } from "../ai/client.js";
import type { OptionsSnapshot } from "../queue/runner.js";
import { DEFAULT_JUDGE_PROMPT_TEMPLATE, JSON_ONLY_SYSTEM } from "./prompt.js";
import { buildGoldenTrajectory } from "./trajectory.js";
import { evaluateJudgePolicy } from "./policy.js";

type AnyObj = Record<string, any>;

const DIMENSION_LABELS: Record<string, string> = {
  task_realism: "Task Realism",
  no_information_leakage: "No Information Leakage",
  task_complexity: "Task Complexity",
  rubric_concreteness: "Rubric Concreteness",
  rubric_completeness: "Rubric Completeness",
  trajectory_task_alignment: "Trajectory-Task Alignment",
};

const DIMENSION_ORDER = [
  "task_realism",
  "no_information_leakage",
  "task_complexity",
  "rubric_concreteness",
  "rubric_completeness",
  "trajectory_task_alignment",
] as const;

const SUPPORTED_VARIABLES = ["taskStatement", "rubricJsonStr", "goldenTrajectory", "passPolicy"];

export interface RunJudgeInput {
  aiClient: AiClient;
  config: Config;
  snapshot: OptionsSnapshot;
  taskPackage: AnyObj;
  evidence: AnyObj;
}

export interface JudgeResult {
  dimensions: Record<string, { score: number; explanation: string }>;
  total_score: number;
  max_score: number;
  has_zeros: boolean;
  trajectory_pass: boolean;
  dbdiff_pass: boolean;
  package_pass: boolean;
  verdict: "PASS" | "NEEDS_REVISION" | "REJECT";
  policy: {
    strictFullMarks: boolean;
    ignoreTaskComplexityForFullMarks: boolean;
    threshold: number;
    maxScore: number;
    rule: string;
  };
  summary: string;
  revision_notes: string[];
  dimensionLabels: Record<string, string>;
  raw?: unknown;
  evaluated?: boolean;
  evaluatedAt?: string;
  overallScore?: number;
  maxScore?: number;
  passed?: boolean;
  feedback?: string;
  rubricScores?: Array<{
    rubricId: string;
    name: string;
    score: number;
    maxScore: number;
    feedback: string;
  }>;
  revisionNotes?: string[];
  promptUsed?: string;
  rawResponse?: unknown;
}

type ParsedJudgeResponse = {
  dimensions: Record<(typeof DIMENSION_ORDER)[number], { score: 0 | 1 | 2; explanation: string }>;
  total_score: number;
  has_zeros: boolean;
  verdict: "PASS" | "NEEDS_REVISION" | "REJECT";
  summary: string;
  revision_notes: string[];
};

function getUnknownTemplateVariables(template: string): string[] {
  const matches = String(template || "").match(/\$\{([^}]+)\}/g) ?? [];
  return matches
    .map((match) => match.slice(2, -1).trim())
    .filter((name) => !SUPPORTED_VARIABLES.includes(name));
}

function buildJudgePrompt(template: string, variables: Record<string, string>): string {
  let prompt = String(template || "");
  for (const [key, value] of Object.entries(variables)) {
    const token = "${" + key + "}";
    const escapedToken = "\\" + token;
    prompt = prompt.split(escapedToken).join(value);
    prompt = prompt.split(token).join(value);
  }
  return prompt;
}

function buildPassPolicyText(snapshot: OptionsSnapshot): string {
  if (snapshot.strictFullMarks) {
    if (snapshot.ignoreTaskComplexityForFullMarks) {
      return [
        "Strict full-marks mode is enabled with Task Complexity ignored for full-marks evaluation.",
        "PASS if all dimensions except task_complexity score 2/2 and task_complexity scores at least 1/2.",
        "A task_complexity score of 1/2 is acceptable; a task_complexity score of 0/2 is not acceptable.",
        "Any non-task_complexity score below 2 must be NEEDS_REVISION or REJECT according to severity.",
      ].join("\n");
    }
    return [
      "Strict full-marks mode is enabled.",
      "PASS only if total_score equals 12 and has_zeros is false.",
      "Any score below 12 must be NEEDS_REVISION or REJECT according to severity.",
    ].join("\n");
  }
  return [
    "Normal mode is enabled.",
    "PASS if total_score >= 10 and has_zeros is false.",
    "NEEDS_REVISION if total_score is 7-9 or total_score >= 10 with has_zeros true.",
    "REJECT if total_score < 7.",
  ].join("\n");
}

function buildRubricJson(rubrics: AnyObj[]): string {
  return JSON.stringify(
    (rubrics || []).map((rubric) => ({
      id: rubric.id,
      name: rubric.name,
      description: rubric.description,
      category: rubric.category ?? "",
      checker_key: rubric.checker_key ?? "",
      maxScore: rubric.maxScore,
      scorePoints: rubric.scorePoints,
    })),
    null,
    2,
  );
}

function normalizeExistingGoldenTrajectory(value: unknown): unknown[] | string | null {
  if (Array.isArray(value)) return value.length ? value : null;
  if (typeof value !== "string") return null;
  const trimmed = value.trim();
  if (!trimmed || trimmed === "[]") return null;
  try {
    const parsed = JSON.parse(trimmed) as unknown;
    return Array.isArray(parsed) && parsed.length ? parsed : trimmed;
  } catch (_) {
    return trimmed;
  }
}

function resolveGoldenTrajectory(taskPackage: AnyObj, evidence: AnyObj, groups: AnyObj[]): string {
  const built = buildGoldenTrajectory(evidence, groups);
  if (built && built.trim() && built.trim() !== "[]") return built;
  if (evidence?.type === "mcp") return "[]";
  const existing = normalizeExistingGoldenTrajectory(taskPackage?.golden_trajectory);
  if (existing) {
    return typeof existing === "string" ? existing : JSON.stringify(existing, null, 2);
  }
  return "[]";
}

function stripCodeFence(content: string): string {
  return String(content || "")
    .replace(/^```json\s*/i, "")
    .replace(/^```\s*/i, "")
    .replace(/```\s*$/i, "");
}

function extractJsonObject(content: string): string {
  const start = content.indexOf("{");
  const end = content.lastIndexOf("}");
  if (start === -1 || end === -1 || end <= start) return "";
  return content.slice(start, end + 1);
}

function invalid(): never {
  throw new Error("Judge response JSON schema invalid");
}

function validateDimension(value: unknown): { score: 0 | 1 | 2; explanation: string } {
  if (!value || typeof value !== "object") invalid();
  const dimension = value as AnyObj;
  if (
    (dimension.score !== 0 && dimension.score !== 1 && dimension.score !== 2) ||
    typeof dimension.explanation !== "string"
  ) {
    invalid();
  }
  return {
    score: dimension.score,
    explanation: dimension.explanation,
  };
}

function validateTotalScore(value: unknown): number {
  if (typeof value !== "number" || value < 0 || value > 12) invalid();
  return value;
}

function validateVerdict(value: unknown): ParsedJudgeResponse["verdict"] {
  if (value === "PASS" || value === "NEEDS_REVISION" || value === "REJECT") return value;
  invalid();
}

function validateJudgeResponse(value: unknown): ParsedJudgeResponse {
  if (!value || typeof value !== "object") invalid();
  const response = value as AnyObj;
  const dimensions = response.dimensions;
  if (!dimensions || typeof dimensions !== "object") invalid();
  return {
    dimensions: {
      task_realism: validateDimension((dimensions as AnyObj).task_realism),
      no_information_leakage: validateDimension((dimensions as AnyObj).no_information_leakage),
      task_complexity: validateDimension((dimensions as AnyObj).task_complexity),
      rubric_concreteness: validateDimension((dimensions as AnyObj).rubric_concreteness),
      rubric_completeness: validateDimension((dimensions as AnyObj).rubric_completeness),
      trajectory_task_alignment: validateDimension((dimensions as AnyObj).trajectory_task_alignment),
    },
    total_score: validateTotalScore(response.total_score),
    has_zeros: typeof response.has_zeros === "boolean" ? response.has_zeros : invalid(),
    verdict: validateVerdict(response.verdict),
    summary: typeof response.summary === "string" ? response.summary : invalid(),
    revision_notes:
      Array.isArray(response.revision_notes) &&
      response.revision_notes.every((item) => typeof item === "string")
        ? response.revision_notes
        : invalid(),
  };
}

function parseJudgeResponse(content: string): ParsedJudgeResponse {
  const candidates = [
    content.trim(),
    stripCodeFence(content).trim(),
    extractJsonObject(content).trim(),
  ].filter(Boolean);
  for (const candidate of candidates) {
    try {
      return validateJudgeResponse(JSON.parse(candidate));
    } catch (_) {
      // try next candidate
    }
  }
  throw new Error("Judge response is not valid JSON");
}

function mapJudgeResponseToResult(
  response: ParsedJudgeResponse,
  promptUsed: string,
  rawResponse: unknown,
  snapshot: OptionsSnapshot,
): JudgeResult {
  const dimensions = response.dimensions;
  const rubricScores = DIMENSION_ORDER.map((key) => ({
    rubricId: "judge:" + key,
    name: DIMENSION_LABELS[key],
    score: dimensions[key].score,
    maxScore: 2,
    feedback: dimensions[key].explanation,
  }));
  const feedbackSections = [response.summary.trim()];
  if (response.revision_notes.length > 0) {
    feedbackSections.push(
      "Revision Notes:\n" + response.revision_notes.map((note) => "- " + note).join("\n"),
    );
  }
  const strictFullMarks = Boolean(snapshot.strictFullMarks);
  const policyDecision = evaluateJudgePolicy(
    {
      total_score: response.total_score,
      max_score: 12,
      has_zeros: response.has_zeros,
      verdict: response.verdict,
      dimensions,
    },
    snapshot,
  );
  const passed = policyDecision.passed;
  const verdict: JudgeResult["verdict"] = passed
    ? "PASS"
    : response.total_score >= 7
      ? "NEEDS_REVISION"
      : "REJECT";
  return {
    dimensions,
    total_score: response.total_score,
    max_score: 12,
    has_zeros: response.has_zeros,
    trajectory_pass: dimensions.trajectory_task_alignment.score > 0,
    dbdiff_pass: true,
    package_pass: passed,
    verdict,
    policy: {
      strictFullMarks,
      ignoreTaskComplexityForFullMarks: policyDecision.ignoreTaskComplexityForFullMarks,
      threshold: policyDecision.threshold,
      maxScore: policyDecision.maxScore,
      rule: policyDecision.rule,
    },
    summary: response.summary,
    revision_notes: response.revision_notes,
    dimensionLabels: { ...DIMENSION_LABELS },
    raw: response,
    evaluated: true,
    evaluatedAt: new Date().toISOString(),
    overallScore: response.total_score,
    maxScore: 12,
    passed,
    feedback: feedbackSections.filter(Boolean).join("\n\n"),
    rubricScores,
    revisionNotes: response.revision_notes,
    promptUsed,
    rawResponse,
  };
}

function resolveJudgeConfig(config: Config, snapshot: OptionsSnapshot): AiClientConfig {
  return {
    apiKey: config.judgeApiKey || config.defaultApiKey,
    baseURL: snapshot.judge?.baseURL || config.judgeBaseURL || config.defaultBaseURL,
    apiMode: "chat",
    model: snapshot.judge?.model || config.judgeModel || config.defaultModel,
    temperature: 0,
    enableThinking: false,
    clearThinking: false,
  };
}

export async function runJudge(input: RunJudgeInput): Promise<JudgeResult> {
  const { aiClient, config, snapshot, taskPackage, evidence } = input;
  const rubrics = Array.isArray(taskPackage.rubrics) ? taskPackage.rubrics : [];
  const groups = Array.isArray(taskPackage.subtasks) ? taskPackage.subtasks : [];
  const goldenTrajectory = resolveGoldenTrajectory(taskPackage, evidence, groups);
  let template = snapshot.judge?.promptTemplate || DEFAULT_JUDGE_PROMPT_TEMPLATE;
  if (!template.includes("${passPolicy}")) {
    template += "\n\n## Active Pass Policy\n${passPolicy}";
  }
  const unknownVariables = getUnknownTemplateVariables(template);
  if (unknownVariables.length > 0) {
    throw new Error("Prompt contains unsupported variables: " + unknownVariables.join(", "));
  }
  const prompt = buildJudgePrompt(template, {
    taskStatement: String(taskPackage.instruction || "").trim(),
    rubricJsonStr: buildRubricJson(rubrics),
    goldenTrajectory,
    passPolicy: buildPassPolicyText(snapshot),
  });

  const response = await aiClient.requestText(
    resolveJudgeConfig(config, snapshot),
    [
      { role: "system", content: JSON_ONLY_SYSTEM },
      { role: "user", content: prompt },
    ],
    {
      stream: false,
      jsonMode: false,
      jsonModeFallback: false,
      retries: 0,
      pluginCompat: true,
      disableBetaParameterFallback: true,
    },
  );
  const parsed = parseJudgeResponse(response.text);
  return mapJudgeResponseToResult(parsed, prompt, response, snapshot);
}

export { buildGoldenTrajectory };