eval-magic 0.5.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//! Stage 3 — `detect-stray-writes`.
//!
//! Classifies a run's tool
//! invocations against its allowed outputs dir:
//!
//! - **violations**: file-write tools (per the adapters' cross-harness
//!   vocabulary union) whose target path resolves outside the outputs dir.
//! - **warnings**: shell commands matching a mutating pattern that don't
//!   reference the outputs dir (via the sandbox `classify_bash` policy).
//! - **live_source_reads**: read tools / shell commands that touched the live
//!   skill-under-test directory instead of its staged copy.

use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::adapters::{all_config_dir_names, all_tool_vocabulary};
use crate::core::{ConditionsRecord, RunRecord, ToolInvocation};
use crate::pipeline::error::PipelineError;
use crate::pipeline::io::{now_iso8601, write_json};
use crate::pipeline::slots::{run_key, run_slots};
use crate::sandbox::{classify_bash, is_shell_tool, is_under, is_write_tool, path_arg};
use crate::validation::{SchemaName, validate_against_schema};

/// A read-only tool carrying a target path argument, in any harness's
/// vocabulary.
fn is_read_tool(name: &str) -> bool {
    all_tool_vocabulary().read_tools.iter().any(|t| t == name)
}

const LIVE_SOURCE_REASON: &str =
    "reads the live skill source instead of its staged copy — the arm may be contaminated";

/// One flagged tool invocation. `path` is set for write/read findings, `command`
/// for shell findings; the unused one is omitted (the schema forbids extras).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StrayFinding {
    pub tool: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub command: Option<String>,
    pub ordinal: u32,
    pub reason: String,
}

/// The stray-write classification for one run.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct RunFindings {
    pub violations: Vec<StrayFinding>,
    pub warnings: Vec<StrayFinding>,
}

/// The `command` arg of a shell invocation, or `""` when absent.
fn command_of(inv: &ToolInvocation) -> &str {
    inv.args
        .as_ref()
        .and_then(|a| a.get("command"))
        .and_then(|v| v.as_str())
        .unwrap_or("")
}

/// Classify a run's tool invocations against its allowed outputs dir. See the
/// module docs for what counts as a violation vs. a warning.
pub fn detect_stray_writes(
    invocations: &[ToolInvocation],
    outputs_dir: &str,
    repo_root: &Path,
) -> RunFindings {
    let mut findings = RunFindings::default();

    for inv in invocations {
        if is_write_tool(&inv.name) {
            if let Some(p) = inv.args.as_ref().and_then(path_arg)
                && !is_under(p, outputs_dir, repo_root)
            {
                findings.violations.push(StrayFinding {
                    tool: inv.name.clone(),
                    path: Some(p.to_string()),
                    command: None,
                    ordinal: inv.ordinal,
                    reason: "writes outside the run's outputs dir".to_string(),
                });
            }
            continue;
        }

        if is_shell_tool(&inv.name) {
            let command = command_of(inv);
            if let Some(reason) =
                classify_bash(command, std::slice::from_ref(&outputs_dir.to_string()))
            {
                findings.warnings.push(StrayFinding {
                    tool: inv.name.clone(),
                    path: None,
                    command: Some(command.to_string()),
                    ordinal: inv.ordinal,
                    reason: reason.to_string(),
                });
            }
        }
    }

    findings
}

/// Lexically absolutize a path (no disk access). Mirrors node's `resolve()`.
fn absolutize(p: &Path) -> PathBuf {
    std::path::absolute(p).unwrap_or_else(|_| p.to_path_buf())
}

/// Node-style lexical `path.relative(from, to)` over absolute, normalized paths.
/// Returns forward-slash-joined components; starts with `..` when `to` is not
/// under `from`.
fn path_relative(from: &Path, to: &Path) -> String {
    let from_comps: Vec<_> = from.components().collect();
    let to_comps: Vec<_> = to.components().collect();
    let mut i = 0;
    while i < from_comps.len() && i < to_comps.len() && from_comps[i] == to_comps[i] {
        i += 1;
    }
    let mut parts: Vec<String> = vec!["..".to_string(); from_comps.len() - i];
    for c in &to_comps[i..] {
        parts.push(c.as_os_str().to_string_lossy().into_owned());
    }
    parts.join("/")
}

