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
//! The harness descriptor registry — the resolution authority for harness
//! identifiers.
//!
//! Descriptor sources load into label-keyed entries, and every way the rest of
//! the crate names a harness funnels through them: [`Harness::resolve`] turns
//! a string into a validated handle (resolving `--harness` after parsing, and
//! behind the artifact `Deserialize`), [`Harness::known`] enumerates the
//! entries, and [`adapter_for`] serves each handle's [`HarnessAdapter`].

use std::path::Path;
use std::sync::{LazyLock, OnceLock};

use crate::core::Harness;

use super::descriptor::layers::{
    DescriptorSource, HarnessFileError, Layer, check_user_layer_restrictions, default_config_root,
    discover_sources, embedded_sources,
};
use super::descriptor::{
    DescriptorError, HarnessDescriptor, finalize_descriptor, merge_descriptor_value,
    parse_descriptor_value,
};
use super::descriptor_adapter::DescriptorAdapter;
use super::harness::{HarnessAdapter, ToolVocabulary};

/// One registry entry: a harness identity (the descriptor's `label`), the
/// descriptor sources that contributed to it (for `harness list`/`show`
/// provenance), the merged descriptor value (the base `harness lint` merges
/// candidate overlays onto), and its descriptor-backed adapter.
#[derive(Debug)]
struct RegistryEntry {
    label: &'static str,
    sources: Vec<(Layer, String)>,
    value: serde_json::Value,
    adapter: DescriptorAdapter,
}

impl RegistryEntry {
    /// True when any contributing source is an embedded built-in — the
    /// provenance check behind guard gating (guards are embedded-only).
    fn has_embedded_layer(&self) -> bool {
        self.sources
            .iter()
            .any(|(layer, _)| *layer == Layer::Embedded)
    }
}

/// Set once by `init_registry` (layered sources), or lazily to the embedded
/// built-ins on first pre-init access. The lazy fallback keeps unit tests
/// hermetic: they never call `init_registry`, so user-supplied descriptor
/// layers can't leak into them.
static REGISTRY: OnceLock<Vec<RegistryEntry>> = OnceLock::new();

fn registry() -> &'static Vec<RegistryEntry> {
    REGISTRY.get_or_init(|| {
        // The embedded descriptors are bundled and known-valid, so a failure
        // here is a programmer error (a bad descriptor edit) and panics —
        // mirroring the bundled-schema panics in `validation::schema` — with
        // the descriptor's own actionable message.
        build_registry(embedded_sources())
            .unwrap_or_else(|e| panic!("bundled harness descriptor is invalid: {e}"))
            .entries
    })
}

