Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 3x 3x 2x 2x 3x 3x 1x 2x 2x 2x 3x 2x 1x 1x 2x 2x 3x 3x 3x 3x 3x 3x 3x 1x 2x 2x 2x | import { z } from 'zod';
import { namedJudge } from 'vitest-evals';
import type { JudgeContext } from 'vitest-evals';
import {
normalizeContent,
normalizeMetadata,
toJsonValue,
type Harness,
} from 'vitest-evals/harness';
import { runJudge } from './judge.js';
import { runEvalSkill, type RunEvalOptions } from './runner.js';
import { evalPassed, type EvalMeta, type JudgeResponse } from './types.js';
import { usageToSummary } from './usage.js';
import { FindingSchema } from '../types/index.js';
import type { Finding, SkillReport, UsageStats } from '../types/index.js';
export const WardenEvalOutputSchema = z.object({
name: z.string(),
summary: z.string(),
skill: z.string(),
runtime: z.string().optional(),
model: z.string().optional(),
findings: z.array(FindingSchema),
failedHunks: z.number().int().nonnegative().optional(),
failedExtractions: z.number().int().nonnegative().optional(),
});
export type WardenEvalOutput = z.infer<typeof WardenEvalOutputSchema>;
function usageMetadata(usage: UsageStats | undefined): Record<string, unknown> | undefined {
Iif (!usage) {
return undefined;
}
return {
inputTokens: usage.inputTokens,
outputTokens: usage.outputTokens,
costUSD: usage.costUSD,
};
}
function reportToOutput(name: string, report: SkillReport): WardenEvalOutput {
return {
name,
summary: report.summary,
skill: report.skill,
runtime: report.runtime,
model: report.model,
findings: report.findings,
failedHunks: report.failedHunks,
failedExtractions: report.failedExtractions,
};
}
function failedJudgeReasons(
meta: EvalMeta,
response: JudgeResponse,
findings: Finding[]
): string[] {
const reasons: string[] = [];
for (let i = 0; i < meta.should_find.length; i++) {
const assertion = meta.should_find[i];
if (!assertion?.required) {
continue;
}
const verdict = response.expectations[i];
Iif (!verdict) {
reasons.push(`missing verdict for should_find[${i}]`);
continue;
}
const matchedFinding = verdict.matchedFindingIndex === null
? undefined
: findings[verdict.matchedFindingIndex];
Iif (!verdict.met) {
reasons.push(`should_find[${i}] not met: ${verdict.reasoning}`);
} else if (assertion.severity) {
if (!matchedFinding) {
reasons.push(`should_find[${i}] severity could not be checked: no matched finding`);
E} else if (matchedFinding.severity !== assertion.severity) {
reasons.push(
`should_find[${i}] severity mismatch: expected ${assertion.severity}, got ${matchedFinding.severity}`
);
}
}
}
for (let i = 0; i < meta.should_not_find.length; i++) {
const verdict = response.antiExpectations[i];
if (verdict?.violated) {
reasons.push(`should_not_find[${i}] violated: ${verdict.reasoning}`);
}
}
return reasons;
}
export function createWardenEvalHarness(options: RunEvalOptions): Harness<EvalMeta> {
return {
name: 'warden',
prompt: async () => {
throw new Error('Warden eval judges use runJudge directly.');
},
run: async (meta, context) => {
const modelOverride = typeof context.metadata['model'] === 'string'
? context.metadata['model']
: undefined;
const runtimeOverride = context.metadata['runtime'] === 'claude'
|| context.metadata['runtime'] === 'pi'
? context.metadata['runtime']
: undefined;
const result = await runEvalSkill(meta, {
...options,
model: modelOverride ?? options.model,
runtime: runtimeOverride ?? options.runtime,
});
const output = reportToOutput(result.name, result.report);
return {
output: toJsonValue(output),
session: {
messages: [
{
role: 'user',
content: normalizeContent({
name: result.name,
given: meta.given,
shouldFind: meta.should_find,
shouldNotFind: meta.should_not_find,
}),
},
{
role: 'assistant',
content: normalizeContent(output),
},
],
outputText: result.report.summary,
provider: result.report.runtime,
model: result.report.model,
metadata: normalizeMetadata({
category: meta.category,
scenario: meta.name,
skill: result.report.skill,
}),
},
usage: usageToSummary({
provider: result.report.runtime ?? 'unknown',
model: result.report.model ?? 'unknown',
usage: result.report.usage,
}),
timings: { totalMs: result.durationMs },
artifacts: {
logs: toJsonValue(result.logs) ?? [],
},
errors: result.report.error
? [{
type: result.report.error.code,
message: result.report.error.message,
}]
: [],
};
},
};
}
export function createWardenEvalJudge(apiKey: string) {
return namedJudge<JudgeContext<EvalMeta>>('WardenEvalJudge', async ({ inputValue, run }) => {
const output = WardenEvalOutputSchema.safeParse(run.output);
Iif (!output.success) {
return {
score: 0,
metadata: {
rationale: `Invalid Warden harness output: ${output.error.message}`,
},
};
}
const meta = inputValue;
const findings = output.data.findings;
const judgeResult = await runJudge(meta, findings, apiKey);
if (judgeResult.error) {
return {
score: 0,
metadata: {
rationale: `Judge failed: ${judgeResult.error}`,
output: judgeResult.response,
usage: usageMetadata(judgeResult.usage),
},
};
}
const passed = evalPassed(meta, judgeResult.response, findings);
const reasons = failedJudgeReasons(meta, judgeResult.response, findings);
return {
score: passed ? 1 : 0,
metadata: {
rationale: reasons.length > 0 ? reasons.join('; ') : 'All eval assertions passed.',
output: judgeResult.response,
usage: usageMetadata(judgeResult.usage),
},
};
});
}
|