/// Leading boundary before a bare `rel` reference: start-of-string or one of
/// `\s'"=:(/`.
fn is_leading_boundary(b: u8) -> bool {
    b.is_ascii_whitespace() || matches!(b, b'\'' | b'"' | b'=' | b':' | b'(' | b'/')
}

/// Trailing boundary after a bare `rel` reference: end-of-string or one of
/// `/\s'")`.
fn is_trailing_boundary(b: u8) -> bool {
    b == b'/' || b.is_ascii_whitespace() || matches!(b, b'\'' | b'"' | b')')
}

/// True if `command` references `rel` as a bare path token — bounded as a path
/// segment and **not** prefixed by any harness config dir (`config_dirs`, the
/// caller-supplied `adapters::all_config_dir_names()` list). The `regex` crate
/// has no lookbehind, so each occurrence is scanned directly for the boundary +
/// preceding-segment conditions.
fn references_bare_rel(command: &str, rel: &str, config_dirs: &[String]) -> bool {
    if rel.is_empty() {
        return false;
    }
    let bytes = command.as_bytes();
    let mut search_from = 0;
    while let Some(off) = command[search_from..].find(rel) {
        let start = search_from + off;
        let end = start + rel.len();

        let leading_ok = start == 0 || is_leading_boundary(bytes[start - 1]);
        // The lookbehind sits before the boundary char: the text up to (but not
        // including) that char must not end with a staging-dir prefix.
        let lookbehind_ok = start == 0 || {
            let before = &command[..start - 1];
            !config_dirs.iter().any(|dir| before.ends_with(dir.as_str()))
        };
        let trailing_ok = end == command.len() || is_trailing_boundary(bytes[end]);

        if leading_ok && lookbehind_ok && trailing_ok {
            return true;
        }
        search_from = start + 1;
    }
    false
}

/// Flag tool invocations that read the **live** skill-under-test directory
/// instead of the staged copy. Reads are detected, not blocked, so this surfaces
/// post-hoc as a validity warning. See `detect-stray-writes.ts` for the rationale.
pub fn detect_live_source_reads(
    invocations: &[ToolInvocation],
    live_skill_dir: &Path,
    repo_root: &Path,
) -> Vec<StrayFinding> {
    let mut findings = Vec::new();
    let live_dir = absolutize(live_skill_dir);
    let live_dir_str = live_dir.to_string_lossy();
    let rel = path_relative(repo_root, &live_dir);
    let rel_usable = !rel.starts_with("..");
    let config_dirs = all_config_dir_names();

    for inv in invocations {
        if is_read_tool(&inv.name) {
            if let Some(p) = inv.args.as_ref().and_then(path_arg)
                && is_under(p, &live_dir_str, repo_root)
            {
                findings.push(StrayFinding {
                    tool: inv.name.clone(),
                    path: Some(p.to_string()),
                    command: None,
                    ordinal: inv.ordinal,
                    reason: LIVE_SOURCE_REASON.to_string(),
                });
            }
            continue;
        }

        if is_shell_tool(&inv.name) {
            let command = command_of(inv);
            if command.contains(live_dir_str.as_ref())
                || (rel_usable && references_bare_rel(command, &rel, &config_dirs))
            {
                findings.push(StrayFinding {
                    tool: inv.name.clone(),
                    path: None,
                    command: Some(command.to_string()),
                    ordinal: inv.ordinal,
                    reason: LIVE_SOURCE_REASON.to_string(),
                });
            }
        }
    }

    findings
}

// --- CLI report ---

/// Per-(eval, condition, run) findings, emitted only for runs with ≥1 finding.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RunReport {
    pub eval_id: String,
    pub condition: String,
    /// 1-based run index within a multi-run cell; absent for single-run cells.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub run_index: Option<u32>,
    pub violations: Vec<StrayFinding>,
    pub warnings: Vec<StrayFinding>,
    pub live_source_reads: Vec<StrayFinding>,
}

/// Aggregate counts across all runs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct Totals {
    pub violations: usize,
    pub warnings: usize,
    pub live_source_reads: usize,
}