/// The registry could not be initialized from the layered sources.
#[derive(Debug, thiserror::Error)]
pub enum RegistryInitError {
    #[error(transparent)]
    HarnessFile(#[from] HarnessFileError),
    #[error(transparent)]
    Build(#[from] RegistryBuildError),
    #[error("the harness registry is already initialized")]
    AlreadyInitialized,
}

/// When `--harness-file` is in play, its descriptor's label — consulted by
/// `Default for Harness` so the one-off harness doesn't have to be repeated
/// via `--harness`.
static SESSION_DEFAULT_HARNESS: OnceLock<&'static str> = OnceLock::new();

/// Initialize the registry from every descriptor layer: embedded built-ins →
/// user-global (`<config-root>/harnesses/*.toml`) → project-local
/// (`<cwd>/.eval-magic/harnesses/*.toml`) → the optional one-off
/// `--harness-file`. Called once by the CLI entry point, before anything
/// resolves a harness.
///
/// Discovered files that fail to load are skipped with a warning on stderr
/// (pointing at `harness lint`); a broken `--harness-file` is a hard error.
/// When `--harness-file` names a descriptor, its label becomes the session's
/// default harness.
pub fn init_registry(harness_file: Option<&Path>) -> Result<(), RegistryInitError> {
    let project_root = std::env::current_dir().unwrap_or_default();
    let (sources, io_warnings) = discover_sources(
        default_config_root().as_deref(),
        &project_root,
        harness_file,
    )?;
    let built = build_registry(sources)?;
    for warning in io_warnings.iter().chain(&built.warnings) {
        eprintln!("{warning}");
    }
    if harness_file.is_some()
        && let Some(entry) = built
            .entries
            .iter()
            .find(|e| e.sources.iter().any(|(l, _)| *l == Layer::HarnessFile))
    {
        let _ = SESSION_DEFAULT_HARNESS.set(entry.label);
    }
    REGISTRY
        .set(built.entries)
        .map_err(|_| RegistryInitError::AlreadyInitialized)
}

/// A descriptor source set that cannot form a registry. Only embedded and
/// `--harness-file` sources can produce this — discovered layer files fail
/// soft (skipped with a warning in [`BuiltRegistry::warnings`]) so a broken
/// user descriptor never bricks the CLI.
#[derive(Debug, thiserror::Error)]
pub enum RegistryBuildError {
    #[error(transparent)]
    Descriptor(#[from] DescriptorError),
    #[error("duplicate harness label {label:?}: {first} and {second}")]
    DuplicateLabel {
        label: String,
        first: String,
        second: String,
    },
}

/// The outcome of loading a layered source set: the label-keyed entries plus
/// the warnings for every discovered file that was skipped.
#[derive(Debug)]
struct BuiltRegistry {
    entries: Vec<RegistryEntry>,
    warnings: Vec<String>,
}

/// One label's in-progress state while folding layered sources.
struct PendingEntry {
    label: String,
    value: serde_json::Value,
    descriptor: HarnessDescriptor,
    sources: Vec<(Layer, String)>,
}

/// Load descriptor sources into label-keyed entries with layered overrides.
///
/// Sources arrive in precedence order. The first source for a label defines
/// it; a later source with the same label from a *different* layer merges
/// field-by-field on top ([`merge_descriptor_value`]) and the merged result is
/// re-validated with the full provenance chain in its error messages. Two
/// same-label files within one layer have no precedence between them, so the
/// second is rejected (fatal for embedded, a skip-warning otherwise).
///
/// Failure policy per layer: embedded sources are known-valid (a failure is a
/// programmer error, propagated), `--harness-file` was explicitly named (a
/// failure is the user's answer, propagated), and discovered user/project
/// files fail soft — the file is skipped, the message lands in
/// [`BuiltRegistry::warnings`], and any earlier state for that label survives.
fn build_registry(sources: Vec<DescriptorSource>) -> Result<BuiltRegistry, RegistryBuildError> {
    let mut pending: Vec<PendingEntry> = Vec::new();
    let mut warnings: Vec<String> = Vec::new();
    for source in sources {
        let strict = matches!(source.layer, Layer::Embedded | Layer::HarnessFile);
        let mut fail_soft = |e: RegistryBuildError| -> Result<(), RegistryBuildError> {
            if strict {
                Err(e)
            } else {
                warnings.push(format!(
                    "skipping harness descriptor: {e}\n  (run `eval-magic harness lint <file>` \
                     for the full report)"
                ));
                Ok(())
            }
        };

        let value = match parse_descriptor_value(&source.toml_src, &source.path) {
            Ok(value) => value,
            Err(e) => {
                fail_soft(e.into())?;
                continue;
            }
        };
        if source.layer != Layer::Embedded
            && let Err(e) = check_user_layer_restrictions(&value, &source.path)
        {
            fail_soft(e.into())?;
            continue;
        }
        let label = value
            .get("label")
            .and_then(serde_json::Value::as_str)
            .expect("the schema gate requires a string label")
            .to_string();

        match pending.iter().position(|p| p.label == label) {
            None => match finalize_descriptor(&value, &source.path) {
                Ok(descriptor) => pending.push(PendingEntry {
                    label,
                    value,
                    descriptor,
                    sources: vec![(source.layer, source.path)],
                }),
                Err(e) => fail_soft(e.into())?,
            },
            Some(index) => {
                let entry = &mut pending[index];
                let (last_layer, last_path) = entry
                    .sources
                    .last()
                    .expect("every entry records its source");
                if *last_layer == source.layer {
                    fail_soft(RegistryBuildError::DuplicateLabel {
                        label,
                        first: last_path.clone(),
                        second: source.path,
                    })?;
                    continue;
                }
                let mut merged = entry.value.clone();
                merge_descriptor_value(&mut merged, value);
                let provenance = provenance_chain(&entry.sources, &source);
                match finalize_descriptor(&merged, &provenance) {
                    Ok(descriptor) => {
                        entry.value = merged;
                        entry.descriptor = descriptor;
                        entry.sources.push((source.layer, source.path));
                    }
                    Err(e) => fail_soft(e.into())?,
                }
            }
        }
    }

    let entries = pending
        .into_iter()
        .map(|p| RegistryEntry {
            // Leaked once per registry entry per process: the label becomes
            // the `'static` identity the rest of the crate passes around by
            // handle.
            label: Box::leak(p.label.into_boxed_str()),
            sources: p.sources,
            value: p.value,
            adapter: DescriptorAdapter::from_descriptor(p.descriptor),
        })
        .collect();
    Ok(BuiltRegistry { entries, warnings })
}

/// `base.toml (built-in) + overlay.toml (project)` — the provenance string a
/// merged descriptor's validation errors carry.
fn provenance_chain(sources: &[(Layer, String)], next: &DescriptorSource) -> String {
    sources
        .iter()
        .map(|(layer, path)| format!("{path} ({})", layer.display_name()))
        .chain([format!("{} ({})", next.path, next.layer.display_name())])
        .collect::<Vec<_>>()
        .join(" + ")
}

/// The registry-level default harness — what `--harness` falls back to when
/// absent. A registry concept rather than a descriptor field, so layered
/// user-supplied descriptor files (#136) can never fight over an
/// exactly-one-default invariant.
pub const DEFAULT_HARNESS_NAME: &str = "claude-code";

/// A harness name that no registry entry matches. Names every registered
/// harness so the rejection is actionable wherever it surfaces (artifact
/// deserialization, direct resolution).
#[derive(Debug, thiserror::Error)]
#[error("unknown harness '{name}'; known harnesses: {}", known.join(", "))]
pub struct UnknownHarnessError {
    pub name: String,
    pub known: Vec<&'static str>,
}

impl Harness {
    /// Resolve a kebab-case identifier against the descriptor registry — the
    /// only way to obtain a [`Harness`], so every held handle is valid.
    pub fn resolve(name: &str) -> Result<Harness, UnknownHarnessError> {
        registry()
            .iter()
            .find(|e| e.label == name)
            .map(|e| Harness::from_static_name(e.label))
            .ok_or_else(|| UnknownHarnessError {
                name: name.to_string(),
                known: Harness::known().map(Harness::name).collect(),
            })
    }

    /// Every registered harness, in registry (embedded descriptor) order —
    /// for code that must sweep all of them (e.g. guard teardown scans each
    /// harness's skills dir for a marker).
    pub fn known() -> impl Iterator<Item = Harness> {
        registry()
            .iter()
            .map(|e| Harness::from_static_name(e.label))
    }
}

impl Default for Harness {
    fn default() -> Self {
        // A `--harness-file` descriptor becomes the session default, so the
        // one-off harness doesn't have to be repeated via `--harness`.
        Harness::resolve(default_harness_name())
            .expect("the session default resolves against the registry")
    }
}

impl<'de> serde::Deserialize<'de> for Harness {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let name = String::deserialize(deserializer)?;
        Harness::resolve(&name).map_err(serde::de::Error::custom)
    }
}

/// Resolve the adapter for a [`Harness`]. This is the single dispatch point on
/// the harness identifier for all harness-specific behavior; every other
/// module goes through the returned trait object.
pub fn adapter_for(harness: Harness) -> &'static dyn HarnessAdapter {
    &registry()
        .iter()
        .find(|e| e.label == harness.name())
        .expect("Harness handles originate from the registry")
        .adapter
}

/// True when `harness` has an embedded built-in descriptor among its sources
/// — i.e. it is not defined by user-supplied descriptor files alone. Preflight
/// uses this to hard-reject `--guard` on user-only harnesses (the write guard
/// stays restricted to built-ins because it fails open).
pub fn has_embedded_layer(harness: Harness) -> bool {
    registry()
        .iter()
        .find(|e| e.label == harness.name())
        .expect("Harness handles originate from the registry")
        .has_embedded_layer()
}

/// One registered harness as `harness list`/`show`/`lint` see it: identity,
/// contributing sources in layer order, the merged descriptor value, and the
/// resolved descriptor.
pub struct HarnessInfo {
    pub label: &'static str,
    pub sources: &'static [(Layer, String)],
    pub value: &'static serde_json::Value,
    pub descriptor: &'static HarnessDescriptor,
}

/// Every registered harness in registry order, with provenance and the
/// resolved descriptor — the data source for the `harness` subcommands.
pub fn harness_info() -> impl Iterator<Item = HarnessInfo> {
    registry().iter().map(|e| HarnessInfo {
        label: e.label,
        sources: &e.sources,
        value: &e.value,
        descriptor: e.adapter.descriptor(),
    })
}

/// The effective default harness name: the `--harness-file` descriptor's
/// label when one is loaded, else [`DEFAULT_HARNESS_NAME`].
pub fn default_harness_name() -> &'static str {
    SESSION_DEFAULT_HARNESS
        .get()
        .copied()
        .unwrap_or(DEFAULT_HARNESS_NAME)
}

