fix(evals): reject null grader expectations without crashing

This commit is contained in:
Raghu :)
2026-09-06 23:20:26 -07:00
parent 48cb1168ae
commit e6a58d5fc3
2 changed files with 21 additions and 1 deletions
+19
View File
@@ -90,6 +90,25 @@ test('rejects grader results that omit expectations', () => {
assert.equal(parseGrading(raw, 2), null);
});
test('rejects null expectation entries without throwing', () => {
for (const expectations of [
[null],
[{ text: 'first expectation', passed: true, evidence: 'observed' }, null],
]) {
const raw = JSON.stringify({
expectations,
summary: {
passed: expectations.length - 1,
failed: 1,
total: expectations.length,
pass_rate: (expectations.length - 1) / expectations.length,
},
});
assert.equal(parseGrading(raw, expectations.length), null);
}
});
test('rejects incomplete or inconsistent grader summaries', () => {
const expectation = { text: 'expected behavior', passed: false, evidence: 'not observed' };
const cases = [
+2 -1
View File
@@ -439,12 +439,13 @@ function parseGrading(raw, expectedCount) {
const expectations = g.expectations;
const summary = g.summary;
const passed = Array.isArray(expectations)
? expectations.filter((expectation) => expectation.passed === true).length
? expectations.filter((expectation) => expectation?.passed === true).length
: 0;
const ok =
Number.isInteger(expectedCount) && expectedCount > 0 &&
Array.isArray(expectations) && expectations.length === expectedCount &&
expectations.every((expectation) =>
expectation !== null &&
typeof expectation.text === 'string' &&
typeof expectation.passed === 'boolean' &&
typeof expectation.evidence === 'string') &&