/// The full `stray-writes.json` report.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StrayWritesReport {
    pub generated: String,
    pub iteration: u32,
    pub totals: Totals,
    pub runs: Vec<RunReport>,
    /// How many transcript tool-calls were actually examined across every run.
    /// Zero means nothing was inspected — a clean `totals` is then *unverifiable*,
    /// not a pass. In-memory only; never serialized into `stray-writes.json`.
    #[serde(skip)]
    pub invocations_inspected: usize,
}

/// `dispatch.json` fields the report builder reads (outputs-dir override).
#[derive(Debug, Deserialize)]
struct DispatchEnvelope {
    tasks: Option<Vec<DispatchRef>>,
}

#[derive(Debug, Deserialize)]
struct DispatchRef {
    eval_id: String,
    condition: String,
    #[serde(default)]
    run_index: Option<u32>,
    #[serde(default)]
    outputs_dir: Option<String>,
}

/// Build, validate, and write `<iteration_dir>/stray-writes.json` for every
/// `run.json` in the iteration. `repo_root` is the runner's cwd (relative paths
/// resolve against it); `live_skill_dir` is the skill-under-test source.
pub fn detect_stray_writes_report(
    iteration_dir: &Path,
    iteration: u32,
    live_skill_dir: &Path,
    repo_root: &Path,
) -> Result<StrayWritesReport, PipelineError> {
    let conditions_path = iteration_dir.join("conditions.json");
    if !conditions_path.exists() {
        return Err(PipelineError::Message(format!(
            "missing: {}",
            conditions_path.display()
        )));
    }
    let conditions: ConditionsRecord =
        serde_json::from_str(&std::fs::read_to_string(&conditions_path)?)?;
    let condition_names: Vec<String> = conditions
        .conditions
        .iter()
        .map(|c| c.name.clone())
        .collect();

    let outputs_by_key = outputs_dirs_by_key(iteration_dir);

    let mut runs = Vec::new();
    let mut totals = Totals {
        violations: 0,
        warnings: 0,
        live_source_reads: 0,
    };
    let mut invocations_inspected = 0usize;

    let mut eval_dirs: Vec<String> = std::fs::read_dir(iteration_dir)?
        .flatten()
        .filter_map(|e| {
            let name = e.file_name().to_string_lossy().into_owned();
            name.starts_with("eval-").then_some(name)
        })
        .collect();
    eval_dirs.sort();

    for dir_name in &eval_dirs {
        let eval_id = dir_name.strip_prefix("eval-").unwrap_or(dir_name);
        for cond in &condition_names {
            let cond_dir = iteration_dir.join(dir_name).join(cond);
            for slot in run_slots(&cond_dir) {
                let run_path = slot.dir.join("run.json");
                if !run_path.exists() {
                    continue;
                }
                let source = run_path.to_string_lossy();
                let run: RunRecord = validate_against_schema(
                    SchemaName::RunRecord,
                    &serde_json::from_str(&std::fs::read_to_string(&run_path)?)?,
                    &source,
                )?;

                let outputs_dir = outputs_by_key.get(&run_key(eval_id, cond, slot.run_index));

                invocations_inspected += run.tool_invocations.len();
                // `dispatch.json` is the authoritative source of the outputs
                // boundary: an absolute path into the isolated env
                // (`env/.eval-magic-outputs/...`). Without it we cannot honor the
                // outputs-only contract, so we skip out-of-bounds *write*
                // classification for that run rather than guess a boundary — the old
                // `<slot>/outputs` convention no longer matches where agents write and
                // would mis-flag every legitimate write. Live-source-read detection is
                // independent of the boundary and still runs.
                let findings = match outputs_dir {
                    Some(dir) => detect_stray_writes(&run.tool_invocations, dir, repo_root),
                    None => {
                        let run_label = slot
                            .run_index
                            .map(|k| format!(" run-{k}"))
                            .unwrap_or_default();
                        eprintln!(
                            "{eval_id}/{cond}{run_label}: no outputs_dir in dispatch.json — \
                             skipping out-of-bounds write classification (boundary unknown)"
                        );
                        RunFindings::default()
                    }
                };
                let live_reads =
                    detect_live_source_reads(&run.tool_invocations, live_skill_dir, repo_root);

                totals.violations += findings.violations.len();
                totals.warnings += findings.warnings.len();
                totals.live_source_reads += live_reads.len();

                if !findings.violations.is_empty()
                    || !findings.warnings.is_empty()
                    || !live_reads.is_empty()
                {
                    runs.push(RunReport {
                        eval_id: eval_id.to_string(),
                        condition: cond.clone(),
                        run_index: slot.run_index,
                        violations: findings.violations,
                        warnings: findings.warnings,
                        live_source_reads: live_reads,
                    });
                }
            }
        }
    }

    let report = StrayWritesReport {
        generated: now_iso8601(),
        iteration,
        totals,
        runs,
        invocations_inspected,
    };

    let out_path = iteration_dir.join("stray-writes.json");
    validate_against_schema::<serde_json::Value>(
        SchemaName::StrayWrites,
        &serde_json::to_value(&report)?,
        &out_path.to_string_lossy(),
    )?;
    write_json(&out_path, &report)?;

    Ok(report)
}

