eval-magic 0.6.0

One-stop CLI for running skill evals — measure whether an agent skill actually shifts behavior.
Documentation
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Runner-owned command checks across workspace construction, ingest, and finalize.

use crate::helpers::*;
use predicates::str::contains;
use serde_json::{Value, json};
use std::fs;
use std::path::{Path, PathBuf};

const COOL_DESCRIPTOR: &str = r#"label = "cool-custom-harness"

[dispatch]
exec_template = "cool-cli run --cd <eval-root>{model_arg} <dispatch_prompt_path> > <outputs_dir>/final-message.md"
"#;

fn write_project_descriptor(cwd: &Path) {
    let dir = cwd.join(".eval-magic").join("harnesses");
    fs::create_dir_all(&dir).unwrap();
    fs::write(dir.join("cool.toml"), COOL_DESCRIPTOR).unwrap();
}

fn resolve(cwd: &Path, path: &str) -> PathBuf {
    let path = Path::new(path);
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        cwd.join(path)
    }
}

fn dispatch_tasks(cwd: &Path) -> Vec<Value> {
    read_json(&iteration_dir(cwd).join("dispatch.json"))["tasks"]
        .as_array()
        .expect("dispatch.json carries tasks[]")
        .clone()
}

#[cfg(unix)]
fn setup_exists_command() -> &'static str {
    "test -f holdout/secret.txt"
}

#[cfg(windows)]
fn setup_exists_command() -> &'static str {
    "if exist holdout\\secret.txt (exit /B 0) else (exit /B 1)"
}

#[cfg(unix)]
fn held_out_compare_command() -> &'static str {
    "test \"$(cat answer.txt)\" = \"$(cat holdout/expected.txt)\""
}

#[cfg(windows)]
fn held_out_compare_command() -> &'static str {
    "fc /B answer.txt holdout\\expected.txt >NUL"
}

#[test]
fn auto_isolates_and_multi_run_tasks_get_distinct_hidden_envs() {
    let tmp = tempfile::TempDir::new().unwrap();
    let evals = r#"{ "skill_name": "mr-review", "evals": [
        { "id": "ordinary-1", "prompt": "p1", "expected_output": "o" },
        { "id": "held-out", "prompt": "p2", "expected_output": "o", "runs": 2,
          "assertions": [{ "id": "secret-test", "type": "command_check",
            "setup_files": ["holdout/secret.txt"], "command": "test -f holdout/secret.txt" }] },
        { "id": "ordinary-2", "prompt": "p3", "expected_output": "o" }
    ] }"#;
    let (skill_dir, cwd) = setup(tmp.path(), evals);
    fs::create_dir_all(skill_dir.join("mr-review/evals/holdout")).unwrap();
    fs::write(
        skill_dir.join("mr-review/evals/holdout/secret.txt"),
        "secret",
    )
    .unwrap();

    skill_eval()
        .current_dir(&cwd)
        .args(["run", "--skill-dir"])
        .arg(&skill_dir)
        .args(["--skill", "mr-review", "--mode", "new-skill", "--dry-run"])
        .assert()
        .success();

    let dispatch = read_json(&iteration_dir(&cwd).join("dispatch.json"));
    let groups = dispatch["groups"].as_array().unwrap();
    assert_eq!(groups.len(), 3);
    assert_eq!(groups[0]["evals"], json!(["ordinary-1"]));
    assert_eq!(groups[1]["evals"], json!(["held-out"]));
    assert_eq!(groups[2]["evals"], json!(["ordinary-2"]));
    assert_eq!(groups[1]["rationale"], "metric: diff_scope");

    let command_tasks: Vec<_> = dispatch["tasks"]
        .as_array()
        .unwrap()
        .iter()
        .filter(|task| task["eval_id"] == "held-out")
        .collect();
    assert_eq!(command_tasks.len(), 4, "2 conditions × 2 runs");
    let roots: std::collections::HashSet<_> = command_tasks
        .iter()
        .map(|task| task["eval_root"].as_str().unwrap())
        .collect();
    assert_eq!(roots.len(), 4, "every command-check task owns its env");
    for task in command_tasks {
        let root = Path::new(task["eval_root"].as_str().unwrap());
        let run_index = task["run_index"].as_u64().unwrap();
        assert!(
            root.to_string_lossy()
                .ends_with(&format!("run-{run_index}")),
            "root carries run suffix: {}",
            root.display()
        );
        assert!(root.exists(), "staged task env exists: {}", root.display());
        assert!(
            !root.join("holdout/secret.txt").exists(),
            "held-out setup is absent before ingest"
        );
        assert_eq!(task["fixtures"], json!([]));
        let prompt = read_str(Path::new(task["dispatch_prompt_path"].as_str().unwrap()));
        assert!(!prompt.contains("holdout/secret.txt"));
    }

    let recorded_roots: std::collections::HashSet<_> = groups[1]["envs"]
        .as_array()
        .unwrap()
        .iter()
        .map(|env| env["dir"].as_str().unwrap())
        .collect();
    assert_eq!(recorded_roots, roots);
}

