Files
Addy Osmani 99820a9055 Merge #769: isolate fixture repositories from inherited Git hooks
fix(evals): isolate fixture repositories from inherited Git hooks
2026-11-23 23:08:48 -08:00

1522 lines
68 KiB
JavaScript

#!/usr/bin/env node
'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { spawnSync } = require('node:child_process');
const test = require('node:test');
const { materializeWorkspace, parseGrading, clearGradingSlot, persistGradingOutcome, extractExecutorModel } = require('./run-evals');
const { stageSkillResources } = require('./lib/skill-resources');
const RUNNER = path.join(__dirname, 'run-evals.js');
function writeJson(file, value) {
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, `${JSON.stringify(value, null, 2)}\n`);
}
function writeSkill(root, name, description) {
const dir = path.join(root, 'skills', name);
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(
path.join(dir, 'SKILL.md'),
`---\nname: ${name}\ndescription: ${description}\n---\n\n# ${name}\n`,
);
}
function behavioralEval(files = ['project/context.txt']) {
return {
id: 1,
prompt: 'Inspect the attached project and complete the requested work.',
expected_output: 'A verified result grounded in the attached project',
files,
expectations: ['The attached project is inspected before reporting a result'],
};
}
function completeCase(skillName, positivePrompt, topK = 1, files) {
return {
skill_name: skillName,
trigger: {
positive: [1, 2, 3].map(() => ({ prompt: positivePrompt, top_k: topK })),
negative: [
{ prompt: 'unrelated banana request' },
{ prompt: 'unrelated orange request' },
],
},
evals: [behavioralEval(files)],
};
}
function makeSandbox() {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-skills-run-evals-test-'));
fs.mkdirSync(path.join(root, 'scripts'), { recursive: true });
fs.mkdirSync(path.join(root, 'evals', 'cases'), { recursive: true });
fs.mkdirSync(path.join(root, 'evals', 'fixtures', 'project'), { recursive: true });
fs.copyFileSync(RUNNER, path.join(root, 'scripts', 'run-evals.js'));
fs.cpSync(path.join(__dirname, 'lib'), path.join(root, 'scripts', 'lib'), { recursive: true });
fs.writeFileSync(path.join(root, 'evals', 'fixtures', 'project', 'context.txt'), 'fixture\n');
return root;
}
function run(root, args = []) {
return spawnSync(process.execPath, [path.join(root, 'scripts', 'run-evals.js'), ...args], {
cwd: root,
encoding: 'utf8',
});
}
function runBehavioralWithGrading(root, raw, options = {}) {
const { args = [] } = options;
writeSkill(root, 'alpha-skill', 'Handles alpha widgets.');
const evalCase = completeCase('alpha-skill', 'change alpha widget');
evalCase.evals = (options.ids || [1]).map((id) => (options.execution
? { ...behavioralEval(), id }
: { ...behavioralEval([]), id, kind: 'dialogue' }));
writeJson(path.join(root, 'evals', 'cases', 'alpha-skill.json'), evalCase);
for (const [rel, content] of Object.entries(options.resources || {})) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), content);
}
const stub = path.join(root, 'stub-claude.cjs');
fs.writeFileSync(stub, `
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const execute = require('child_process').execFileSync;
const temporary = fs.mkdtempSync;
fs.mkdtempSync = (prefix, ...args) => {
const dir = temporary(prefix, ...args);
if (prefix.includes('skill-resources-')) fs.writeFileSync(${JSON.stringify(path.join(root, 'staged-path.txt'))}, dir);
return dir;
};
require('child_process').execFileSync = (command, args, settings) => {
if (command !== 'claude') return execute(command, args, settings);
const executor = args.includes('--output-format');
fs.appendFileSync(${JSON.stringify(path.join(root, 'claude-calls.txt'))}, executor ? 'executor\\n' : 'grader\\n');
if (executor) { ${options.onExecute || ''} }
if (executor && ${JSON.stringify(Boolean(options.error))}) throw Object.assign(new Error('executor failed'), ${JSON.stringify(options.error || {})});
return executor ? ${JSON.stringify(options.trace ?? '{"type":"result","subtype":"success","is_error":false,"result":"done"}\n')} : ${JSON.stringify(raw)};
};
`);
return spawnSync(process.execPath, [
'--require', stub, path.join(root, 'scripts', 'run-evals.js'),
'--behavioral', 'alpha-skill', ...args,
], { cwd: root, encoding: 'utf8' });
}
const passingGrading = JSON.stringify({
expectations: [{ id: 1, text: 'check', passed: true, evidence: 'observed' }],
summary: { passed: 1, failed: 0, total: 1, pass_rate: 1 },
});
const terminal = { type: 'result', subtype: 'success', is_error: false };
const call = { type: 'assistant', message: { content: [{ type: 'tool_use', name: 'Bash', id: 'test', input: {} }] } };
const returned = (id, is_error = false) => ({ type: 'user', message: { content: [{ type: 'tool_result', tool_use_id: id, is_error }] } });
const traceOf = (...events) => events.map((event) => JSON.stringify(event)).join('\n') + '\n';
for (const [name, trace] of Object.entries({
'terminal error': traceOf({ type: 'result', subtype: 'error_max_turns', is_error: true }),
'missing terminal': traceOf(call),
'implicit success': traceOf({ type: 'result', subtype: 'success' }),
'stale terminal': traceOf(terminal, call),
'permission denial': traceOf({ ...terminal, permission_denials: [{ tool_name: 'Read' }] }),
'pending tool': traceOf(call, terminal),
'unmatched return': traceOf(returned('unknown'), terminal),
'malformed event': 'null\n',
'invalid JSON': 'not JSON\n',
'null assistant message': traceOf({ type: 'assistant', message: null }, terminal),
'array assistant message': traceOf({ type: 'assistant', message: [] }, terminal),
'null assistant content': traceOf({ type: 'assistant', message: { content: null } }, terminal),
'object assistant content': traceOf({ type: 'assistant', message: { content: { type: 'tool_use', id: 'hidden' } } }, terminal),
'null user message': traceOf({ type: 'user', message: null }, terminal),
'null content block': traceOf({ type: 'assistant', message: { content: [null] } }, terminal),
})) {
test(`does not grade an incomplete execution: ${name}`, () => {
const root = makeSandbox();
try {
const result = runBehavioralWithGrading(root, passingGrading, { trace });
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stdout, /execution incomplete/);
const base = path.join(root, 'evals/results/alpha-skill.eval-1');
assert.equal(fs.existsSync(`${base}.grading.json`), false);
assert.equal(fs.readFileSync(`${base}.execution.raw.txt`, 'utf8'), trace);
assert.equal(fs.readFileSync(path.join(root, 'claude-calls.txt'), 'utf8'), 'executor\n');
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
}
for (const id of [null, 0, 2]) {
test(id === null ? 'behavioral default grades every case' : `behavioral --eval ${id} grades only the selected case`, () => {
const root = makeSandbox();
try {
const raw = JSON.stringify({
expectations: [{ id: 1, text: 'check', passed: true, evidence: 'observed' }],
summary: { passed: 1, failed: 0, total: 1, pass_rate: 1 },
});
const args = id === null ? [] : ['--eval', String(id)];
const result = runBehavioralWithGrading(root, raw, { args, ids: [0, 2] });
assert.equal(result.status, 0, result.stdout + result.stderr);
for (const candidate of [0, 2]) {
assert.equal(fs.existsSync(path.join(root, 'evals/results', `alpha-skill.eval-${candidate}.grading.json`)), id === null || id === candidate);
}
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
}
for (const fail of [false, true]) {
test(`stages local and shared resources outside the fixture tree and cleans up on ${fail ? 'failure' : 'success'}`, () => {
const root = makeSandbox();
try {
const raw = JSON.stringify({
expectations: [{ id: 1, text: 'check', passed: true, evidence: 'observed' }],
summary: { passed: 1, failed: 0, total: 1, pass_rate: 1 },
});
const result = runBehavioralWithGrading(root, raw, {
execution: true,
resources: {
'skills/alpha-skill/SKILL.md': '---\nname: alpha-skill\ndescription: Handles alpha widgets.\n---\n\n# Alpha\nRead references/shared.md and the local references/local.md.',
'skills/alpha-skill/references/local.md': 'See ../../../references/shared.md',
'skills/alpha-skill/scripts/helper.js': 'local helper',
'references/shared.md': 'See nested/next.md',
'references/nested/next.md': 'transitive shared material',
},
onExecute: `
const index = args.indexOf('--add-dir');
assert.notEqual(index, -1, 'resource access must be granted');
const staged = args[index + 1];
const skill = path.join(staged, 'skills', 'alpha-skill');
const prompt = args[args.indexOf('--append-system-prompt') + 1];
assert.ok(prompt.includes('Base directory for this skill: ' + skill));
assert.ok(prompt.includes('Shared references directory: ' + path.join(staged, 'references')));
assert.ok(prompt.includes('Read references/shared.md'));
assert.equal(fs.readFileSync(path.join(staged, 'references/shared.md'), 'utf8'), 'See nested/next.md');
assert.equal(fs.readFileSync(path.join(skill, 'references/local.md'), 'utf8'), 'See ../../../references/shared.md');
assert.equal(fs.readFileSync(path.resolve(skill, '../../references/nested/next.md'), 'utf8'), 'transitive shared material');
assert.equal(fs.readFileSync(path.join(skill, 'scripts/helper.js'), 'utf8'), 'local helper');
assert.ok(path.relative(settings.cwd, staged).startsWith('..'), 'resources must stay outside the fixture tree');
assert.equal(execute('git', ['status', '--porcelain'], {cwd: settings.cwd, encoding: 'utf8'}), '');
assert.equal(execute('git', ['ls-files'], {cwd: settings.cwd, encoding: 'utf8'}).trim(), 'project/context.txt');
fs.writeFileSync(${JSON.stringify(path.join(root, 'workspace-path.txt'))}, settings.cwd);
if (${fail}) throw new Error('synthetic executor failure');
`,
});
assert.equal(result.status, fail ? 1 : 0, result.stdout + result.stderr);
const staged = fs.readFileSync(path.join(root, 'staged-path.txt'), 'utf8');
const workspace = fs.readFileSync(path.join(root, 'workspace-path.txt'), 'utf8');
assert.equal(fs.existsSync(staged), false, 'resource directory must be removed');
assert.equal(fs.existsSync(workspace), false, 'fixture workspace must be removed');
assert.equal(fs.readFileSync(path.join(root, 'references/nested/next.md'), 'utf8'), 'transitive shared material');
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
}
test('an incomplete execution saves no record and is not graded', () => {
const root = makeSandbox();
try {
const result = runBehavioralWithGrading(root, passingGrading, { trace: traceOf(call), args: ['--record'] });
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stdout, /execution incomplete/);
const results = path.join(root, 'evals/results');
assert.deepEqual(fs.readdirSync(results).filter((f) => f.endsWith('.record.json')), []);
assert.equal(fs.existsSync(path.join(results, 'alpha-skill.eval-1.grading.json')), false);
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
test('a returned failed probe can recover before successful completion', () => {
const root = makeSandbox();
try {
const next = { ...call, message: { content: [{ ...call.message.content[0], id: 'green' }] } };
const trace = traceOf(call, returned('test', true),
{ type: 'user', message: { content: 'Continue the repair.' } }, next, returned('green'), terminal,
{ type: 'system', subtype: 'status', status: 'idle' });
const result = runBehavioralWithGrading(root, passingGrading, { trace });
assert.equal(result.status, 0, result.stdout + result.stderr);
assert.equal(fs.existsSync(path.join(root, 'evals/results/alpha-skill.eval-1.grading.json')), true);
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
for (const code of ['ETIMEDOUT', 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER']) {
test(`retains partial execution evidence and continues after ${code}`, () => {
const root = makeSandbox();
try {
const trace = traceOf(call);
const result = runBehavioralWithGrading(root, passingGrading,
{ error: { code, stdout: trace, stderr: 'captured diagnostic' }, ids: [1, 2] });
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stdout, /eval 2: executing/);
for (const id of [1, 2]) {
const base = path.join(root, `evals/results/alpha-skill.eval-${id}`);
assert.equal(fs.existsSync(`${base}.grading.json`), false);
assert.equal(fs.readFileSync(`${base}.execution.raw.txt`, 'utf8'), trace);
assert.match(fs.readFileSync(`${base}.execution.error.txt`, 'utf8'), new RegExp(code));
assert.match(fs.readFileSync(`${base}.execution.error.txt`, 'utf8'), /captured diagnostic/);
}
assert.equal(fs.readFileSync(path.join(root, 'claude-calls.txt'), 'utf8'), 'executor\nexecutor\n');
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
}
for (const args of [['--eval'], ['--eval', 'not-a-number'], ['--eval', '1.5'], ['--eval', '9007199254740993']]) {
test(`rejects an invalid behavioral selector ${JSON.stringify(args)}`, () => {
const root = makeSandbox();
try {
const result = run(root, ['--behavioral', 'alpha-skill', ...args]);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stderr, /--eval must be an integer case id/);
assert.equal(fs.existsSync(path.join(root, 'evals/results')), false);
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
}
test('rejects an unknown behavioral case before creating results', () => {
const root = makeSandbox();
try {
const result = runBehavioralWithGrading(root, '{}', { args: ['--eval', '99'] });
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stderr, /has no behavioral eval id=99/);
assert.equal(fs.existsSync(path.join(root, 'evals/results')), false);
assert.equal(fs.existsSync(path.join(root, 'claude-calls.txt')), false);
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
test('a selector does not hide duplicate IDs among unselected cases', () => {
const root = makeSandbox();
try {
const result = runBehavioralWithGrading(root, '{}', { args: ['--eval', '0'], ids: [0, 2, 2] });
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stderr, /duplicate behavioral eval id=2/);
assert.equal(fs.existsSync(path.join(root, 'evals/results')), false);
assert.equal(fs.existsSync(path.join(root, 'claude-calls.txt')), false);
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
test('--eval is available only for behavioral runs', () => {
const root = makeSandbox();
try {
const result = run(root, ['--eval', '1']);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stderr, /--eval applies only to behavioral evals/);
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
test('rejects symlinked resources before execution and removes partial staging', () => {
const root = makeSandbox();
try {
fs.mkdirSync(path.join(root, 'skills/alpha-skill'), { recursive: true });
fs.mkdirSync(path.join(root, 'private'));
fs.symlinkSync(path.join(root, 'private'), path.join(root, 'skills/alpha-skill/escape'), 'junction');
const result = runBehavioralWithGrading(root, '{}');
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stderr, /symbolic link in skill resources/);
assert.equal(fs.existsSync(path.join(root, 'claude-calls.txt')), false);
assert.equal(fs.existsSync(fs.readFileSync(path.join(root, 'staged-path.txt'), 'utf8')), false);
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
for (const entry of ['skills', 'references']) {
test(`rejects a ${entry === 'skills' ? 'linked skill ancestor' : 'dangling shared-reference link'}`, () => {
const root = makeSandbox();
const outside = fs.mkdtempSync(path.join(os.tmpdir(), 'outside-skill-resources-'));
let staged;
try {
writeSkill(entry === 'skills' ? outside : root, 'alpha-skill', 'Handles alpha.');
const target = entry === 'skills' ? path.join(outside, 'skills') : path.join(outside, 'missing');
fs.symlinkSync(target, path.join(root, entry), 'junction');
assert.throws(() => { staged = stageSkillResources(root, 'alpha-skill'); }, /symbolic link in skill resources/);
} finally {
if (staged) fs.rmSync(staged.root, { recursive: true, force: true });
fs.rmSync(root, { recursive: true, force: true });
fs.rmSync(outside, { recursive: true, force: true });
}
});
}
test('a whole-checkout alias can stage ordinary skill resources', () => {
const root = makeSandbox();
let staged;
try {
writeSkill(root, 'alpha-skill', 'Handles alpha.');
const alias = path.join(root, 'checkout-alias');
fs.symlinkSync(root, alias, 'junction');
staged = stageSkillResources(alias, 'alpha-skill');
assert.equal(fs.readFileSync(path.join(staged.skillDir, 'SKILL.md'), 'utf8'),
fs.readFileSync(path.join(root, 'skills/alpha-skill/SKILL.md'), 'utf8'));
} finally {
if (staged) fs.rmSync(staged.root, { recursive: true, force: true });
fs.rmSync(root, { recursive: true, force: true });
}
});
for (const passed of [true, false]) {
for (const evidence of ['', ' \t\r\n', '\u00a0\u2003']) {
test(`behavioral CLI diagnoses blank evidence for ${passed ? 'passing' : 'failing'} verdict ${JSON.stringify(evidence)}`, () => {
const root = makeSandbox();
try {
const raw = JSON.stringify({
expectations: [{ id: 1, text: 'check the result', passed, evidence }],
summary: { passed: Number(passed), failed: Number(!passed), total: 1, pass_rate: Number(passed) },
});
const base = path.join(root, 'evals', 'results', 'alpha-skill.eval-1');
writeJson(`${base}.grading.json`, { stale: true });
const result = runBehavioralWithGrading(root, raw);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stdout, /grader returned blank evidence/);
assert.doesNotMatch(result.stdout, /invalid JSON/);
assert.match(result.stdout, /raw saved to .*alpha-skill\.eval-1\.grading\.raw\.txt/);
assert.equal(fs.readFileSync(`${base}.grading.raw.txt`, 'utf8'), raw);
assert.equal(fs.existsSync(`${base}.grading.json`), false);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
}
}
for (const raw of ['not JSON', '{"expectations":', '{"expectations":[]}']) {
test(`behavioral CLI retains the generic diagnostic for invalid grading ${raw}`, () => {
const root = makeSandbox();
try {
const result = runBehavioralWithGrading(root, raw);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stdout, /grader returned invalid JSON/);
assert.doesNotMatch(result.stdout, /blank evidence/);
const base = path.join(root, 'evals', 'results', 'alpha-skill.eval-1');
assert.equal(fs.readFileSync(`${base}.grading.raw.txt`, 'utf8'), raw);
assert.equal(fs.existsSync(`${base}.grading.json`), false);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
}
test('behavioral CLI still publishes valid grading without a rejection diagnostic', () => {
const root = makeSandbox();
try {
const evidence = ' observed in the trace ';
const raw = JSON.stringify({
expectations: [{ id: 1, text: 'check the result', passed: true, evidence }],
summary: { passed: 1, failed: 0, total: 1, pass_rate: 1 },
});
const result = runBehavioralWithGrading(root, raw);
assert.equal(result.status, 0, result.stdout + result.stderr);
assert.doesNotMatch(result.stdout, /grader returned/);
const base = path.join(root, 'evals', 'results', 'alpha-skill.eval-1');
const grading = JSON.parse(fs.readFileSync(`${base}.grading.json`, 'utf8'));
assert.equal(grading.expectations[0].evidence, evidence);
assert.equal(fs.existsSync(`${base}.grading.raw.txt`), false);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
test('accepts a complete and consistent grader result', () => {
const raw = JSON.stringify({
expectations: [
{ id: 1, text: 'first expectation', passed: true, evidence: 'observed in the trace' },
{ id: 2, text: 'second expectation', passed: false, evidence: 'not observed in the trace' },
],
summary: { passed: 1, failed: 1, total: 2, pass_rate: 0.5 },
});
assert.deepEqual(parseGrading(raw, ['first expectation', 'second expectation']), JSON.parse(raw));
});
for (const passed of [true, false]) {
for (const evidence of ['', ' \t\r\n', '\u00a0\u2003']) {
test(`rejects ${passed ? 'passing' : 'failing'} grading with blank evidence ${JSON.stringify(evidence)}`, () => {
const raw = JSON.stringify({
expectations: [{ id: 1, text: 'check the result', passed, evidence }],
summary: { passed: Number(passed), failed: Number(!passed), total: 1, pass_rate: Number(passed) },
});
assert.equal(parseGrading(raw, ['check the result']), null);
});
}
}
test('preserves nonblank evidence including surrounding whitespace', () => {
const evidence = ' No matching tool call was found.\n';
const raw = JSON.stringify({
expectations: [{ id: 1, text: 'check the result', passed: false, evidence }],
summary: { passed: 0, failed: 1, total: 1, pass_rate: 0 },
});
assert.equal(parseGrading(raw, ['check the result']).expectations[0].evidence, evidence);
});
test('blank evidence retains raw grading without publishing a scored artifact', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'grading-blank-evidence-'));
try {
const base = path.join(dir, 'my-skill.eval-1');
const raw = JSON.stringify({
expectations: [{ id: 1, text: 'check the result', passed: true, evidence: '' }],
summary: { passed: 1, failed: 0, total: 1, pass_rate: 1 },
});
clearGradingSlot(base);
assert.equal(persistGradingOutcome(base, parseGrading(raw, ['check the result']), raw), false);
assert.equal(fs.existsSync(`${base}.grading.json`), false);
assert.equal(fs.readFileSync(`${base}.grading.raw.txt`, 'utf8'), raw);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
test('rejects grader results that omit expectations', () => {
const raw = JSON.stringify({
expectations: [
{ id: 1, text: 'first expectation', passed: true, evidence: 'observed in the trace' },
],
summary: { passed: 1, failed: 0, total: 1, pass_rate: 1 },
});
assert.equal(parseGrading(raw, ['first expectation', 'second expectation']), null);
});
test('rejects null expectation entries without throwing', () => {
const cases = [
{ results: [null], declared: ['first expectation'] },
{ results: [{ id: 1, text: 'first expectation', passed: true, evidence: 'observed' }, null], declared: ['first expectation', 'second expectation'] },
];
for (const { results, declared } of cases) {
const raw = JSON.stringify({
expectations: results,
summary: {
passed: results.length - 1,
failed: 1,
total: results.length,
pass_rate: (results.length - 1) / results.length,
},
});
assert.equal(parseGrading(raw, declared), null);
}
});
test('rejects incomplete or inconsistent grader summaries', () => {
const declared = ['expected behavior'];
const expectation = { id: 1, text: 'expected behavior', passed: false, evidence: 'not observed' };
const cases = [
{
expectations: [],
summary: { passed: 0, failed: 0, total: 0, pass_rate: 0 },
declared: [],
},
{
expectations: [{ id: 1, text: 'expected behavior', passed: false }],
summary: { passed: 0, failed: 1, total: 1, pass_rate: 0 },
declared,
},
{
expectations: [expectation],
summary: { passed: 1, failed: 0, total: 1, pass_rate: 1 },
declared,
},
{
expectations: [expectation],
summary: { passed: 0, total: 1, pass_rate: 0 },
declared,
},
{
expectations: [expectation],
summary: { passed: 0, failed: 1, total: 1 },
declared,
},
];
for (const { declared: d, ...grading } of cases) {
assert.equal(parseGrading(JSON.stringify(grading), d), null);
}
});
test('names a positive prompt that passes top_k but does not rank first', () => {
const root = makeSandbox();
writeSkill(root, 'alpha-skill', 'Handles alpha widgets. Use when changing alpha widgets.');
writeSkill(root, 'beta-skill', 'Tunes widget caches. Use when tuning the widget cache for alpha.');
writeJson(
path.join(root, 'evals', 'cases', 'alpha-skill.json'),
completeCase('alpha-skill', 'tune the alpha widget cache', 3),
);
writeJson(
path.join(root, 'evals', 'cases', 'beta-skill.json'),
completeCase('beta-skill', 'tune the widget cache', 1),
);
const result = run(root);
assert.equal(result.status, 0, result.stdout + result.stderr);
assert.match(result.stdout, /alpha-skill: positive prompt ranked #2, inside top 3 but not first/);
assert.match(result.stdout, /#1: beta-skill \(\d\.\d{3}\) vs \d\.\d{3}/);
assert.match(result.stdout, /rank-1 rate: 50% \(3\/6/);
});
test('fails when a skill has no eval case file', () => {
const root = makeSandbox();
writeSkill(root, 'alpha-skill', 'Handles alpha widgets. Use when changing alpha widgets.');
const result = run(root);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stdout, /no eval case file/);
});
test('fails when an eval case is below the required minimums', () => {
const root = makeSandbox();
writeSkill(root, 'alpha-skill', 'Handles alpha widgets. Use when changing alpha widgets.');
writeJson(path.join(root, 'evals', 'cases', 'alpha-skill.json'), {
skill_name: 'alpha-skill',
trigger: {
positive: [{ prompt: 'change alpha widget', top_k: 1 }],
negative: [],
},
evals: [behavioralEval()],
});
const result = run(root);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stdout, /below required minimums/);
});
test('fails when a behavioral eval references a missing fixture', () => {
const root = makeSandbox();
writeSkill(root, 'alpha-skill', 'Handles alpha widgets. Use when changing alpha widgets.');
writeJson(
path.join(root, 'evals', 'cases', 'alpha-skill.json'),
completeCase('alpha-skill', 'change alpha widget', 1, ['missing/project.txt']),
);
const result = run(root);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stdout, /fixture not found/);
});
test('requires fixtures for execution evals', () => {
const root = makeSandbox();
writeSkill(root, 'alpha-skill', 'Handles alpha widgets. Use when changing alpha widgets.');
writeJson(
path.join(root, 'evals', 'cases', 'alpha-skill.json'),
completeCase('alpha-skill', 'change alpha widget', 1, []),
);
const result = run(root);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stdout, /needs a non-empty files\[\] fixture list/);
});
test('allows dialogue evals without fixtures', () => {
const root = makeSandbox();
writeSkill(root, 'alpha-skill', 'Handles alpha widgets. Use when changing alpha widgets.');
const evalCase = completeCase('alpha-skill', 'change alpha widget');
evalCase.evals = [{ ...behavioralEval([]), kind: 'dialogue' }];
writeJson(path.join(root, 'evals', 'cases', 'alpha-skill.json'), evalCase);
const result = run(root);
assert.equal(result.status, 0, result.stdout + result.stderr);
});
test('rejects provisional execution evals', () => {
const root = makeSandbox();
writeSkill(root, 'alpha-skill', 'Handles alpha widgets. Use when changing alpha widgets.');
const evalCase = completeCase('alpha-skill', 'change alpha widget');
evalCase.evals[0].trust_level = 'provisional';
writeJson(path.join(root, 'evals', 'cases', 'alpha-skill.json'), evalCase);
const result = run(root);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stdout, /is still provisional/);
});
test('allows dialogue evals with a legacy provisional marker', () => {
const root = makeSandbox();
writeSkill(root, 'alpha-skill', 'Handles alpha widgets. Use when changing alpha widgets.');
const evalCase = completeCase('alpha-skill', 'change alpha widget');
evalCase.evals = [{ ...behavioralEval([]), kind: 'dialogue', trust_level: 'provisional' }];
writeJson(path.join(root, 'evals', 'cases', 'alpha-skill.json'), evalCase);
const result = run(root);
assert.equal(result.status, 0, result.stdout + result.stderr);
});
test('rejects unknown behavioral eval kinds', () => {
const root = makeSandbox();
writeSkill(root, 'alpha-skill', 'Handles alpha widgets. Use when changing alpha widgets.');
const evalCase = completeCase('alpha-skill', 'change alpha widget');
evalCase.evals[0].kind = 'conversation';
writeJson(path.join(root, 'evals', 'cases', 'alpha-skill.json'), evalCase);
const result = run(root);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stdout, /unknown kind "conversation"/);
});
test('dry-runs a fixtureless dialogue eval', () => {
const root = makeSandbox();
writeSkill(root, 'alpha-skill', 'Handles alpha widgets. Use when changing alpha widgets.');
const evalCase = completeCase('alpha-skill', 'change alpha widget');
evalCase.evals = [{ ...behavioralEval([]), kind: 'dialogue' }];
writeJson(path.join(root, 'evals', 'cases', 'alpha-skill.json'), evalCase);
const result = run(root, ['--behavioral', 'alpha-skill', '--dry-run']);
assert.equal(result.status, 0, result.stdout + result.stderr);
assert.match(result.stdout, /dialogue transcript/);
assert.match(result.stdout, /--add-dir <staged skill resources>/);
});
test('enforces the configured rank-1 floor', () => {
const root = makeSandbox();
writeSkill(root, 'alpha-skill', 'Handles widget work. Use when implementing widget changes.');
writeSkill(
root,
'beta-skill',
'Diagnoses urgent widget failures in production. Use when repairing urgent widget failures.',
);
writeJson(
path.join(root, 'evals', 'cases', 'alpha-skill.json'),
completeCase('alpha-skill', 'urgent widget failure production', 2),
);
writeJson(
path.join(root, 'evals', 'cases', 'beta-skill.json'),
completeCase('beta-skill', 'repair urgent widget failure', 1),
);
const passing = run(root, ['--min-rank1', '50']);
const failing = run(root, ['--min-rank1', '60']);
assert.equal(passing.status, 0, passing.stdout + passing.stderr);
assert.equal(failing.status, 1, failing.stdout + failing.stderr);
assert.match(failing.stdout, /below required 60%/);
});
test('rejects an invalid rank-1 floor', () => {
const root = makeSandbox();
const result = run(root, ['--min-rank1', '101']);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stderr, /--min-rank1 must be a number from 0 to 100/);
});
test('routes clipped prompt forms to their long-form skill (see #660)', () => {
const root = makeSandbox();
// Note: the skill name must not contain the clipped form itself — name
// tokens are weighted 2x, which would route the prompt even without the
// alias map. The prompt shares no vocabulary with the description except
// the clipped/long pair (auth/authentication): without the alias map the
// skill scores zero and the positive prompt fails.
writeSkill(
root,
'credential-skill',
'Protects credential verification. Use when configuring authentication ceremonies.',
);
writeSkill(root, 'paint-skill', 'Paints sheds. Use when choosing exterior paint colors.');
writeJson(
path.join(root, 'evals', 'cases', 'credential-skill.json'),
completeCase('credential-skill', 'fix broken auth checks', 1),
);
writeJson(
path.join(root, 'evals', 'cases', 'paint-skill.json'),
completeCase('paint-skill', 'choose exterior paint colors', 1),
);
const result = run(root);
assert.equal(result.status, 0, result.stdout + result.stderr);
});
test('folds -ifications to their -ify verb, not to the unrelated "specific" (see #660)', () => {
const root = makeSandbox();
// "specification" is a nominalization of "specify", so it must reach the
// spec skill. Stripping a bare "-ation" instead yields "specific", which
// silently cross-merges the two words and starves the spec skill. Each
// prompt shares vocabulary with its own skill only.
writeSkill(root, 'spec-skill', 'Agree on requirements up front. Use when you specify behaviour.');
writeSkill(root, 'specific-skill', 'Narrow a choice down. Use when one target matters.');
writeJson(
path.join(root, 'evals', 'cases', 'spec-skill.json'),
completeCase('spec-skill', 'document a specification for the billing rollout', 1),
);
writeJson(
path.join(root, 'evals', 'cases', 'specific-skill.json'),
completeCase('specific-skill', 'narrow the choice down to one target', 1),
);
const result = run(root);
assert.equal(result.status, 0, result.stdout + result.stderr);
});
test('strips -ations as well as -ation so plurals do not out-stem singulars (see #660)', () => {
const root = makeSandbox();
// "migration" and "migrations" are the same word, so both forms have to
// reach the migration skill. With only "-ation" in the suffix list the
// plural keeps its "-s" ("migration" -> "migr" vs "migrations" ->
// "migration"), which splits the pair and halves the score. Neither
// prompt shares vocabulary with the other skill.
writeSkill(root, 'migration-skill', 'Move old data stores off production without downtime.');
writeSkill(root, 'recipe-skill', 'Plan tonight dinner from what is already in the cupboard.');
writeJson(
path.join(root, 'evals', 'cases', 'migration-skill.json'),
completeCase('migration-skill', 'plan the database migrations', 1),
);
writeJson(
path.join(root, 'evals', 'cases', 'recipe-skill.json'),
completeCase('recipe-skill', 'plan tonight dinner from the cupboard', 1),
);
const result = run(root);
assert.equal(result.status, 0, result.stdout + result.stderr);
});
// ---------- parseGrading expectation-binding tests ----------
test('accepts reordered-but-complete grader results', () => {
const expectations = ['first expectation', 'second expectation'];
const raw = JSON.stringify({
expectations: [
{ id: 2, text: 'second expectation', passed: false, evidence: 'not observed' },
{ id: 1, text: 'first expectation', passed: true, evidence: 'observed in the trace' },
],
summary: { passed: 1, failed: 1, total: 2, pass_rate: 0.5 },
});
const result = parseGrading(raw, expectations);
assert.notEqual(result, null);
assert.equal(result.summary.passed, 1);
assert.equal(result.summary.failed, 1);
});
test('rejects duplicate grader results for the same expectation', () => {
const expectations = ['first expectation', 'second expectation'];
// Valid baseline: each id appears exactly once
const validRaw = JSON.stringify({
expectations: [
{ id: 1, text: 'first expectation', passed: true, evidence: 'observed' },
{ id: 2, text: 'second expectation', passed: false, evidence: 'not observed' },
],
summary: { passed: 1, failed: 1, total: 2, pass_rate: 0.5 },
});
assert.notEqual(parseGrading(validRaw, expectations), null);
// Duplicate: id 1 appears twice, id 2 is missing
const dupRaw = JSON.stringify({
expectations: [
{ id: 1, text: 'first expectation', passed: true, evidence: 'observed' },
{ id: 1, text: 'first expectation', passed: true, evidence: 'observed again' },
],
summary: { passed: 2, failed: 0, total: 2, pass_rate: 1 },
});
assert.equal(parseGrading(dupRaw, expectations), null);
});
test('rejects grader results whose ids are not in the declared set', () => {
const expectations = ['first expectation', 'second expectation'];
// Valid baseline
const validRaw = JSON.stringify({
expectations: [
{ id: 1, text: 'first expectation', passed: true, evidence: 'observed' },
{ id: 2, text: 'second expectation', passed: false, evidence: 'not observed' },
],
summary: { passed: 1, failed: 1, total: 2, pass_rate: 0.5 },
});
assert.notEqual(parseGrading(validRaw, expectations), null);
// id 3 is out of range 1..2
const badIdRaw = JSON.stringify({
expectations: [
{ id: 1, text: 'first expectation', passed: true, evidence: 'observed' },
{ id: 3, text: 'unknown expectation', passed: false, evidence: 'not found' },
],
summary: { passed: 1, failed: 1, total: 2, pass_rate: 0.5 },
});
assert.equal(parseGrading(badIdRaw, expectations), null);
});
test('rejects a result set that omits a declared expectation', () => {
const expectations = ['first expectation', 'second expectation', 'third expectation'];
// Valid baseline
const validRaw = JSON.stringify({
expectations: [
{ id: 1, text: 'first expectation', passed: true, evidence: 'observed' },
{ id: 2, text: 'second expectation', passed: true, evidence: 'observed' },
{ id: 3, text: 'third expectation', passed: false, evidence: 'not observed' },
],
summary: { passed: 2, failed: 1, total: 3, pass_rate: 2 / 3 },
});
assert.notEqual(parseGrading(validRaw, expectations), null);
// Only 2 results for 3 expectations (id 3 omitted)
const partialRaw = JSON.stringify({
expectations: [
{ id: 1, text: 'first expectation', passed: true, evidence: 'observed' },
{ id: 2, text: 'second expectation', passed: true, evidence: 'observed' },
],
summary: { passed: 2, failed: 0, total: 2, pass_rate: 1 },
});
assert.equal(parseGrading(partialRaw, expectations), null);
});
test('derives pass_rate from counters rather than trusting the grader value', () => {
const expectations = ['first expectation', 'second expectation'];
// Correct pass_rate should be accepted
const validRaw = JSON.stringify({
expectations: [
{ id: 1, text: 'first expectation', passed: true, evidence: 'observed' },
{ id: 2, text: 'second expectation', passed: false, evidence: 'not observed' },
],
summary: { passed: 1, failed: 1, total: 2, pass_rate: 0.5 },
});
const valid = parseGrading(validRaw, expectations);
assert.notEqual(valid, null);
assert.equal(valid.summary.pass_rate, 0.5);
// Wrong pass_rate with correct counters: accepted, but recomputed
const wrongRaw = JSON.stringify({
expectations: [
{ id: 1, text: 'first expectation', passed: true, evidence: 'observed' },
{ id: 2, text: 'second expectation', passed: false, evidence: 'not observed' },
],
summary: { passed: 1, failed: 1, total: 2, pass_rate: 0.999 },
});
const corrected = parseGrading(wrongRaw, expectations);
assert.notEqual(corrected, null);
assert.equal(corrected.summary.pass_rate, 0.5);
// The integer counters stay exact checks
assert.equal(corrected.summary.passed, 1);
assert.equal(corrected.summary.failed, 1);
});
test('replaces paraphrased grader text with the declared expectation', () => {
const expectations = ['first expectation', 'second expectation'];
const raw = JSON.stringify({
expectations: [
{ id: 2, text: 'the agent did the second thing', passed: false, evidence: 'not observed' },
{ id: 1, text: 'roughly the first one', passed: true, evidence: 'observed' },
],
summary: { passed: 1, failed: 1, total: 2, pass_rate: 0.5 },
});
const result = parseGrading(raw, expectations);
assert.notEqual(result, null);
assert.equal(result.expectations.find((r) => r.id === 1).text, 'first expectation');
assert.equal(result.expectations.find((r) => r.id === 2).text, 'second expectation');
});
// ---------- persistGradingOutcome stale-cleanup tests ----------
test('rejected grading writes raw output', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'grading-cleanup-'));
try {
const base = path.join(dir, 'my-skill.eval-1');
const result = persistGradingOutcome(base, null, 'unparseable grader output');
assert.equal(result, false);
assert.equal(fs.existsSync(`${base}.grading.raw.txt`), true, 'raw output must be written');
assert.equal(fs.readFileSync(`${base}.grading.raw.txt`, 'utf8'), 'unparseable grader output');
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
test('rejected grading succeeds even when no prior grading.json exists', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'grading-cleanup-'));
try {
const base = path.join(dir, 'my-skill.eval-1');
const result = persistGradingOutcome(base, null, 'bad output');
assert.equal(result, false);
assert.equal(fs.existsSync(`${base}.grading.raw.txt`), true);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
test('accepted grading writes grading.json', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'grading-cleanup-'));
try {
const base = path.join(dir, 'my-skill.eval-1');
const grading = {
expectations: [{ id: 1, text: 'x', passed: true, evidence: 'y' }],
summary: { passed: 1, failed: 0, total: 1, pass_rate: 1 },
};
const result = persistGradingOutcome(base, grading, 'unused');
assert.equal(result, true);
const written = JSON.parse(fs.readFileSync(`${base}.grading.json`, 'utf8'));
assert.deepEqual(written, grading);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
// ---------- upfront slot-clearing tests ----------
test('a grader that throws leaves no result file behind', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'grading-crash-'));
try {
const base = path.join(dir, 'my-skill.eval-1');
// Stale files from a prior run
fs.writeFileSync(`${base}.grading.json`, '{"previous":"run"}\n');
fs.writeFileSync(`${base}.grading.raw.txt`, 'previous raw output');
fs.writeFileSync(`${base}.execution.raw.txt`, 'previous partial trace');
fs.writeFileSync(`${base}.execution.error.txt`, 'previous execution failure');
clearGradingSlot(base);
// Executor or grader crashes — persistGradingOutcome is never called
assert.equal(fs.existsSync(`${base}.grading.json`), false, 'stale grading.json must not survive a crash');
assert.equal(fs.existsSync(`${base}.grading.raw.txt`), false, 'stale grading.raw.txt must not survive a crash');
assert.equal(fs.existsSync(`${base}.execution.raw.txt`), false, 'old partial trace must not survive a new attempt');
assert.equal(fs.existsSync(`${base}.execution.error.txt`), false, 'old execution failure must not survive a new attempt');
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
test('successful grading leaves no stale raw file behind', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'grading-success-'));
try {
const base = path.join(dir, 'my-skill.eval-1');
// Stale raw from a prior rejected run
fs.writeFileSync(`${base}.grading.raw.txt`, 'previous raw output');
clearGradingSlot(base);
// Successful grading
const grading = {
expectations: [{ id: 1, text: 'x', passed: true, evidence: 'y' }],
summary: { passed: 1, failed: 0, total: 1, pass_rate: 1 },
};
persistGradingOutcome(base, grading, 'unused');
assert.equal(fs.existsSync(`${base}.grading.json`), true, 'grading.json must be written');
assert.equal(fs.existsSync(`${base}.grading.raw.txt`), false, 'stale grading.raw.txt must not survive');
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
// ---------- extractExecutorModel tests ----------
test('extracts model from the stream-json init event', () => {
const trace = [
'{"type":"system","subtype":"init","model":"claude-sonnet-4-6-20250514","session_id":"abc"}',
'{"type":"assistant","message":{"id":"msg_1","content":[{"type":"text","text":"Hi"}]}}',
'{"type":"result","subtype":"success","result":"Hi"}',
].join('\n');
assert.equal(extractExecutorModel(trace), 'claude-sonnet-4-6-20250514');
});
test('returns null when the trace has no init event', () => {
const trace = [
'{"type":"assistant","message":{"id":"msg_1","content":[]}}',
'{"type":"result","subtype":"success","result":"done"}',
].join('\n');
assert.equal(extractExecutorModel(trace), null);
});
test('returns null when the init event has no model field', () => {
const trace = '{"type":"system","subtype":"init","session_id":"abc"}\n';
assert.equal(extractExecutorModel(trace), null);
});
test('tolerates non-JSON lines in the trace', () => {
const trace = [
'not json',
'{"type":"system","subtype":"init","model":"claude-opus-4-6","session_id":"abc"}',
].join('\n');
assert.equal(extractExecutorModel(trace), 'claude-opus-4-6');
});
// ---------- persistGradingOutcome run identity tests ----------
test('accepted grading includes run metadata when provided', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'grading-run-meta-'));
try {
const base = path.join(dir, 'my-skill.eval-1');
const grading = {
expectations: [{ id: 1, text: 'x', passed: true, evidence: 'y' }],
summary: { passed: 1, failed: 0, total: 1, pass_rate: 1 },
};
const runMeta = {
executor_model: 'claude-sonnet-4-6-20250514',
grader_model: 'unknown',
timestamp: '2026-09-21T00:00:00.000Z',
};
persistGradingOutcome(base, grading, 'unused', runMeta);
const written = JSON.parse(fs.readFileSync(`${base}.grading.json`, 'utf8'));
assert.deepEqual(written.run, runMeta);
assert.deepEqual(written.expectations, grading.expectations);
assert.deepEqual(written.summary, grading.summary);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
test('accepted grading omits run key when no metadata is provided', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'grading-no-meta-'));
try {
const base = path.join(dir, 'my-skill.eval-1');
const grading = {
expectations: [{ id: 1, text: 'x', passed: true, evidence: 'y' }],
summary: { passed: 1, failed: 0, total: 1, pass_rate: 1 },
};
persistGradingOutcome(base, grading, 'unused');
const written = JSON.parse(fs.readFileSync(`${base}.grading.json`, 'utf8'));
assert.equal('run' in written, false);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
test('materializes a git baseline and applies a working-tree patch', () => {
const workspace = materializeWorkspace({ files: ['git-workflow-and-versioning'] });
try {
const status = spawnSync('git', ['status', '--short'], { cwd: workspace, encoding: 'utf8' });
const commits = spawnSync('git', ['rev-list', '--count', 'HEAD'], { cwd: workspace, encoding: 'utf8' });
assert.equal(status.status, 0, status.stdout + status.stderr);
assert.match(status.stdout, / M git-workflow-and-versioning\/app\.js/);
assert.equal(commits.stdout.trim(), '1');
assert.equal(fs.existsSync(path.join(workspace, '.eval')), false);
} finally {
fs.rmSync(workspace, { recursive: true, force: true });
}
});
// The real CLI, with only the paid Claude subprocess replaced. Works without
// a shell shim on Windows as well as Unix; git and filesystem operations stay real.
function recordedSandbox(t, kind = 'dialogue') {
const root = makeSandbox();
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
writeSkill(root, 'alpha-skill', 'Handles alpha widgets.');
fs.mkdirSync(path.join(root, 'skills/alpha-skill/references'));
fs.writeFileSync(path.join(root, 'skills/alpha-skill/references/guide.md'), 'local reference');
fs.mkdirSync(path.join(root, 'references'));
fs.writeFileSync(path.join(root, 'references/shared.md'), 'shared reference');
const rubric = completeCase('alpha-skill', 'change alpha widget');
rubric.evals = [{ ...behavioralEval(kind === 'dialogue' ? [] : ['project/context.txt']), kind }];
const caseFile = path.join(root, 'evals', 'cases', 'alpha-skill.json');
writeJson(caseFile, rubric);
const stub = path.join(root, 'claude-stub.cjs');
fs.writeFileSync(stub, `
const fs = require('node:fs');
const cp = require('node:child_process');
const original = cp.execFileSync;
cp.execFileSync = (command, args, options) => {
if (command !== 'claude') return original(command, args, options);
fs.appendFileSync(process.env.CALL_LOG, JSON.stringify({args, input: options?.input}) + '\\n');
if (args.includes('--version')) return '2.1.test\\n';
if (args.includes('stream-json')) return '{"type":"system","subtype":"init","model":"observed-model"}\\n{"type":"result","subtype":"success","is_error":false,"result":"original evidence"}\\n';
if (process.env.GRADER_THROWS) throw new Error('grader unavailable');
return process.env.GRADER_RAW || '{"expectations":[{"id":1,"text":"paraphrase","passed":true,"evidence":"original evidence"}],"summary":{"passed":1,"failed":0,"total":1,"pass_rate":1}}';
};
`);
const log = path.join(root, 'calls.jsonl');
return {
root, rubric, caseFile,
run: (args, env = {}) => spawnSync(process.execPath, ['--require', stub, path.join(root, 'scripts/run-evals.js'), ...args], {
cwd: root, encoding: 'utf8', env: { ...process.env, CALL_LOG: log, ...env },
}),
calls: () => fs.existsSync(log) ? fs.readFileSync(log, 'utf8').trim().split('\n').map(JSON.parse) : [],
records: () => fs.readdirSync(path.join(root, 'evals/results')).filter(n => n.endsWith('.record.json')).map(n => path.join(root, 'evals/results', n)),
};
}
for (const kind of ['dialogue', 'execution']) {
test(`records and regrades the same ${kind} evidence without another executor`, t => {
const s = recordedSandbox(t, kind);
const first = s.run(['--behavioral', 'alpha-skill', '--record', '--model', 'requested-model']);
assert.equal(first.status, 0, first.stdout + first.stderr);
assert.equal(s.records().length, 1);
const file = s.records()[0];
const original = fs.readFileSync(file, 'utf8');
const record = JSON.parse(original);
assert.equal(record.evidence.run.executor_model, 'observed-model');
assert.equal(record.evidence.run.requested_model, 'requested-model');
assert.equal(record.evidence.run.claude_version, '2.1.test');
const digest = (text) => require('node:crypto').createHash('sha256').update(text).digest('hex');
assert.equal(record.evidence.references, undefined);
assert.deepEqual(record.evidence.staged, [
{ path: 'references/shared.md', sha256: digest('shared reference') },
{ path: 'skills/alpha-skill/SKILL.md', sha256: digest(record.evidence.skill_text) },
{ path: 'skills/alpha-skill/references/guide.md', sha256: digest('local reference') },
]);
const latest = fs.readFileSync(path.join(s.root, 'evals/results/alpha-skill.eval-1.grading.json'), 'utf8');
s.rubric.evals[0].expectations = ['Revised expectation'];
writeJson(s.caseFile, s.rubric);
// Replay must not read today's skill or fixture tree.
fs.rmSync(path.join(s.root, 'skills'), { recursive: true });
fs.rmSync(path.join(s.root, 'evals/fixtures'), { recursive: true });
const replay = s.run(['--regrade', file, '--rubric', s.caseFile, '--model', 'new-grader']);
assert.equal(replay.status, 0, replay.stdout + replay.stderr);
assert.match(replay.stdout, /historical/i);
assert.match(replay.stdout, /staged 3 file\(s\), listed in the record/);
assert.equal(fs.readFileSync(file, 'utf8'), original);
assert.equal(fs.readFileSync(path.join(s.root, 'evals/results/alpha-skill.eval-1.grading.json'), 'utf8'), latest);
const calls = s.calls().filter(c => !c.args.includes('--version'));
assert.equal(calls.length, 3);
assert.equal(calls.filter(c => c.args.includes('stream-json')).length, 1);
assert.match(calls[2].input, /Revised expectation/);
assert.match(calls[2].input, /original evidence/);
assert.equal(calls[2].args.at(-1), 'new-grader');
const attempts = fs.readdirSync(path.dirname(file)).filter(n => n.includes('.attempt-'));
assert.equal(attempts.length, 2);
const grades = attempts.map(n => JSON.parse(fs.readFileSync(path.join(path.dirname(file), n), 'utf8')));
assert.ok(grades.some(g => g.historical === true && g.grading.expectations[0].text === 'Revised expectation'));
});
}
function recordOnce(s, env) {
const result = s.run(['--behavioral', 'alpha-skill', '--record'], env);
return { result, file: s.records()[0] };
}
for (const passed of [true, false]) {
for (const evidence of ['', ' \t\r\n', '\u00a0\u2003']) {
test(`record and regrade retain blank evidence diagnostics for ${passed ? 'passing' : 'failing'} verdict ${JSON.stringify(evidence)}`, t => {
const s = recordedSandbox(t);
const raw = JSON.stringify({
expectations: [{ id: 1, text: 'check the result', passed, evidence }],
summary: { passed: Number(passed), failed: Number(!passed), total: 1, pass_rate: Number(passed) },
});
const { result, file } = recordOnce(s, { GRADER_RAW: raw });
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stdout, /grader returned blank evidence/);
assert.doesNotMatch(result.stdout + result.stderr, /invalid JSON/);
const original = fs.readFileSync(file, 'utf8');
const base = path.join(s.root, 'evals/results/alpha-skill.eval-1');
assert.equal(fs.existsSync(`${base}.grading.json`), false);
assert.equal(fs.readFileSync(`${base}.grading.raw.txt`, 'utf8'), raw);
const replay = s.run(['--regrade', file, '--rubric', s.caseFile], { GRADER_RAW: raw });
assert.equal(replay.status, 1, replay.stdout + replay.stderr);
assert.match(replay.stderr, /grader returned blank evidence/);
assert.doesNotMatch(replay.stderr, /invalid JSON/);
const attempts = fs.readdirSync(path.dirname(file)).filter(n => n.includes('.attempt-'));
assert.equal(attempts.length, 2);
for (const name of attempts) {
const attempt = JSON.parse(fs.readFileSync(path.join(path.dirname(file), name), 'utf8'));
assert.equal(attempt.status, 'invalid');
assert.equal(attempt.gradingError, 'grader returned blank evidence');
assert.equal(attempt.raw, raw);
assert.equal(attempt.grading, null);
}
const retry = s.run(['--regrade', file, '--rubric', s.caseFile]);
assert.equal(retry.status, 0, retry.stdout + retry.stderr);
assert.equal(fs.readFileSync(file, 'utf8'), original);
assert.equal(fs.existsSync(`${base}.grading.json`), false);
assert.equal(fs.readFileSync(`${base}.grading.raw.txt`, 'utf8'), raw);
const calls = s.calls().filter(c => !c.args.includes('--version'));
assert.equal(calls.filter(c => c.args.includes('stream-json')).length, 1);
for (const call of calls.filter(c => !c.args.includes('stream-json'))) {
assert.match(call.input, /provide non-blank evidence/);
}
});
}
}
test('rejects altered evidence before any Claude invocation', t => {
const s = recordedSandbox(t);
const { file } = recordOnce(s);
const original = JSON.parse(fs.readFileSync(file, 'utf8'));
const calls = s.calls().length;
for (const field of ['trace', 'skill_text', 'eval', 'staged', 'run']) {
const changed = structuredClone(original);
changed.evidence[field] = 'altered';
writeJson(file, changed);
const result = s.run(['--regrade', file, '--rubric', s.caseFile]);
assert.equal(result.status, 1);
assert.match(result.stderr, /integrity/);
assert.equal(s.calls().length, calls);
}
});
test('rejects a staged list that does not match the record, even with a valid digest', t => {
const s = recordedSandbox(t);
const { file } = recordOnce(s);
const original = JSON.parse(fs.readFileSync(file, 'utf8'));
const calls = s.calls().length;
const digest = (value) => require('node:crypto').createHash('sha256').update(JSON.stringify(value)).digest('hex');
const changes = [
e => { e.staged = e.staged.filter(f => !f.path.endsWith('/SKILL.md')); },
e => { e.staged[0].path = 'evals/fixtures/project/context.txt'; },
e => { e.staged[0].path = 'references/../secret.txt'; },
e => { e.staged.push({ ...e.staged[0] }); },
e => { e.skill_text += 'changed'; },
e => { delete e.staged; e.references = { status: 'not-staged', files: [] }; },
];
for (const change of changes) {
const changed = structuredClone(original);
change(changed.evidence);
changed.sha256 = digest(changed.evidence);
writeJson(file, changed);
const result = s.run(['--regrade', file, '--rubric', s.caseFile]);
assert.equal(result.status, 1);
assert.match(result.stderr, /Invalid behavioral record evidence/);
assert.equal(s.calls().length, calls);
}
});
test('rejects a changed task, mismatched rubric, or invalid expectations before model calls', t => {
const s = recordedSandbox(t);
const { file } = recordOnce(s);
const calls = s.calls().length;
const changes = [
r => { r.evals[0].prompt = 'different task'; },
r => { r.evals[0].kind = 'execution'; },
r => { r.evals[0].files = ['different/input']; },
r => { r.skill_name = 'another-skill'; },
r => { r.evals[0].id = 9; },
r => { r.evals.push(structuredClone(r.evals[0])); },
r => { r.evals[0].expectations = []; },
r => { r.evals[0].expectations = [null]; },
];
for (const change of changes) {
const rubric = structuredClone(s.rubric);
change(rubric);
writeJson(s.caseFile, rubric);
const result = s.run(['--regrade', file, '--rubric', s.caseFile]);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.equal(s.calls().length, calls);
}
});
for (const [env, status] of [[{ GRADER_RAW: 'invalid response' }, 'invalid'], [{ GRADER_THROWS: '1' }, 'error']]) {
test(`keeps the generation and ${status} attempt when the grader fails`, t => {
const s = recordedSandbox(t);
const { result, file } = recordOnce(s, env);
assert.equal(result.status, 1);
assert.ok(file);
const original = fs.readFileSync(file, 'utf8');
const attemptFile = fs.readdirSync(path.dirname(file)).find(n => n.includes('.attempt-'));
const attempt = JSON.parse(fs.readFileSync(path.join(path.dirname(file), attemptFile), 'utf8'));
assert.equal(attempt.status, status);
if (status === 'invalid') assert.equal(attempt.raw, 'invalid response');
else assert.match(attempt.error, /grader unavailable/);
const replay = s.run(['--regrade', file, '--rubric', s.caseFile]);
assert.equal(replay.status, 0, replay.stdout + replay.stderr);
assert.equal(fs.readFileSync(file, 'utf8'), original);
assert.equal(s.calls().filter(c => c.args.includes('stream-json')).length, 1);
});
}
test('record dry-run and replay dry-run make no Claude calls or output changes', t => {
const s = recordedSandbox(t);
assert.equal(s.run(['--behavioral', 'alpha-skill', '--record', '--dry-run']).status, 0);
assert.equal(s.calls().length, 0);
assert.equal(fs.existsSync(path.join(s.root, 'evals/results')), false);
const { file } = recordOnce(s);
const calls = s.calls().length;
const files = fs.readdirSync(path.dirname(file));
assert.equal(s.run(['--regrade', file, '--rubric', s.caseFile, '--dry-run']).status, 0);
assert.equal(s.calls().length, calls);
assert.deepEqual(fs.readdirSync(path.dirname(file)), files);
});
test('ordinary runs retain their output shape and do not record or query CLI version', t => {
const s = recordedSandbox(t);
assert.equal(s.run(['--behavioral', 'alpha-skill']).status, 0);
const files = fs.readdirSync(path.join(s.root, 'evals/results'));
assert.deepEqual(files, ['alpha-skill.eval-1.grading.json']);
assert.equal(s.calls().length, 2);
});
test('separate generations and repeated regrades preserve every record and attempt', t => {
const s = recordedSandbox(t);
const first = recordOnce(s).file;
const original = fs.readFileSync(first, 'utf8');
recordOnce(s);
assert.equal(s.records().length, 2);
for (let i = 0; i < 2; i++) assert.equal(s.run(['--regrade', first, '--rubric', s.caseFile]).status, 0);
assert.equal(fs.readFileSync(first, 'utf8'), original);
assert.equal(fs.readdirSync(path.dirname(first)).filter(n => n.includes('.attempt-')).length, 4);
});
test('rejects incomplete and incompatible record/replay CLI options', t => {
const s = recordedSandbox(t);
for (const args of [
['--record'], ['--model'], ['--model', '--record'], ['--regrade'],
['--regrade', 'missing'], ['--rubric', 'missing'],
['--regrade', 'missing', '--rubric', 'missing', '--behavioral', 'alpha-skill'],
['--regrade', 'missing', '--rubric', 'missing', '--record'],
['--regrade', 'missing', '--rubric', 'missing', '--min-rank1', '95'],
]) {
assert.equal(s.run(args).status, 1, args.join(' '));
assert.equal(s.calls().length, 0);
}
});
test('a failing historical grade stays a failure and retains its attempt', t => {
const s = recordedSandbox(t);
const { file } = recordOnce(s);
const raw = JSON.stringify({ expectations: [{ id: 1, text: 'x', passed: false, evidence: 'not observed' }],
summary: { passed: 0, failed: 1, total: 1, pass_rate: 0 } });
const result = s.run(['--regrade', file, '--rubric', s.caseFile], { GRADER_RAW: raw });
assert.equal(result.status, 1);
assert.match(result.stdout, /0\/1 expectations passed/);
const attempts = fs.readdirSync(path.dirname(file)).filter(n => n.includes('.attempt-'));
assert.equal(attempts.length, 2);
});
test('rejects unknown record versions and malformed JSON without calling Claude', t => {
const s = recordedSandbox(t);
const { file } = recordOnce(s);
const calls = s.calls().length;
const record = JSON.parse(fs.readFileSync(file, 'utf8'));
record.version = 99;
writeJson(file, record);
assert.equal(s.run(['--regrade', file, '--rubric', s.caseFile]).status, 1);
fs.writeFileSync(file, '{broken json');
assert.equal(s.run(['--regrade', file, '--rubric', s.caseFile]).status, 1);
assert.equal(s.calls().length, calls);
});
test('record mode rejects duplicate behavioral IDs before any Claude invocation or output changes', t => {
const s = recordedSandbox(t);
s.rubric.evals.push({ ...s.rubric.evals[0], prompt: 'A different evaluation' });
writeJson(s.caseFile, s.rubric);
for (const flags of [[], ['--dry-run']]) {
const result = s.run(['--behavioral', 'alpha-skill', '--record', ...flags]);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stderr, /duplicate behavioral eval id=1/);
assert.equal(s.calls().length, 0);
assert.equal(fs.existsSync(path.join(s.root, 'evals/results')), false);
}
});
test('fixture baseline commits ignore inherited user signing requirements', () => {
const root = makeSandbox();
const config = path.join(root, 'global.gitconfig');
const temp = fs.mkdtempSync(path.join(os.tmpdir(), 'fixture-signing-test-'));
try {
fs.writeFileSync(config, '[commit]\n\tgpgsign = true\n[gpg]\n\tprogram = definitely-missing-eval-signer\n');
const result = spawnSync(process.execPath, ['-e', `
const fs = require('node:fs');
const { materializeWorkspace } = require(${JSON.stringify(path.join(root, 'scripts', 'run-evals.js'))});
let workspace;
try { workspace = materializeWorkspace({ files: ['project/context.txt'] }); }
finally { if (workspace) fs.rmSync(workspace, { recursive: true, force: true }); }
`], { cwd: root, encoding: 'utf8', env: { ...process.env, GIT_CONFIG_GLOBAL: config, TMPDIR: temp, TMP: temp, TEMP: temp } });
assert.equal(result.status, 0, result.stdout + result.stderr);
} finally {
fs.rmSync(root, { recursive: true, force: true });
fs.rmSync(temp, { recursive: true, force: true });
}
});
test('fixture repositories ignore inherited hooks for baseline and later commits', () => {
const root = makeSandbox();
try {
const hooks = path.join(root, 'user-hooks');
const config = path.join(root, 'global.gitconfig');
fs.mkdirSync(hooks);
fs.writeFileSync(path.join(hooks, 'pre-commit'), '#!/bin/sh\necho inherited-hook-ran >&2\nexit 1\n', { mode: 0o755 });
const configured = spawnSync('git', ['config', '--file', config, 'core.hooksPath', hooks], { encoding: 'utf8' });
assert.equal(configured.status, 0, configured.stderr);
const before = fs.readFileSync(config, 'utf8');
const result = spawnSync(process.execPath, ['-e', `
const fs = require('node:fs');
const { execFileSync } = require('node:child_process');
const { materializeWorkspace } = require(${JSON.stringify(path.join(root, 'scripts/run-evals.js'))});
let workspace;
try {
workspace = materializeWorkspace({ files: ['project/context.txt'] });
fs.appendFileSync(workspace + '/project/context.txt', 'edit');
execFileSync('git', ['commit', '-am', 'eval edit'], { cwd: workspace });
process.stdout.write(execFileSync('git', ['rev-list', '--count', 'HEAD'], { cwd: workspace }));
} finally { if (workspace) fs.rmSync(workspace, { recursive: true, force: true }); }
`], { cwd: root, encoding: 'utf8', env: { ...process.env, GIT_CONFIG_GLOBAL: config } });
assert.equal(result.status, 0, result.stdout + result.stderr);
assert.equal(result.stdout.trim(), '2');
assert.doesNotMatch(result.stderr, /inherited-hook-ran/);
assert.equal(fs.readFileSync(config, 'utf8'), before);
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
test('rejects duplicate behavioral IDs before they can overwrite a grading slot', () => {
const root = makeSandbox();
try {
writeSkill(root, 'alpha-skill', 'Handles alpha widgets. Use when changing alpha widgets.');
const evalCase = completeCase('alpha-skill', 'change alpha widget');
evalCase.evals.push({ ...behavioralEval(), prompt: 'A different evaluation' });
writeJson(path.join(root, 'evals', 'cases', 'alpha-skill.json'), evalCase);
const result = run(root);
assert.equal(result.status, 1, result.stdout + result.stderr);
assert.match(result.stdout, /duplicate behavioral eval id=1/);
const dryRun = run(root, ['--behavioral', 'alpha-skill', '--dry-run']);
assert.equal(dryRun.status, 1, dryRun.stdout + dryRun.stderr);
assert.match(dryRun.stderr, /duplicate behavioral eval id=1/);
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
test('accepts distinct behavioral IDs including zero', () => {
const root = makeSandbox();
try {
writeSkill(root, 'alpha-skill', 'Handles alpha widgets. Use when changing alpha widgets.');
const evalCase = completeCase('alpha-skill', 'change alpha widget');
evalCase.evals.push({ ...behavioralEval(), id: 0 });
writeJson(path.join(root, 'evals', 'cases', 'alpha-skill.json'), evalCase);
const result = run(root);
assert.equal(result.status, 0, result.stdout + result.stderr);
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});
test('removes the throwaway workspace when fixture setup fails', () => {
const root = makeSandbox();
const temp = fs.mkdtempSync(path.join(os.tmpdir(), 'workspace-cleanup-test-'));
try {
const result = spawnSync(process.execPath, ['-e', `
const { materializeWorkspace } = require(${JSON.stringify(path.join(root, 'scripts', 'run-evals.js'))});
try { materializeWorkspace({ files: ['project/context.txt', 'missing.txt'] }); }
catch (error) { console.error(error.message); }
`], { cwd: root, encoding: 'utf8', env: { ...process.env, TMPDIR: temp, TMP: temp, TEMP: temp } });
assert.equal(result.status, 0, result.stdout + result.stderr);
assert.match(result.stderr, /fixture listed in files\[\] not found/);
assert.deepEqual(fs.readdirSync(temp), [], 'partially copied fixture data must be removed');
} finally {
fs.rmSync(root, { recursive: true, force: true });
fs.rmSync(temp, { recursive: true, force: true });
}
});
test('loads quoted YAML skill identities using their decoded values', () => {
const root = makeSandbox();
try {
writeSkill(root, 'alpha', 'code unit tests');
const skill = path.join(root, 'skills', 'alpha', 'SKILL.md');
fs.writeFileSync(skill, fs.readFileSync(skill, 'utf8').replace('name: alpha', "name: 'alpha'")
.replace('description: code unit tests', 'description: "code unit tests"'));
writeJson(path.join(root, 'evals', 'cases', 'alpha.json'), completeCase('alpha', 'code unit tests'));
const result = run(root);
assert.equal(result.status, 0, result.stdout + result.stderr);
assert.match(result.stdout, /PASSED/);
} finally { fs.rmSync(root, { recursive: true, force: true }); }
});