/// Map `"<eval_id>:<condition>[:r<k>]"` → the task's `outputs_dir` from
/// `dispatch.json`. Empty when the file is absent or malformed (callers fall
/// back to convention).
fn outputs_dirs_by_key(iteration_dir: &Path) -> std::collections::HashMap<String, String> {
    let mut out = std::collections::HashMap::new();
    if let Ok(raw) = std::fs::read_to_string(iteration_dir.join("dispatch.json"))
        && let Ok(env) = serde_json::from_str::<DispatchEnvelope>(&raw)
    {
        for t in env.tasks.unwrap_or_default() {
            if let Some(dir) = t.outputs_dir {
                out.insert(run_key(&t.eval_id, &t.condition, t.run_index), dir);
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    const OUTPUTS: &str = "/work/iteration-1/eval-x/with_skill/outputs";
    const REPO: &str = "/work/repo";
    const LIVE_SKILL: &str = "/work/repo/skills/mr-review";

    /// Build a minimal invocation from name/args/ordinal (result is unused here).
    fn inv(name: &str, args: serde_json::Value, ordinal: u32) -> ToolInvocation {
        ToolInvocation {
            name: name.to_string(),
            args: Some(args),
            result: None,
            ordinal,
        }
    }

    fn repo() -> &'static Path {
        Path::new(REPO)
    }

    fn live() -> &'static Path {
        Path::new(LIVE_SKILL)
    }

    // --- detectStrayWrites ---

    #[test]
    fn a_write_inside_outputs_is_clean() {
        let f = detect_stray_writes(
            &[inv(
                "Write",
                json!({"file_path": format!("{OUTPUTS}/answer.md")}),
                0,
            )],
            OUTPUTS,
            repo(),
        );
        assert!(f.violations.is_empty());
        assert!(f.warnings.is_empty());
    }

    #[test]
    fn a_write_outside_outputs_is_a_violation() {
        let f = detect_stray_writes(
            &[inv(
                "Write",
                json!({"file_path": format!("{REPO}/runner/run.ts")}),
                2,
            )],
            OUTPUTS,
            repo(),
        );
        assert_eq!(f.violations.len(), 1);
        assert_eq!(f.violations[0].tool, "Write");
        assert_eq!(
            f.violations[0].path.as_deref(),
            Some(&*format!("{REPO}/runner/run.ts"))
        );
        assert_eq!(f.violations[0].ordinal, 2);
    }

    #[test]
    fn edit_multiedit_notebookedit_outside_outputs_is_a_violation() {
        let f = detect_stray_writes(
            &[
                inv("Edit", json!({"file_path": "/etc/hosts"}), 0),
                inv("NotebookEdit", json!({"notebook_path": "/tmp/x.ipynb"}), 1),
            ],
            OUTPUTS,
            repo(),
        );
        let mut tools: Vec<&str> = f.violations.iter().map(|v| v.tool.as_str()).collect();
        tools.sort();
        assert_eq!(tools, vec!["Edit", "NotebookEdit"]);
    }

    #[test]
    fn an_install_command_is_a_warning() {
        let f = detect_stray_writes(
            &[inv("Bash", json!({"command": "npm install left-pad"}), 0)],
            OUTPUTS,
            repo(),
        );
        assert_eq!(f.warnings.len(), 1);
        assert_eq!(f.warnings[0].tool, "Bash");
        assert!(f.warnings[0].reason.to_lowercase().contains("install"));
    }

    #[test]
    fn a_codex_command_execution_install_is_a_warning() {
        let f = detect_stray_writes(
            &[inv(
                "command_execution",
                json!({"command": "npm install left-pad"}),
                0,
            )],
            OUTPUTS,
            repo(),
        );
        assert_eq!(f.warnings.len(), 1);
        assert_eq!(f.warnings[0].tool, "command_execution");
        assert!(f.warnings[0].reason.to_lowercase().contains("install"));
    }

    #[test]
    fn a_codex_file_change_outside_outputs_is_a_violation() {
        let f = detect_stray_writes(
            &[inv(
                "file_change",
                json!({"path": format!("{REPO}/src/app.ts")}),
                4,
            )],
            OUTPUTS,
            repo(),
        );
        assert_eq!(f.violations.len(), 1);
        assert_eq!(f.violations[0].tool, "file_change");
        assert_eq!(
            f.violations[0].path.as_deref(),
            Some(&*format!("{REPO}/src/app.ts"))
        );
        assert_eq!(f.violations[0].ordinal, 4);
    }

    #[test]
    fn a_mutating_bash_scoped_to_outputs_is_not_flagged() {
        let f = detect_stray_writes(
            &[inv(
                "Bash",
                json!({"command": format!("echo hi > {OUTPUTS}/log.txt")}),
                0,
            )],
            OUTPUTS,
            repo(),
        );
        assert!(f.warnings.is_empty());
    }

    #[test]
    fn git_worktree_add_is_a_warning() {
        let f = detect_stray_writes(
            &[inv(
                "Bash",
                json!({"command": "git worktree add ../wt -b scratch"}),
                0,
            )],
            OUTPUTS,
            repo(),
        );
        assert_eq!(f.warnings.len(), 1);
        assert!(f.warnings[0].reason.to_lowercase().contains("worktree"));
    }

    #[test]
    fn creating_a_path_under_dot_claude_is_a_warning() {
        let f = detect_stray_writes(
            &[inv("Bash", json!({"command": "mkdir -p .claude/foo"}), 0)],
            OUTPUTS,
            repo(),
        );
        assert_eq!(f.warnings.len(), 1);
        assert!(f.warnings[0].reason.to_lowercase().contains("config dir"));
    }

    #[test]
    fn creating_a_path_under_dot_codex_is_a_warning() {
        let f = detect_stray_writes(
            &[inv(
                "Bash",
                json!({"command": "cp evil.json .codex/hooks.json"}),
                0,
            )],
            OUTPUTS,
            repo(),
        );
        assert_eq!(f.warnings.len(), 1);
        assert!(f.warnings[0].reason.to_lowercase().contains("config dir"));
    }

    #[test]
    fn read_only_tools_are_never_flagged() {
        let f = detect_stray_writes(
            &[
                inv("Read", json!({"file_path": "/anywhere"}), 0),
                inv("Grep", json!({"pattern": "x"}), 1),
                inv("Bash", json!({"command": "ls -la /"}), 2),
            ],
            OUTPUTS,
            repo(),
        );
        assert!(f.violations.is_empty());
        assert!(f.warnings.is_empty());
    }

    // --- detectLiveSourceReads ---

    #[test]
    fn a_read_of_the_live_skill_md_is_flagged() {
        let f = detect_live_source_reads(
            &[inv(
                "Read",
                json!({"file_path": format!("{LIVE_SKILL}/SKILL.md")}),
                1,
            )],
            live(),
            repo(),
        );
        assert_eq!(f.len(), 1);
        assert_eq!(f[0].tool, "Read");
        assert_eq!(
            f[0].path.as_deref(),
            Some(&*format!("{LIVE_SKILL}/SKILL.md"))
        );
        assert_eq!(f[0].ordinal, 1);
        assert!(f[0].reason.to_lowercase().contains("live skill source"));
    }

    #[test]
    fn a_read_of_a_staged_eval_copy_is_not_flagged() {
        let f = detect_live_source_reads(
            &[inv(
                "Read",
                json!({"file_path": format!("{REPO}/.claude/skills/slow-powers-eval-1-old_skill__mr-review/SKILL.md")}),
                0,
            )],
            live(),
            repo(),
        );
        assert!(f.is_empty());
    }

    #[test]
    fn a_relative_read_resolving_under_the_live_dir_is_flagged() {
        let f = detect_live_source_reads(
            &[inv(
                "Read",
                json!({"file_path": "skills/mr-review/SKILL.md"}),
                0,
            )],
            live(),
            repo(),
        );
        assert_eq!(f.len(), 1);
    }

    #[test]
    fn a_grep_scoped_to_the_live_dir_is_flagged() {
        let f = detect_live_source_reads(
            &[inv("Grep", json!({"pattern": "x", "path": LIVE_SKILL}), 2)],
            live(),
            repo(),
        );
        assert_eq!(f.len(), 1);
        assert_eq!(f[0].tool, "Grep");
    }

    #[test]
    fn a_bash_referencing_the_live_dir_relatively_is_flagged() {
        let f = detect_live_source_reads(
            &[inv(
                "Bash",
                json!({"command": "cat skills/mr-review/SKILL.md"}),
                3,
            )],
            live(),
            repo(),
        );
        assert_eq!(f.len(), 1);
        assert_eq!(f[0].tool, "Bash");
        assert_eq!(
            f[0].command.as_deref(),
            Some("cat skills/mr-review/SKILL.md")
        );
    }

    #[test]
    fn a_codex_command_referencing_the_live_dir_relatively_is_flagged() {
        let f = detect_live_source_reads(
            &[inv(
                "command_execution",
                json!({"command": "cat skills/mr-review/SKILL.md"}),
                3,
            )],
            live(),
            repo(),
        );
        assert_eq!(f.len(), 1);
        assert_eq!(f[0].tool, "command_execution");
        assert_eq!(
            f[0].command.as_deref(),
            Some("cat skills/mr-review/SKILL.md")
        );
    }

    #[test]
    fn a_bash_referencing_the_live_dir_absolutely_is_flagged() {
        let f = detect_live_source_reads(
            &[inv(
                "Bash",
                json!({"command": format!("grep -r trigger {LIVE_SKILL}/")}),
                0,
            )],
            live(),
            repo(),
        );
        assert_eq!(f.len(), 1);
    }

    #[test]
    fn a_bash_referencing_a_staged_copy_under_dot_claude_skills_is_not_flagged() {
        let f = detect_live_source_reads(
            &[inv(
                "Bash",
                json!({"command": "cat .claude/skills/mr-review/SKILL.md"}),
                0,
            )],
            live(),
            repo(),
        );
        assert!(f.is_empty());
    }

    #[test]
    fn a_bash_referencing_a_staged_copy_under_dot_agents_skills_is_not_flagged() {
        let f = detect_live_source_reads(
            &[inv(
                "Bash",
                json!({"command": "cat .agents/skills/mr-review/SKILL.md"}),
                0,
            )],
            live(),
            repo(),
        );
        assert!(f.is_empty());
    }

    #[test]
    fn a_bash_referencing_a_staged_copy_under_dot_opencode_skills_is_not_flagged() {
        let f = detect_live_source_reads(
            &[inv(
                "Bash",
                json!({"command": "cat .opencode/skills/mr-review/SKILL.md"}),
                0,
            )],
            live(),
            repo(),
        );
        assert!(f.is_empty());
    }

    #[test]
    fn unrelated_reads_and_commands_are_not_flagged() {
        let f = detect_live_source_reads(
            &[
                inv("Read", json!({"file_path": format!("{OUTPUTS}/x.md")}), 0),
                inv("Bash", json!({"command": "ls .eval-magic"}), 1),
                // Write tools are detect_stray_writes' jurisdiction — reads only here.
                inv(
                    "Write",
                    json!({"file_path": format!("{LIVE_SKILL}/SKILL.md")}),
                    2,
                ),
            ],
            live(),
            repo(),
        );
        assert!(f.is_empty());
    }
}