/// The union of every harness's project-local config dir names (sorted,
/// deduplicated): the dirs harness-agnostic code must treat as protected —
/// staging's sibling-asset filter, the guard's Bash tamper rule, and
/// detect-stray-writes' staging-dir lookbehind.
pub fn all_config_dir_names() -> Vec<String> {
    let mut names: Vec<String> = registry()
        .iter()
        .flat_map(|e| e.adapter.config_dir_names())
        .collect();
    names.sort_unstable();
    names.dedup();
    names
}

/// The union of every harness's tool vocabulary (each list sorted,
/// deduplicated). Computed once behind a `LazyLock` — the guard arbiter
/// consults it on every hooked tool call.
pub fn all_tool_vocabulary() -> &'static ToolVocabulary {
    static ALL: LazyLock<ToolVocabulary> = LazyLock::new(|| {
        let mut union = ToolVocabulary::default();
        for entry in registry().iter() {
            let vocab = entry.adapter.tool_vocabulary();
            union.write_tools.extend(vocab.write_tools);
            union.patch_tools.extend(vocab.patch_tools);
            union.shell_tools.extend(vocab.shell_tools);
            union.read_tools.extend(vocab.read_tools);
        }
        for list in [
            &mut union.write_tools,
            &mut union.patch_tools,
            &mut union.shell_tools,
            &mut union.read_tools,
        ] {
            list.sort_unstable();
            list.dedup();
        }
        union
    });
    &ALL
}

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

    use crate::adapters::descriptor::EMBEDDED_DESCRIPTORS;
    use crate::adapters::descriptor::layers::{Layer, embedded_sources};

    #[test]
    fn all_config_dir_names_unions_every_adapter() {
        assert_eq!(
            all_config_dir_names(),
            [".agents", ".claude", ".codex", ".opencode"]
        );
    }

    #[test]
    fn all_tool_vocabulary_unions_every_adapter() {
        let vocab = all_tool_vocabulary();
        assert_eq!(
            vocab.write_tools,
            ["Edit", "MultiEdit", "NotebookEdit", "Write", "file_change"]
        );
        assert_eq!(vocab.patch_tools, ["apply_patch"]);
        assert_eq!(vocab.shell_tools, ["Bash", "command_execution"]);
        assert_eq!(vocab.read_tools, ["Glob", "Grep", "Read"]);
    }

    fn src(layer: Layer, path: &str, toml_src: &str) -> DescriptorSource {
        DescriptorSource {
            layer,
            path: path.to_string(),
            toml_src: toml_src.to_string(),
        }
    }

    /// A schema-valid user descriptor declaring a [guard] block — must be
    /// rejected by the user-layer restriction, not the schema gate.
    const USER_GUARD_TOML: &str = r#"
