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
//! The hidden `guard` PreToolUse hook entry point and `teardown-guard`.

use crate::helpers::skill_eval;
use predicates::prelude::*;
use predicates::str::contains;
use std::fs;
use tempfile::TempDir;

/// The internal `guard` hook entry point is hidden from `--help` (its unique
/// description never appears) yet remains callable.
#[test]
fn guard_subcommand_is_hidden_but_callable() {
    skill_eval()
        .arg("--help")
        .assert()
        .success()
        .stdout(contains("PreToolUse hook entry point").not());

    skill_eval().arg("guard").arg("--help").assert().success();
    skill_eval()
        .args(["guard-hook", "--help"])
        .assert()
        .success();
}

/// Write an armed guard marker scoping writes to `<allowed>`, and return its path.
fn write_armed_marker(root: &std::path::Path, allowed: &std::path::Path) -> std::path::PathBuf {
    let skills = root.join(".claude").join("skills");
    fs::create_dir_all(&skills).unwrap();
    let marker = skills.join(".slow-powers-eval-guard.json");
    fs::write(
        &marker,
        format!(
            r#"{{ "active": true, "allowedRoots": ["{}"], "expiresAt": "2999-01-01T00:00:00.000Z" }}"#,
            allowed.display()
        ),
    )
    .unwrap();
    marker
}

fn write_codex_armed_marker(
    root: &std::path::Path,
    allowed: &std::path::Path,
) -> std::path::PathBuf {
    let skills = root.join(".agents").join("skills");
    fs::create_dir_all(&skills).unwrap();
    let marker = skills.join(".slow-powers-eval-guard.json");
    fs::write(
        &marker,
        format!(
            r#"{{ "active": true, "allowedRoots": ["{}"], "expiresAt": "2999-01-01T00:00:00.000Z" }}"#,
            allowed.display()
        ),
    )
    .unwrap();
    marker
}

