test(eval): add semantic rubric gate

This commit is contained in:
ZacharyZhang-NY committed 2026-04-23 19:31:31 -04:00
1 parent 84d80f02aa
commit 04c4059159
15 files changed
+348 -33

No files matched your search

+48
View File
@@ -22,6 +22,7 @@ import type {
FixtureFile,
FixtureLine,
FixtureMeta,
SemanticRubricDimension,
TraceTurn,
} from "./types.js";
@@ -48,6 +49,51 @@ function isTurn(value: FixtureLine): value is TraceTurn {
return value.type === "turn";
}
function requireNonEmptyString(value: unknown, field: string, source: string): void {
if (typeof value !== "string" || value.trim().length === 0) {
throw new Error(`fixture ${source}: semanticRubric field "${field}" must be a non-empty string`);
}
}
function requireStringArray(value: unknown, field: string, source: string): void {
if (!Array.isArray(value) || value.length === 0) {
throw new Error(`fixture ${source}: semanticRubric field "${field}" must be a non-empty string array`);
}
for (const item of value) {
if (typeof item !== "string" || item.trim().length === 0) {
throw new Error(`fixture ${source}: semanticRubric field "${field}" contains a non-string item`);
}
}
}
function validateSemanticRubric(
rubric: unknown,
source: string,
): asserts rubric is SemanticRubricDimension[] {
if (!Array.isArray(rubric) || rubric.length === 0) {
throw new Error(`fixture ${source}: assertions.semanticRubric must contain at least one dimension`);
}
const ids = new Set<string>();
for (const dimension of rubric) {
if (!dimension || typeof dimension !== "object") {
throw new Error(`fixture ${source}: semanticRubric dimension must be an object`);
}
const record = dimension as Record<string, unknown>;
requireNonEmptyString(record.id, "id", source);
requireNonEmptyString(record.question, "question", source);
requireNonEmptyString(record.passRubric, "passRubric", source);
requireNonEmptyString(record.failRubric, "failRubric", source);
requireStringArray(record.mustContain, "mustContain", source);
if (record.mustNotContain !== undefined) {
requireStringArray(record.mustNotContain, "mustNotContain", source);
}
if (ids.has(record.id as string)) {
throw new Error(`fixture ${source}: duplicate semanticRubric id "${record.id}"`);
}
ids.add(record.id as string);
}
}
export async function loadFixture(path: string): Promise<FixtureFile> {
const raw = await readFile(path, "utf8");
const lines = raw
@@ -80,6 +126,7 @@ export async function loadFixture(path: string): Promise<FixtureFile> {
`fixture ${path}: last line must be a {"type":"assertions"} record`,
);
}
validateSemanticRubric(last.record.semanticRubric, path);
const turns: TraceTurn[] = [];
for (let i = 1; i < parsed.length - 1; i += 1) {
@@ -105,5 +152,6 @@ export async function loadFixture(path: string): Promise<FixtureFile> {
meta: first.record,
turns,
assertions: last.record.assertions,
semanticRubric: last.record.semanticRubric,
};
}