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
//! The harness adapter API — the single seam between generic dispatch code and
//! harness-specific behavior.
//!
//! The trait is tiered into a **baseline** every harness must implement and
//! **enhancements** that raise fidelity when a harness has the native support:
//!
//! - **Baseline (required):** [`label`](HarnessAdapter::label) and
//!   [`skills_dir`](HarnessAdapter::skills_dir). A new harness compiles with
//!   just these two methods; dispatched through its one-shot CLI (with
//!   `--no-stage` inlining the skill when native staging isn't wired), it
//!   already supports `llm_judge` grading and the `detect-stray-writes`
//!   post-pass.
//! - **Enhancements (defaulted):** every other method has a default — either a
//!   working generic fallback (e.g. the plain available-skills block) or an
//!   `Unsupported` error naming the enhancement it belongs to (e.g. transcript
//!   ingest, the write guard). Override the methods of an enhancement to wire
//!   it for a harness.
//!
//! Generic code resolves an adapter with [`adapter_for`](super::registry::adapter_for)
//! and then calls the trait — so the [`registry`](super::registry) is the one
//! place that names a concrete harness for this surface. The impls live in the
//! per-harness modules ([`claude_code`](super::claude_code),
//! [`codex`](super::codex), [`opencode`](super::opencode)).

use std::io;
use std::path::{Path, PathBuf};
use std::time::Duration;

use crate::core::{AvailableSkill, HarnessRunCapabilities, ToolInvocation};
use crate::sandbox::GuardMarker;

use super::TranscriptSummary;
use super::skill_shadow::PluginShadowReport;

/// One harness's tool-name vocabulary: every name its guard hook payloads or
/// transcript parser can produce, grouped by role. Consumers match against the
/// union across all harnesses ([`all_tool_vocabulary`](super::registry::all_tool_vocabulary)).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ToolVocabulary {
    /// Tools that write the filesystem with a single target path argument.
    pub write_tools: Vec<String>,
    /// apply_patch-style tools whose payload carries multiple patch targets.
    pub patch_tools: Vec<String>,
    /// Shell-execution tools carrying a `command` argument.
    pub shell_tools: Vec<String>,
    /// Read-only tools carrying a target path argument.
    pub read_tools: Vec<String>,
}

/// The behavior that varies by harness. Generic dispatch code depends on this
/// trait, never on a concrete harness variant. See the module docs for the
/// baseline-vs-enhancement contract.
pub trait HarnessAdapter {
    // ── Baseline (required) — every harness implements these ────────────────

    /// **Baseline.** The kebab-case identifier used in CLI flags,
    /// `dispatch.json`, and the staged `conditions.json`.
    fn label(&self) -> String;

    /// **Baseline.** The project-local directory staged skills live under for
    /// this harness. Under `--no-stage` nothing is staged into it, so a
    /// baseline harness may point this at any repo-local path its discovery
    /// would read. `None` when the harness declares no skills directory —
    /// native staging is then unavailable and the run preflight forces
    /// `--no-stage` (each SKILL.md is inlined into its dispatch prompt).
    fn skills_dir(&self, repo_root: &Path) -> Option<PathBuf>;

    // ── Run-option capabilities (defaulted) ──────────────────────────────────

    /// The run options the generic `run` preflight may accept for this
    /// harness. The default is the baseline: no write guard, `--bootstrap` and
    /// `--stage-name` allowed alongside `--no-stage`. Override alongside the
    /// enhancement that changes support (e.g. wiring the write guard flips
    /// `supports_guard`).
    fn run_capabilities(&self) -> HarnessRunCapabilities {
        HarnessRunCapabilities {
            supports_guard: false,
            supports_bootstrap_with_no_stage: true,
            supports_stage_name_with_no_stage: true,
        }
    }

    /// The project-local config dir names this harness reads or the adapter
    /// writes (e.g. `.claude`). Staging excludes every harness's config dirs
    /// when copying a skill's sibling assets, so a stray checked-in config dir
    /// never rides into a staged env. Via
    /// [`all_config_dir_names`](super::registry::all_config_dir_names) this list
    /// also feeds the guard's Bash tamper rule and detect-stray-writes'
    /// staging-dir lookbehind, so adding a dir here automatically grows the
    /// write-guard's deny surface. List the parent of
    /// [`skills_dir`](Self::skills_dir) plus any hook/config dirs the adapter
    /// writes.
    fn config_dir_names(&self) -> Vec<String> {
        Vec::new()
    }