#[test]
fn missing_setup_source_fails_before_staging() {
    let tmp = tempfile::TempDir::new().unwrap();
    let evals = r#"{ "skill_name": "mr-review", "evals": [
        { "id": "held-out", "prompt": "p", "expected_output": "o",
          "assertions": [{ "id": "secret-test", "type": "command_check",
            "setup_files": ["holdout/missing.txt"], "command": "true" }] }
    ] }"#;
    let (skill_dir, cwd) = setup(tmp.path(), evals);

    skill_eval()
        .current_dir(&cwd)
        .args(["run", "--skill-dir"])
        .arg(&skill_dir)
        .args(["--skill", "mr-review", "--mode", "new-skill", "--dry-run"])
        .assert()
        .failure()
        .stderr(contains("command-check setup file not found"))
        .stderr(contains("holdout/missing.txt"));
    assert!(
        !iteration_dir(&cwd).exists(),
        "preflight failure must happen before staging"
    );
}

#[test]
fn descriptor_without_transcript_support_runs_command_checks() {
    let tmp = tempfile::TempDir::new().unwrap();
    let evals = json!({
        "skill_name": "mr-review",
        "evals": [{
            "id": "held-out",
            "prompt": "do the task",
            "expected_output": "passes held-out check",
            "skill_should_trigger": false,
            "assertions": [{
                "id": "held-out-check",
                "type": "command_check",
                "setup_files": ["holdout/secret.txt"],
                "command": setup_exists_command()
            }]
        }]
    });
    let (skill_dir, cwd) = setup(tmp.path(), &serde_json::to_string(&evals).unwrap());
    fs::create_dir_all(skill_dir.join("mr-review/evals/holdout")).unwrap();
    fs::write(
        skill_dir.join("mr-review/evals/holdout/secret.txt"),
        "secret",
    )
    .unwrap();
    write_project_descriptor(&cwd);

    skill_eval()
        .current_dir(&cwd)
        .args(["run", "--skill-dir"])
        .arg(&skill_dir)
        .args([
            "--skill",
            "mr-review",
            "--mode",
            "new-skill",
            "--harness",
            "cool-custom-harness",
        ])
        .assert()
        .success();
    let tasks = dispatch_tasks(&cwd);
    for task in &tasks {
        let eval_root = resolve(&cwd, task["eval_root"].as_str().unwrap());
        assert!(!eval_root.join("holdout/secret.txt").exists());
        let outputs = resolve(&cwd, task["outputs_dir"].as_str().unwrap());
        fs::create_dir_all(&outputs).unwrap();
        fs::write(outputs.join("final-message.md"), "done").unwrap();
    }

    skill_eval()
        .current_dir(&cwd)
        .args(["ingest", "--skill-dir"])
        .arg(&skill_dir)
        .args([
            "--skill",
            "mr-review",
            "--harness",
            "cool-custom-harness",
            "--iteration",
            "1",
        ])
        .assert()
        .success()
        .stderr(contains("no transcript parser"));

    for condition in ["with_skill", "without_skill"] {
        let result = read_json(
            &iteration_dir(&cwd)
                .join("eval-held-out")
                .join(condition)
                .join("command-checks/held-out-check.json"),
        );
        assert_eq!(result["passed"], true);
    }
    assert_eq!(
        read_json(&iteration_dir(&cwd).join("judge-tasks.json"))["total_tasks"],
        0
    );
}