label = "armed"

[guard]
hooks_file = ".armed/hooks.json"
matcher = "Write"
command_template = '"{exe}" guard-hook --harness armed "{marker}"'
hook_entry = '{"matcher":"{matcher}","hooks":[{"type":"command","command":"{command}"}]}'
verdict_template = '{"decision":"block","reason":"{reason}"}'
armed_message = "x"
"#;

    #[test]
    fn duplicate_embedded_label_errors() {
        let mut sources = embedded_sources();
        sources.push(sources[0].clone());
        let err = build_registry(sources).unwrap_err().to_string();
        assert!(err.contains("duplicate harness label"), "{err}");
        assert!(err.contains("claude-code"), "names the label: {err}");
        assert!(
            err.contains("harnesses/claude-code.toml"),
            "names the colliding source files: {err}"
        );
    }

    #[test]
    fn registry_entries_record_embedded_provenance() {
        let built = build_registry(embedded_sources()).unwrap();
        assert!(built.warnings.is_empty(), "{:?}", built.warnings);
        assert_eq!(built.entries.len(), EMBEDDED_DESCRIPTORS.len());
        for entry in &built.entries {
            assert_eq!(entry.sources.len(), 1, "one contributing file per built-in");
            assert_eq!(entry.sources[0].0, Layer::Embedded);
            assert!(
                entry.sources[0].1.contains(entry.label),
                "source path names the harness: {}",
                entry.sources[0].1
            );
        }
    }

    #[test]
    fn project_layer_overrides_a_single_field_of_a_builtin() {
        let mut sources = embedded_sources();
        sources.push(src(
            Layer::ProjectLocal,
            ".eval-magic/harnesses/claude-code.toml",
            "label = \"claude-code\"\n\n[model]\nflag = \"--model-x\"\n",
        ));
        let built = build_registry(sources).unwrap();
        assert!(built.warnings.is_empty(), "{:?}", built.warnings);
        assert_eq!(built.entries.len(), EMBEDDED_DESCRIPTORS.len());
        let entry = built
            .entries
            .iter()
            .find(|e| e.label == "claude-code")
            .unwrap();
        // The overridden field changed; everything else survives from the
        // embedded base (field-level merge, not file shadowing).
        assert_eq!(entry.adapter.cli_model_flag(), Some("--model-x".into()));
        assert!(entry.adapter.run_capabilities().supports_guard);
        assert_eq!(
            entry.sources,
            vec![
                (Layer::Embedded, "harnesses/claude-code.toml".to_string()),
                (
                    Layer::ProjectLocal,
                    ".eval-magic/harnesses/claude-code.toml".to_string()
                ),
            ]
        );
    }

    #[test]
    fn new_label_in_a_user_layer_registers_a_new_harness() {
        let mut sources = embedded_sources();
        sources.push(src(
            Layer::ProjectLocal,
            ".eval-magic/harnesses/cool.toml",
            "label = \"cool-custom-harness\"\n",
        ));
        let built = build_registry(sources).unwrap();
        assert!(built.warnings.is_empty(), "{:?}", built.warnings);
        let entry = built
            .entries
            .iter()
            .find(|e| e.label == "cool-custom-harness")
            .expect("new harness registered");
        assert_eq!(entry.sources[0].0, Layer::ProjectLocal);
        assert!(entry.adapter.skills_dir(Path::new("/r")).is_none());
    }

    #[test]
    fn discovered_file_declaring_a_guard_warns_and_is_skipped() {
        let mut sources = embedded_sources();
        sources.push(src(
            Layer::ProjectLocal,
            ".eval-magic/harnesses/armed.toml",
            USER_GUARD_TOML,
        ));
        let built = build_registry(sources).unwrap();
        assert_eq!(
            built.entries.len(),
            EMBEDDED_DESCRIPTORS.len(),
            "file skipped"
        );
        assert_eq!(built.warnings.len(), 1);
        assert!(
            built.warnings[0].contains("may not declare [guard]"),
            "{}",
            built.warnings[0]
        );
    }

    #[test]
    fn discovered_overlay_breaking_invariants_warns_and_keeps_the_base() {
        let mut sources = embedded_sources();
        // Replacing config_dirs orphans skills_dir's parent — an invariant
        // break that only shows post-merge.
        sources.push(src(
            Layer::ProjectLocal,
            ".eval-magic/harnesses/claude-code.toml",
            "label = \"claude-code\"\nconfig_dirs = [\".other\"]\n",
        ));
        let built = build_registry(sources).unwrap();
        assert_eq!(built.warnings.len(), 1);
        assert!(
            built.warnings[0].contains(".eval-magic/harnesses/claude-code.toml"),
            "names the offending file: {}",
            built.warnings[0]
        );
        let entry = built
            .entries
            .iter()
            .find(|e| e.label == "claude-code")
            .unwrap();
        assert_eq!(
            entry.adapter.config_dir_names(),
            vec![".claude".to_string()],
            "embedded base survives the dropped overlay"
        );
        assert_eq!(
            entry.sources.len(),
            1,
            "the bad overlay records no provenance"
        );
    }

    #[test]
    fn same_label_twice_in_one_discovered_layer_warns_and_skips_the_second() {
        let mut sources = embedded_sources();
        sources.push(src(
            Layer::ProjectLocal,
            "a.toml",
            "label = \"claude-code\"\n\n[model]\nflag = \"--from-a\"\n",
        ));
        sources.push(src(
            Layer::ProjectLocal,
            "b.toml",
            "label = \"claude-code\"\n\n[model]\nflag = \"--from-b\"\n",
        ));
        let built = build_registry(sources).unwrap();
        assert_eq!(built.warnings.len(), 1);
        assert!(
            built.warnings[0].contains("a.toml"),
            "{}",
            built.warnings[0]
        );
        assert!(
            built.warnings[0].contains("b.toml"),
            "{}",
            built.warnings[0]
        );
        let entry = built
            .entries
            .iter()
            .find(|e| e.label == "claude-code")
            .unwrap();
        assert_eq!(entry.adapter.cli_model_flag(), Some("--from-a".into()));
    }

    #[test]
    fn harness_file_failures_are_fatal() {
        let mut sources = embedded_sources();
        sources.push(src(Layer::HarnessFile, "one-off.toml", "label = "));
        let err = build_registry(sources).unwrap_err().to_string();
        assert!(err.contains("one-off.toml"), "{err}");
    }

    #[test]
    fn harness_file_guard_rejection_is_fatal() {
        let mut sources = embedded_sources();
        sources.push(src(Layer::HarnessFile, "one-off.toml", USER_GUARD_TOML));
        let err = build_registry(sources).unwrap_err().to_string();
        assert!(err.contains("may not declare [guard]"), "{err}");
    }

    #[test]
    fn harness_file_overlay_merges_on_top_of_discovered_layers() {
        let mut sources = embedded_sources();
        sources.push(src(
            Layer::ProjectLocal,
            "p.toml",
            "label = \"claude-code\"\n\n[model]\nflag = \"--from-project\"\n",
        ));
        sources.push(src(
            Layer::HarnessFile,
            "one-off.toml",
            "label = \"claude-code\"\n\n[model]\nflag = \"--from-file\"\n",
        ));
        let built = build_registry(sources).unwrap();
        assert!(built.warnings.is_empty(), "{:?}", built.warnings);
        let entry = built
            .entries
            .iter()
            .find(|e| e.label == "claude-code")
            .unwrap();
        assert_eq!(entry.adapter.cli_model_flag(), Some("--from-file".into()));
        assert_eq!(entry.sources.len(), 3);
    }

    #[test]
    fn embedded_layer_provenance_distinguishes_built_ins_from_user_only_harnesses() {
        let mut sources = embedded_sources();
        sources.push(src(
            Layer::ProjectLocal,
            ".eval-magic/harnesses/cool.toml",
            "label = \"cool\"\n",
        ));
        let built = build_registry(sources).unwrap();
        let entry = |label: &str| built.entries.iter().find(|e| e.label == label).unwrap();
        assert!(
            entry("claude-code").has_embedded_layer(),
            "built-ins carry their embedded source"
        );
        assert!(
            !entry("cool").has_embedded_layer(),
            "a user-only harness has no embedded layer"
        );
    }

    #[test]
    fn resolve_unknown_name_lists_known_harnesses() {
        let err = Harness::resolve("nonexistent").unwrap_err().to_string();
        assert!(err.contains("unknown harness 'nonexistent'"), "{err}");
        for name in ["claude-code", "codex", "opencode"] {
            assert!(err.contains(name), "error must name {name}: {err}");
        }
    }

    #[test]
    fn resolve_round_trips_every_registry_entry() {
        for harness in Harness::known() {
            assert_eq!(Harness::resolve(harness.name()).unwrap(), harness);
        }
    }

    #[test]
    fn default_harness_is_claude_code() {
        assert_eq!(DEFAULT_HARNESS_NAME, "claude-code");
        assert_eq!(Harness::default().name(), DEFAULT_HARNESS_NAME);
    }

    #[test]
    fn known_iterates_in_descriptor_order() {
        let names: Vec<_> = Harness::known().map(Harness::name).collect();
        assert_eq!(names, ["claude-code", "codex", "opencode"]);
    }

    #[test]
    fn labels_match_kebab_case_identifiers() {
        for name in ["claude-code", "codex", "opencode"] {
            let harness = Harness::resolve(name).unwrap();
            assert_eq!(adapter_for(harness).label(), name);
        }
    }
}