    /// The tool names this harness's guard hook payloads and parsed transcripts
    /// use, grouped by role. Via
    /// [`all_tool_vocabulary`](super::registry::all_tool_vocabulary) this feeds the guard
    /// arbiter's tool classification and detect-stray-writes' invocation audit,
    /// so list every name this harness's surfaces produce — even names another
    /// harness also uses; the union dedups. Default empty: a harness with no
    /// guard and no transcript parser contributes nothing.
    fn tool_vocabulary(&self) -> ToolVocabulary {
        ToolVocabulary::default()
    }

    // ── Enhancement: native skill staging (defaulted) ────────────────────────
    // Fallback without it: `--no-stage` inlines each SKILL.md into its
    // dispatch prompt instead of staging files for native discovery.

    /// **Enhancement: native staging.** Build the conspicuous staged-skill
    /// slug. The default underscore form is fine for any harness without
    /// naming rules; a harness with constrained skill names (e.g. OpenCode)
    /// overrides it. `prefix` must be preserved so cleanup prefix-scans still
    /// find the staged dir.
    fn staged_slug(
        &self,
        prefix: &str,
        iteration: u32,
        condition: &str,
        skill_name: &str,
    ) -> String {
        format!("{prefix}{iteration}-{condition}__{skill_name}")
    }

    /// **Enhancement: native staging.** Validate a staged-skill identifier
    /// (generated slug or `--stage-name` override) against this harness's
    /// naming rules. The default accepts anything.
    fn validate_stage_name(&self, _name: &str) -> Result<(), String> {
        Ok(())
    }

    /// **Enhancement: native staging.** Whether a staged skill's frontmatter
    /// `name:` is rewritten to its slug so the harness's repo-local discovery
    /// resolves the staged copy.
    fn rewrites_frontmatter_name(&self) -> bool {
        false
    }

    /// **Enhancement: native staging.** Whether the skill-under-test is
    /// advertised in the available-skills block under its staged slug (vs. its
    /// natural name). True for Codex, whose repo-local discovery keys on the
    /// rewritten frontmatter name. (OpenCode also rewrites the frontmatter to
    /// the slug yet still advertises the natural name — a known inconsistency
    /// tracked for a separate fix.)
    fn advertises_staged_slug_name(&self) -> bool {
        false
    }

    /// **Enhancement: native staging.** Render the discoverable skills the way
    /// this harness natively surfaces them (e.g. Claude Code's Skill-tool
    /// list, Codex's `## Skills`, OpenCode's `<available_skills>` XML). The
    /// default is a neutral bulleted list.
    fn render_available_skills_block(&self, skills: &[AvailableSkill]) -> String {
        super::skills_block::render_skills_block(
            super::skills_block::DEFAULT_HEADER,
            super::skills_block::DEFAULT_ITEM,
            "",
            skills,
        )
    }

    /// **Enhancement: native staging.** How a staged skill is described as
    /// discoverable in the neutral slug-disambiguation line (e.g. "via the
    /// Skill tool").
    fn skill_surface_phrase(&self) -> String {
        "as a discoverable skill".to_string()
    }

    /// **Enhancement: native staging.** The lead-in for the fallback "read the
    /// skill from `<path>`" instruction when the staged identifier can't be
    /// resolved.
    fn skill_unresolved_phrase(&self) -> String {
        "If the staged skill cannot be resolved".to_string()
    }

    // ── Enhancement: transcript parser (defaulted) ───────────────────────────
    // Fallback without it: `transcript_check` assertions grade as
    // unverifiable, `llm_judge` carries the grading, token/cost/duration go
    // unrecorded, and run records are assembled by hand (or from
    // `outputs/final-message.md`) instead of auto-ingested.

    /// **Enhancement: transcript parser.** The filename (under a task's
    /// `outputs/` dir) this harness's one-shot CLI writes the captured
    /// transcript to. `None` when no transcript ingest is wired — the ingest
    /// pipeline then never calls the parsers below.
    fn cli_events_filename(&self) -> Option<String> {
        None
    }

