Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/scorers/rubric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ export interface RubricCriterion {
pattern?: string;
}

function criterionSatisfied(output: string, c: RubricCriterion): boolean {
function criterionSatisfied(
output: string,
c: RubricCriterion,
): boolean | { invalidPattern: string } {
const hay = normalize(output);
if (c.allOf && !c.allOf.every((s) => hay.includes(normalize(s)))) return false;
if (c.anyOf && !c.anyOf.some((s) => hay.includes(normalize(s)))) return false;
if (c.noneOf && c.noneOf.some((s) => hay.includes(normalize(s)))) return false;
if (c.pattern && !new RegExp(c.pattern, "i").test(output)) return false;
if (c.pattern) {
try {
if (!new RegExp(c.pattern, "i").test(output)) return false;
} catch (err) {
return { invalidPattern: (err as Error).message };
}
}
// A criterion with no checks is treated as satisfied (documentation only).
return true;
}
Expand Down Expand Up @@ -50,7 +59,15 @@ export const rubricScorer: Scorer = {
for (const c of criteria) {
const points = c.points ?? 1;
total += points;
if (criterionSatisfied(ctx.output, c)) {
const satisfied = criterionSatisfied(ctx.output, c);
if (typeof satisfied === "object") {
return result(spec, {
score: 0,
passed: false,
reason: `invalid regex in criterion "${c.description}": ${satisfied.invalidPattern}`,
});
}
if (satisfied) {
earned += points;
} else {
failed.push(c.description);
Expand Down
9 changes: 9 additions & 0 deletions tests/scorers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ describe("rubric", () => {
expect(r.score).toBeCloseTo(0.5);
expect(r.passed).toBe(true);
});
it("returns a fail result for an invalid criterion pattern regex", async () => {
const r = await run(
rubricScorer,
{ type: "rubric", criteria: [{ description: "x", pattern: "([" }] },
ctx("anything"),
);
expect(r.passed).toBe(false);
expect(r.reason).toMatch(/invalid regex/i);
});
});

describe("exact-match trim:false preserves internal whitespace", () => {
Expand Down
Loading