/// `guard` denies a Write outside the sandbox: it prints a PreToolUse deny verdict
/// on stdout and still exits 0 (the hook must never fail the session).
#[test]
fn guard_denies_out_of_bounds_write() {
    let tmp = TempDir::new().unwrap();
    let marker = write_armed_marker(tmp.path(), &tmp.path().join(".eval-magic"));

    skill_eval()
        .arg("guard")
        .arg(&marker)
        .write_stdin(r#"{ "tool_name": "Write", "tool_input": { "file_path": "/etc/passwd" } }"#)
        .assert()
        .success()
        .stdout(contains(r#""permissionDecision":"deny""#));
}

/// `guard` allows an in-bounds write: empty stdout, exit 0.
#[test]
fn guard_allows_in_bounds_write() {
    let tmp = TempDir::new().unwrap();
    let workspace = tmp.path().join(".eval-magic");
    let marker = write_armed_marker(tmp.path(), &workspace);

    skill_eval()
        .arg("guard")
        .arg(&marker)
        .write_stdin(format!(
            r#"{{ "tool_name": "Write", "tool_input": {{ "file_path": "{}/out.md" }} }}"#,
            workspace.display()
        ))
        .assert()
        .success()
        .stdout("");
}

#[test]
fn guard_codex_subcommand_blocks_with_codex_verdict_shape() {
    let tmp = TempDir::new().unwrap();
    let marker = write_codex_armed_marker(tmp.path(), &tmp.path().join(".eval-magic"));

    skill_eval()
        .arg("guard-codex")
        .arg(&marker)
        .write_stdin(
            r#"{ "tool_name": "Bash", "tool_input": { "command": "npm install left-pad" } }"#,
        )
        .assert()
        .success()
        .stdout(contains(r#""decision":"block""#))
        .stdout(contains("blocked Bash"));
}

/// Byte-pin of the Claude deny verdict: armed hooks from previous releases keep
/// reading this exact shape, so the serialized bytes are a compatibility
/// contract — full-string equality, not substrings.
#[test]
fn guard_deny_verdict_bytes_are_stable() {
    let tmp = TempDir::new().unwrap();
    let marker = tmp.path().join("marker.json");
    fs::write(
        &marker,
        r#"{"active":true,"allowedRoots":["/work/env"],"expiresAt":"2999-01-01T00:00:00.000Z"}"#,
    )
    .unwrap();

    skill_eval()
        .arg("guard")
        .arg(&marker)
        .write_stdin(r#"{ "tool_name": "Write", "tool_input": { "file_path": "/etc/passwd" } }"#)
        .assert()
        .success()
        .stdout(
            "{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\
             \"permissionDecision\":\"deny\",\"permissionDecisionReason\":\
             \"eval guard: Write to /etc/passwd is outside the eval sandbox \
             (allowed: /work/env)\"}}",
        );
}

/// Byte-pin of the Codex block verdict — same compatibility contract as the
/// Claude pin above.
#[test]
fn guard_codex_block_verdict_bytes_are_stable() {
    let tmp = TempDir::new().unwrap();
    let marker = tmp.path().join("marker.json");
    fs::write(
        &marker,
        r#"{"active":true,"allowedRoots":["/work/env"],"expiresAt":"2999-01-01T00:00:00.000Z"}"#,
    )
    .unwrap();

    skill_eval()
        .arg("guard-codex")
        .arg(&marker)
        .write_stdin(
            r#"{ "tool_name": "Bash", "tool_input": { "command": "npm install left-pad" } }"#,
        )
        .assert()
        .success()
        .stdout(
            "{\"decision\":\"block\",\"reason\":\"eval guard: blocked Bash \
             (package install/add) — runs outside the eval sandbox\"}",
        );
}

/// `guard` fails open when the marker is absent: empty stdout, exit 0.
#[test]
fn guard_fails_open_without_marker() {
    let tmp = TempDir::new().unwrap();
    skill_eval()
        .arg("guard")
        .arg(tmp.path().join("nope.json"))
        .write_stdin(r#"{ "tool_name": "Write", "tool_input": { "file_path": "/etc/passwd" } }"#)
        .assert()
        .success()
        .stdout("");
}

/// The generic `guard-hook --harness <name>` entry point resolves the verdict
/// shape from the named harness's embedded descriptor — Claude's deny shape
/// for claude-code, Codex's block shape for codex. Future guard-capable
/// built-ins use this instead of minting another alias.
#[test]
fn guard_hook_resolves_the_harness_verdict_shape() {
    let tmp = TempDir::new().unwrap();
    let marker = write_armed_marker(tmp.path(), &tmp.path().join(".eval-magic"));

    skill_eval()
        .args(["guard-hook", "--harness", "claude-code"])
        .arg(&marker)
        .write_stdin(r#"{ "tool_name": "Write", "tool_input": { "file_path": "/etc/passwd" } }"#)
        .assert()
        .success()
        .stdout(contains(r#""permissionDecision":"deny""#));

    skill_eval()
        .args(["guard-hook", "--harness", "codex"])
        .arg(&marker)
        .write_stdin(
            r#"{ "tool_name": "Bash", "tool_input": { "command": "npm install left-pad" } }"#,
        )
        .assert()
        .success()
        .stdout(contains(r#""decision":"block""#));
}

/// `guard-hook` fails open on an unknown harness: empty stdout, exit 0 — the
/// hook must never brick a session, whatever config invoked it.
#[test]
fn guard_hook_fails_open_for_an_unknown_harness() {
    let tmp = TempDir::new().unwrap();
    let marker = write_armed_marker(tmp.path(), &tmp.path().join(".eval-magic"));

    skill_eval()
        .args(["guard-hook", "--harness", "mystery"])
        .arg(&marker)
        .write_stdin(r#"{ "tool_name": "Write", "tool_input": { "file_path": "/etc/passwd" } }"#)
        .assert()
        .success()
        .stdout("");
}

fn write_opencode_armed_marker(
    root: &std::path::Path,
    allowed: &std::path::Path,
) -> std::path::PathBuf {
    let skills = root.join(".opencode").join("skills");
    fs::create_dir_all(&skills).unwrap();
    let marker = skills.join(".slow-powers-eval-guard.json");
    fs::write(
        &marker,
        format!(
            r#"{{ "active": true, "allowedRoots": ["{}"], "expiresAt": "2999-01-01T00:00:00.000Z" }}"#,
            allowed.display()
        ),
    )
    .unwrap();
    marker
}

/// `guard-hook --harness opencode` round-trip, fed the exact payload shape
/// the staged plugin forwards: OpenCode's lowercase tool ids with camelCase
/// args. An out-of-bounds `write` blocks; an in-bounds one allows (empty
/// stdout).
#[test]
fn guard_hook_opencode_round_trips_write_verdicts() {
    let tmp = TempDir::new().unwrap();
    let workspace = tmp.path().join(".eval-magic");
    let marker = write_opencode_armed_marker(tmp.path(), &workspace);

    skill_eval()
        .args(["guard-hook", "--harness", "opencode"])
        .arg(&marker)
        .write_stdin(r#"{ "tool_name": "write", "tool_input": { "filePath": "/etc/passwd" } }"#)
        .assert()
        .success()
        .stdout(contains(r#""decision":"block""#));

    skill_eval()
        .args(["guard-hook", "--harness", "opencode"])
        .arg(&marker)
        .write_stdin(format!(
            r#"{{ "tool_name": "write", "tool_input": {{ "filePath": "{}/out.md" }} }}"#,
            workspace.display()
        ))
        .assert()
        .success()
        .stdout("");
}

/// The plugin file itself is protected: a bash call mutating anything under
/// `.opencode` trips the config-dir tamper rule.
#[test]
fn guard_hook_opencode_blocks_bash_tampering_with_the_plugin() {
    let tmp = TempDir::new().unwrap();
    let marker = write_opencode_armed_marker(tmp.path(), &tmp.path().join(".eval-magic"));

    skill_eval()
        .args(["guard-hook", "--harness", "opencode"])
        .arg(&marker)
        .write_stdin(
            r#"{ "tool_name": "bash", "tool_input": { "command": "touch .opencode/plugins/slow-powers-eval-guard.js" } }"#,
        )
        .assert()
        .success()
        .stdout(contains(r#""decision":"block""#));
}

/// Byte-pin of the OpenCode block verdict — same compatibility contract as
/// the Claude/Codex pins above: the staged plugin parses `decision`/`reason`
/// out of these exact bytes.
#[test]
fn guard_hook_opencode_block_verdict_bytes_are_stable() {
    let tmp = TempDir::new().unwrap();
    let marker = tmp.path().join("marker.json");
    fs::write(
        &marker,
        r#"{"active":true,"allowedRoots":["/work/env"],"expiresAt":"2999-01-01T00:00:00.000Z"}"#,
    )
    .unwrap();

    skill_eval()
        .args(["guard-hook", "--harness", "opencode"])
        .arg(&marker)
        .write_stdin(r#"{ "tool_name": "write", "tool_input": { "filePath": "/etc/passwd" } }"#)
        .assert()
        .success()
        .stdout(
            "{\"decision\":\"block\",\"reason\":\"eval guard: write to /etc/passwd is \
             outside the eval sandbox (allowed: /work/env)\"}",
        );
}

/// `teardown-guard` removes the staged plugin, sweeps the marker/manifest,
/// and prunes the plugins dir the install created.
#[test]
fn teardown_guard_removes_an_installed_opencode_plugin() {
    let tmp = TempDir::new().unwrap();
    let skills = tmp.path().join(".opencode").join("skills");
    fs::create_dir_all(&skills).unwrap();
    let marker = skills.join(".slow-powers-eval-guard.json");
    fs::write(
        &marker,
        r#"{ "active": true, "allowedRoots": [], "expiresAt": "2999-01-01T00:00:00.000Z" }"#,
    )
    .unwrap();
    let plugins = tmp.path().join(".opencode").join("plugins");
    fs::create_dir_all(&plugins).unwrap();
    let plugin = plugins.join("slow-powers-eval-guard.js");
    fs::write(&plugin, "// staged plugin\n").unwrap();
    fs::write(
        skills.join(".slow-powers-eval-guard-manifest.json"),
        format!(
            r#"{{ "created_at": "2026-07-23T00:00:00.000Z", "settings_path": "{}", "settings_existed": false, "settings_backup": null, "marker_path": "{}" }}"#,
            plugin.display(),
            marker.display()
        ),
    )
    .unwrap();

    skill_eval()
        .arg("teardown-guard")
        .current_dir(tmp.path())
        .assert()
        .success()
        .stdout(contains("Write guard removed"));

    assert!(!plugin.exists(), "the staged plugin is removed");
    assert!(!marker.exists(), "the marker is swept");
    assert!(
        !plugins.exists(),
        "the plugins dir created for the plugin alone is pruned"
    );
}

/// Like `guard`, the generic entry point must stay off layered descriptor
/// discovery: it fires per tool call and reads embedded descriptors only.
#[test]
fn guard_hook_generic_skips_descriptor_discovery() {
    let tmp = TempDir::new().unwrap();
    let marker = write_armed_marker(tmp.path(), &tmp.path().join(".eval-magic"));
    let harnesses = tmp.path().join(".eval-magic").join("harnesses");
    fs::create_dir_all(&harnesses).unwrap();
    fs::write(harnesses.join("broken.toml"), "label = ").unwrap();

    skill_eval()
        .args(["guard-hook", "--harness", "claude-code"])
        .arg(&marker)
        .current_dir(tmp.path())
        .write_stdin(r#"{ "tool_name": "Write", "tool_input": { "file_path": "/etc/passwd" } }"#)
        .assert()
        .success()
        .stdout(contains(r#""permissionDecision":"deny""#))
        .stderr(contains("skipping harness descriptor").not());
}

/// `teardown-guard` reports when no guard is installed (cwd has no marker).
#[test]
fn teardown_guard_reports_nothing_to_remove() {
    let tmp = TempDir::new().unwrap();
    skill_eval()
        .arg("teardown-guard")
        .current_dir(tmp.path())
        .assert()
        .success()
        .stdout(contains("nothing to remove"));
}

/// `teardown-guard` sweeps a stray marker in the cwd and reports removal.
#[test]
fn teardown_guard_removes_installed_guard() {
    let tmp = TempDir::new().unwrap();
    write_armed_marker(tmp.path(), &tmp.path().join(".eval-magic"));

    skill_eval()
        .arg("teardown-guard")
        .current_dir(tmp.path())
        .assert()
        .success()
        .stdout(contains("Write guard removed"));

    assert!(
        !tmp.path()
            .join(".claude/skills/.slow-powers-eval-guard.json")
            .exists()
    );
}

/// The guard hook fires on every PreToolUse in a dispatched session, so it
/// must not run layered descriptor discovery: a broken user descriptor in the
/// eval env must not spam a skip-warning (or any other noise) per tool call.
#[test]
fn guard_hook_skips_descriptor_discovery() {
    let tmp = TempDir::new().unwrap();
    let marker = write_armed_marker(tmp.path(), &tmp.path().join(".eval-magic"));
    let harnesses = tmp.path().join(".eval-magic").join("harnesses");
    fs::create_dir_all(&harnesses).unwrap();
    fs::write(harnesses.join("broken.toml"), "label = ").unwrap();

    skill_eval()
        .arg("guard")
        .arg(&marker)
        .current_dir(tmp.path())
        .write_stdin(r#"{ "tool_name": "Write", "tool_input": { "file_path": "/etc/passwd" } }"#)
        .assert()
        .success()
        .stdout(contains(r#""permissionDecision":"deny""#))
        .stderr(contains("skipping harness descriptor").not());
}