    /// **Enhancement: transcript parser.** Parse the events file this
    /// harness's one-shot CLI wrote (the captured transcript) into ordered
    /// tool invocations.
    fn parse_cli_events(&self, _path: &Path) -> io::Result<Vec<ToolInvocation>> {
        Err(io::Error::new(
            io::ErrorKind::Unsupported,
            format!(
                "transcript ingest is not wired for the {} harness",
                self.label()
            ),
        ))
    }

    /// **Enhancement: transcript parser.** The full-summary counterpart of
    /// [`parse_cli_events`](Self::parse_cli_events): tool invocations, deduped
    /// token usage, duration, and final message text.
    fn parse_cli_events_full(&self, _path: &Path) -> io::Result<TranscriptSummary> {
        Err(io::Error::new(
            io::ErrorKind::Unsupported,
            format!(
                "transcript ingest is not wired for the {} harness",
                self.label()
            ),
        ))
    }

    /// **Enhancement: transcript parser.** Whether the parsed transcript
    /// exposes a deterministic skill-invocation event the `__skill_invoked`
    /// meta-check can match. False for Codex (its JSONL has no skill-tool
    /// event), which routes the meta-check to the LLM-judge fallback.
    fn transcript_surfaces_skill_invocation(&self) -> bool {
        true
    }

    // ── Enhancement: model flag (defaulted) ──────────────────────────────────
    // Fallback without it: `--agent-model` / `--judge-model` are recorded as
    // provenance only; dispatches run on the harness's default model.

    /// **Enhancement: model flag.** The native model-selection flag accepted
    /// by this harness's CLI. `None` means no model-selection support is
    /// wired.
    fn cli_model_flag(&self) -> Option<String> {
        None
    }

    // ── Enhancement: write guard (defaulted) ─────────────────────────────────
    // Fallback without it: the `detect-stray-writes` post-pass (folded into
    // `ingest`) audits out-of-bounds writes after the fact.

    /// **Enhancement: write guard.** Arm the write guard using this harness's
    /// native pre-tool hook surface, returning the staged marker path. The
    /// guard's allowed roots are derived from `stage_root` (the isolated env /
    /// agent cwd), so it bounds the agent to the same env boundary that
    /// isolates its reads.
    fn install_guard(
        &self,
        _stage_root: &Path,
        _guard_exe: &Path,
        _ttl: Option<Duration>,
    ) -> io::Result<PathBuf> {
        Err(io::Error::new(
            io::ErrorKind::Unsupported,
            format!("--guard is not supported for the {} harness", self.label()),
        ))
    }

    /// **Enhancement: write guard.** The banner printed after `--guard`
    /// successfully arms, describing the harness's native hook surface and how
    /// to remove it. `None` for a harness with no write guard (its
    /// [`install_guard`](Self::install_guard) errors), in which case no banner
    /// is printed.
    fn guard_armed_message(&self) -> Option<String> {
        None
    }

    /// **Enhancement: write guard.** Evaluate a PreToolUse hook `payload`
    /// against `marker`, returning the serialized deny verdict to print on
    /// stdout, or `None` to allow. The default fails open — a harness with no
    /// guard never denies — matching the hook entry points' contract that a
    /// guard invocation can never brick a session.
    fn guard_verdict(&self, _payload: &str, _marker: Option<GuardMarker>) -> Option<String> {
        None
    }

    /// **Enhancement: write guard.** A hook-config dir the guard install
    /// created outside [`skills_dir`](Self::skills_dir) (e.g. Codex's
    /// `.codex/`), which teardown prunes when restoring the original config
    /// leaves it empty. `None` when the guard writes only under existing dirs.
    fn guard_hook_cleanup_dir(&self, _stage_root: &Path) -> Option<PathBuf> {
        None
    }

    // ── Enhancement: shadow preflight (defaulted) ────────────────────────────
    // Fallback without it: no preflight — the run proceeds with no shadow
    // report, exactly as for a harness whose dispatches load nothing global.

    /// **Enhancement: shadow preflight.** Detect staged skill names that are
    /// also discoverable from the operator's live environment (e.g. Claude
    /// Code's enabled plugins or global skills dir), which contaminates the
    /// with/without comparison. `scan_root` is a real staged env root — its
    /// project-local settings participate in detection. `None` when the
    /// harness has no shadow preflight (the default) or nothing is shadowed.
    fn detect_shadowed_skills(
        &self,
        _scan_root: &Path,
        _staged_skill_names: &[&str],
    ) -> Option<PluginShadowReport> {
        None
    }