#[test]
fn ingests_without_transcripts_while_guard_is_armed_and_aggregates() {
    let tmp = tempfile::TempDir::new().unwrap();
    let evals = json!({
        "skill_name": "mr-review",
        "evals": [{
            "id": "held-out",
            "prompt": "write answer.txt",
            "expected_output": "the answer is correct",
            "skill_should_trigger": false,
            "assertions": [{
                "id": "answer-correct",
                "type": "command_check",
                "setup_files": ["holdout/expected.txt"],
                "command": held_out_compare_command()
            }]
        }]
    });
    let (skill_dir, cwd) = setup(tmp.path(), &serde_json::to_string(&evals).unwrap());
    fs::create_dir_all(skill_dir.join("mr-review/evals/holdout")).unwrap();
    fs::write(
        skill_dir.join("mr-review/evals/holdout/expected.txt"),
        "correct",
    )
    .unwrap();

    skill_eval()
        .current_dir(&cwd)
        .args(["run", "--skill-dir"])
        .arg(&skill_dir)
        .args([
            "--skill",
            "mr-review",
            "--mode",
            "new-skill",
            "--harness",
            "claude-code",
            "--guard",
        ])
        .assert()
        .success();

    let dispatch = read_json(&iteration_dir(&cwd).join("dispatch.json"));
    for task in dispatch["tasks"].as_array().unwrap() {
        let eval_root = Path::new(task["eval_root"].as_str().unwrap());
        assert!(!eval_root.join("holdout/expected.txt").exists());
        assert!(
            eval_root
                .join(".claude/skills/.slow-powers-eval-guard.json")
                .exists(),
            "guard is armed while command checks later run"
        );
        fs::write(
            eval_root.join("answer.txt"),
            if task["condition"] == "with_skill" {
                "correct"
            } else {
                "wrong"
            },
        )
        .unwrap();
        let outputs_dir = Path::new(task["outputs_dir"].as_str().unwrap());
        fs::create_dir_all(outputs_dir).unwrap();
        fs::write(outputs_dir.join("final-message.md"), "done").unwrap();
    }

    skill_eval()
        .current_dir(&cwd)
        .args(["ingest", "--skill-dir"])
        .arg(&skill_dir)
        .args([
            "--skill",
            "mr-review",
            "--iteration",
            "1",
            "--harness",
            "claude-code",
        ])
        .assert()
        .success();

    for condition in ["with_skill", "without_skill"] {
        let result = read_json(
            &iteration_dir(&cwd)
                .join("eval-held-out")
                .join(condition)
                .join("command-checks/answer-correct.json"),
        );
        assert_eq!(result["passed"], condition == "with_skill");
        let task = dispatch["tasks"]
            .as_array()
            .unwrap()
            .iter()
            .find(|task| task["condition"] == condition)
            .unwrap();
        assert!(
            Path::new(task["eval_root"].as_str().unwrap())
                .join("holdout/expected.txt")
                .exists(),
            "setup is injected only once ingest grades the task"
        );
    }

    let with_task = dispatch["tasks"]
        .as_array()
        .unwrap()
        .iter()
        .find(|task| task["condition"] == "with_skill")
        .unwrap();
    let with_root = Path::new(with_task["eval_root"].as_str().unwrap());
    let with_result_path =
        iteration_dir(&cwd).join("eval-held-out/with_skill/command-checks/answer-correct.json");
    fs::write(with_root.join("answer.txt"), "wrong").unwrap();
    skill_eval()
        .current_dir(&cwd)
        .args(["grade", "--skill-dir"])
        .arg(&skill_dir)
        .args([
            "--skill",
            "mr-review",
            "--iteration",
            "1",
            "--harness",
            "claude-code",
        ])
        .assert()
        .success()
        .stdout(contains("0 executed, 2 reused"));
    assert_eq!(read_json(&with_result_path)["passed"], true);

    skill_eval()
        .current_dir(&cwd)
        .args(["grade", "--skill-dir"])
        .arg(&skill_dir)
        .args([
            "--skill",
            "mr-review",
            "--iteration",
            "1",
            "--harness",
            "claude-code",
            "--overwrite",
        ])
        .assert()
        .success()
        .stdout(contains("2 executed, 0 reused"));
    assert_eq!(read_json(&with_result_path)["passed"], false);

    fs::write(with_root.join("answer.txt"), "correct").unwrap();
    skill_eval()
        .current_dir(&cwd)
        .args(["grade", "--skill-dir"])
        .arg(&skill_dir)
        .args([
            "--skill",
            "mr-review",
            "--iteration",
            "1",
            "--harness",
            "claude-code",
            "--overwrite",
        ])
        .assert()
        .success();
    assert_eq!(read_json(&with_result_path)["passed"], true);
    assert_eq!(
        read_json(&iteration_dir(&cwd).join("judge-tasks.json"))["total_tasks"],
        0
    );

    skill_eval()
        .current_dir(&cwd)
        .args(["finalize", "--skill-dir"])
        .arg(&skill_dir)
        .args([
            "--skill",
            "mr-review",
            "--iteration",
            "1",
            "--harness",
            "claude-code",
        ])
        .assert()
        .success()
        .stdout(contains("Guard still armed"));

    let benchmark = read_json(&iteration_dir(&cwd).join("benchmark.json"));
    assert_eq!(
        benchmark["run_summary"]["with_skill"]["pass_rate"]["mean"],
        1.0
    );
    assert_eq!(
        benchmark["run_summary"]["without_skill"]["pass_rate"]["mean"],
        0.0
    );
}

#[cfg(unix)]
mod matrix;