    // ── Enhancement: plan-mode context (defaulted) ───────────────────────────

    /// **Enhancement: plan-mode context.** Wrap a plan-mode profile as an
    /// operating-context layer. The shared `<system-reminder>` default
    /// usually suffices; a harness with a real native plan mode could inject
    /// it differently.
    fn render_plan_mode_context(&self, profile_text: &str) -> String {
        let trimmed = profile_text.trim();
        if trimmed.is_empty() {
            return String::new();
        }
        format!("<system-reminder>\n{trimmed}\n</system-reminder>")
    }

    // ── Enhancement: dispatch recipes (defaulted) ────────────────────────────
    // Fallback without them: `run` prints the generic handoff and the runbook
    // carries no copy-pasteable per-task command.

    /// **Enhancement: dispatch recipes.** Whether a copy-pasteable per-task
    /// exec command is wired (the descriptor's `[dispatch] exec_template`).
    /// `false` means `RUNBOOK.md` / `dispatch-manifest.md` carry handoff
    /// guidance without a per-task command recipe, and the `run` preflight
    /// warns naming that limitation.
    fn has_dispatch_recipes(&self) -> bool {
        false
    }

    /// **Enhancement: dispatch recipes.** The `Next:` guidance printed after
    /// `run`: how to dispatch each task through this harness's one-shot CLI
    /// and then ingest. Empty when no dispatch recipe is wired.
    fn cli_next_steps(&self, _ctx: CliDispatchContext<'_>) -> String {
        String::new()
    }

    /// **Enhancement: dispatch recipes.** Extra `dispatch-manifest.md` lines
    /// describing this harness's dispatch recipe (command template, parallel
    /// recipe, ingest note). `None` when the harness contributes no manifest
    /// section.
    fn cli_manifest_section(&self, _ctx: CliManifestContext<'_>) -> Option<Vec<String>> {
        None
    }

    /// **Enhancement: dispatch recipes.** The post-`grade` / post-`ingest`
    /// judge dispatch guidance for this harness. `None` leaves the generic
    /// judge handoff in place.
    fn cli_judge_next_steps(&self, _ctx: CliJudgeContext<'_>) -> Option<String> {
        None
    }
}

/// The shared (human-followed) `RUNBOOK.md` template used by every run,
/// regardless of harness (Claude Code, Codex, OpenCode).
pub const RUNBOOK_TEMPLATE: &str = include_str!("../../profiles/shared/runbook.md");

/// Context for rendering a harness's one-shot CLI agent-dispatch guidance.
#[derive(Debug, Clone, Copy)]
pub struct CliDispatchContext<'a> {
    pub guard: bool,
    pub target_args: &'a str,
    pub iteration: u32,
    pub agent_model: Option<&'a str>,
}

/// Context for rendering a harness's `dispatch-manifest.md` CLI recipe.
#[derive(Debug, Clone, Copy)]
pub struct CliManifestContext<'a> {
    pub guard: bool,
    pub agent_model: Option<&'a str>,
}

/// Context for rendering a harness's one-shot CLI judge-dispatch guidance.
#[derive(Debug, Clone, Copy)]
pub struct CliJudgeContext<'a> {
    pub guard: bool,
    pub iteration_dir: &'a Path,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::adapters::registry::adapter_for;
    use crate::core::Harness;

    // Cross-method invariants (guard/banner lockstep, hook-matcher ⊆
    // vocabulary, slug ↔ naming rules, …) are enforced at descriptor load
    // time in `descriptor::validation`; only per-value pins live here.

    #[test]
    fn detect_shadowed_skills_defaults_to_none_for_harnesses_without_a_preflight() {
        for h in [
            Harness::resolve("codex").unwrap(),
            Harness::resolve("opencode").unwrap(),
        ] {
            assert_eq!(
                adapter_for(h).detect_shadowed_skills(Path::new("/nonexistent"), &["any-skill"]),
                None
            );
        }
    }

    #[test]
    fn has_dispatch_recipes_matches_the_readme_support_table() {
        assert!(adapter_for(Harness::resolve("claude-code").unwrap()).has_dispatch_recipes());
        assert!(adapter_for(Harness::resolve("codex").unwrap()).has_dispatch_recipes());
        assert!(!adapter_for(Harness::resolve("opencode").unwrap()).has_dispatch_recipes());
    }

    #[test]
    fn skills_dir_is_harness_native() {
        let root = Path::new("/repo");
        assert_eq!(
            adapter_for(Harness::resolve("claude-code").unwrap()).skills_dir(root),
            Some(root.join(".claude").join("skills"))
        );
        assert_eq!(
            adapter_for(Harness::resolve("codex").unwrap()).skills_dir(root),
            Some(root.join(".agents").join("skills"))
        );
        assert_eq!(
            adapter_for(Harness::resolve("opencode").unwrap()).skills_dir(root),
            Some(root.join(".opencode").join("skills"))
        );
    }

    #[test]
    fn only_codex_and_opencode_rewrite_frontmatter() {
        assert!(!adapter_for(Harness::resolve("claude-code").unwrap()).rewrites_frontmatter_name());
        assert!(adapter_for(Harness::resolve("codex").unwrap()).rewrites_frontmatter_name());
        assert!(adapter_for(Harness::resolve("opencode").unwrap()).rewrites_frontmatter_name());
    }

    #[test]
    fn plan_mode_context_wraps_in_system_reminder_for_every_harness() {
        for h in Harness::known() {
            let out = adapter_for(h).render_plan_mode_context("BODY");
            assert_eq!(out, "<system-reminder>\nBODY\n</system-reminder>");
            assert_eq!(adapter_for(h).render_plan_mode_context("   "), "");
            assert_eq!(
                adapter_for(h).render_plan_mode_context("\n\n  BODY  \n\n"),
                "<system-reminder>\nBODY\n</system-reminder>"
            );
        }
    }

    #[test]
    fn run_capabilities_capture_run_option_support_by_harness() {
        let claude = adapter_for(Harness::resolve("claude-code").unwrap()).run_capabilities();
        assert!(claude.supports_guard);
        assert!(claude.supports_bootstrap_with_no_stage);
        assert!(claude.supports_stage_name_with_no_stage);

        let codex = adapter_for(Harness::resolve("codex").unwrap()).run_capabilities();
        assert!(codex.supports_guard);
        assert!(!codex.supports_bootstrap_with_no_stage);
        assert!(!codex.supports_stage_name_with_no_stage);

        let opencode = adapter_for(Harness::resolve("opencode").unwrap()).run_capabilities();
        assert!(!opencode.supports_guard);
        assert!(opencode.supports_bootstrap_with_no_stage);
        assert!(opencode.supports_stage_name_with_no_stage);
    }

    #[test]
    fn guard_armed_message_is_harness_specific_and_absent_for_opencode() {
        // The post-arm `--guard` banner names the harness's native hook surface,
        // so it lives behind the adapter rather than in generic run code.
        let claude = adapter_for(Harness::resolve("claude-code").unwrap())
            .guard_armed_message()
            .expect("claude code has a write guard");
        assert!(
            claude.contains(".claude/settings.local.json"),
            "claude banner names its hook file: {claude}"
        );

        let codex = adapter_for(Harness::resolve("codex").unwrap())
            .guard_armed_message()
            .expect("codex has a write guard");
        assert!(
            codex.contains(".codex/hooks.json"),
            "codex banner names its hook file: {codex}"
        );

        // OpenCode has no write guard (its install_guard errors), so there is no
        // banner to print.
        assert_eq!(
            adapter_for(Harness::resolve("opencode").unwrap()).guard_armed_message(),
            None
        );
    }

    #[test]
    fn staged_slug_default_and_opencode_override_preserve_the_prefix() {
        let prefix = "slow-powers-eval-";
        assert_eq!(
            adapter_for(Harness::resolve("claude-code").unwrap()).staged_slug(
                prefix,
                2,
                "with_skill",
                "my-skill"
            ),
            "slow-powers-eval-2-with_skill__my-skill"
        );
        assert_eq!(
            adapter_for(Harness::resolve("opencode").unwrap()).staged_slug(
                prefix,
                2,
                "with_skill",
                "my-skill"
            ),
            "slow-powers-eval-2-with-skill-my-skill"
        );
    }
}