agentcarousel 0.7.0

Unit tests for AI agents. Run behavioral tests in CI, score with an LLM judge, and export signed evidence your auditors accept.
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
use agentcarousel_core::{
    judge_key_candidates, judge_provider_from_model, prefetch_pricing, JudgeProvider,
};
use agentcarousel_evaluators::run_prompt_audit;
use agentcarousel_reporters::{fetch_run, persist_run, print_audit};
use chrono::Utc;
use clap::{Parser, Subcommand};
use indicatif::{ProgressBar, ProgressStyle};
use std::fs;
use std::io::{stderr, IsTerminal};
use std::path::{Path, PathBuf};
use std::time::Duration;

use super::config::ResolvedConfig;
use super::exit_codes::ExitCode;
use super::output::{JsonError, JsonOutput};
use super::GlobalOptions;

/// Prompt-audit analysis commands: run a second-pass audit against a saved run or apply its
/// suggestions to your prompt.md.
#[derive(Debug, Parser)]
pub struct AuditArgs {
    /// Config file path (default: agentcarousel.toml in the current directory).
    #[arg(long)]
    pub config: Option<PathBuf>,
    #[command(subcommand)]
    command: AuditCommand,
}

#[derive(Debug, Subcommand)]
enum AuditCommand {
    /// Run the prompt-audit LLM analysis against a saved run and store the result.
    ///
    /// Loads the run, calls the judge to diagnose whether failures are due to prompt
    /// design, model capability, or fixture miscalibration, and saves the result back
    /// to the history DB so `agc report show <id>` displays it going forward.
    #[command(
        after_help = "Examples:\n  agc audit run ECR99Y6BWT\n  agc audit run ECR99Y6BWT --prompt fixtures/my-skill/prompt.md\n  agc audit run ECR99Y6BWT --model claude-opus-4-7 --json\n  agc audit run path/to/run.json"
    )]
    Run {
        /// Run ID from `agc report list`, or a path to run.json / a directory containing it.
        run_id: String,
        /// Path to the prompt.md to audit against (default: auto-discovered from fixtures/<skill>/prompt.md).
        #[arg(long)]
        prompt: Option<PathBuf>,
        /// Judge model to use (overrides config judge.model).
        #[arg(long)]
        model: Option<String>,
        /// Base URL for a custom/Ollama judge endpoint (required when judge model is custom/* or ollama/*).
        #[arg(long, value_name = "URL")]
        judge_endpoint: Option<String>,
        /// Do not save the audit result back to the history database.
        #[arg(long)]
        no_save: bool,
    },
    /// Print suggestions from a stored audit, or apply them to your prompt.md.
    ///
    /// Reads the `suggested_fixes` from a previously-stored audit result — no LLM call.
    /// Without `--apply`, prints suggestions to stdout. With `--apply`, appends them as
    /// a commented block to prompt.md so you can review and integrate them like a diff.
    #[command(
        after_help = "Examples:\n  agc audit suggest ECR99Y6BWT                           # print suggestions\n  agc audit suggest ECR99Y6BWT --apply                   # append to prompt.md\n  agc audit suggest ECR99Y6BWT --apply --prompt fixtures/my-skill/prompt.md"
    )]
    Suggest {
        /// Run ID from `agc report list`, or a path to run.json / a directory containing it.
        run_id: String,
        /// Append the suggestions as a commented block to prompt.md.
        #[arg(long)]
        apply: bool,
        /// Path to the prompt.md to write into (default: auto-discovered from fixtures/<skill>/prompt.md).
        #[arg(long)]
        prompt: Option<PathBuf>,
    },
}

pub fn run_audit_command(args: AuditArgs, config: &ResolvedConfig, globals: &GlobalOptions) -> i32 {
    match args.command {
        AuditCommand::Run {
            run_id,
            prompt,
            model,
            judge_endpoint,
            no_save,
        } => run_audit(
            run_id,
            prompt,
            model,
            judge_endpoint,
            no_save,
            config,
            globals,
        ),
        AuditCommand::Suggest {
            run_id,
            apply,
            prompt,
        } => run_suggest(run_id, apply, prompt, globals),
    }
}

fn run_audit(
    run_id: String,
    prompt_path: Option<PathBuf>,
    model: Option<String>,
    judge_endpoint: Option<String>,
    no_save: bool,
    config: &ResolvedConfig,
    globals: &GlobalOptions,
) -> i32 {
    let run = match load_run_for_audit(&run_id) {
        Ok(r) => r,
        Err(err) => {
            if globals.json {
                JsonOutput::err(
                    "audit",
                    JsonError::new("not_found", err).with_suggestions(vec![
                        "Run 'agc report list' to see available run IDs.".to_string(),
                    ]),
                )
                .print();
            } else {
                eprintln!("error: {err}");
            }
            return ExitCode::NotFound.as_i32();
        }
    };

    let prompt_text = match resolve_prompt(prompt_path.as_deref(), &run) {
        Some(t) => t,
        None => {
            let msg = "could not find prompt.md — pass --prompt <path> to specify it explicitly";
            if globals.json {
                JsonOutput::err("audit", JsonError::new("not_found", msg)).print();
            } else {
                eprintln!("error: {msg}");
                eprintln!(
                    "hint: looked for fixtures/{}/prompt.md",
                    run.skill_or_agent.as_deref().unwrap_or("<skill>")
                );
            }
            return ExitCode::NotFound.as_i32();
        }
    };

    let judge_model = model.unwrap_or_else(|| config.judge.model.clone());
    let resolved_judge_endpoint = judge_endpoint.as_deref();
    let judge_provider = judge_provider_from_model(&judge_model);

    if !matches!(judge_provider, JudgeProvider::Custom) {
        let has_key = judge_key_candidates(judge_provider)
            .iter()
            .any(|k| std::env::var(k).is_ok());
        if !has_key {
            let keys = judge_key_candidates(judge_provider).join(", ");
            let msg = format!(
                "set one of {} to run audit for model '{}'",
                keys, judge_model
            );
            if globals.json {
                JsonOutput::err("audit", JsonError::new("auth_error", msg)).print();
            } else {
                eprintln!("error: {msg}");
            }
            return ExitCode::ConfigError.as_i32();
        }
    } else if resolved_judge_endpoint.is_none() {
        let msg = format!(
            "--judge-endpoint is required for custom model '{}'",
            judge_model
        );
        if globals.json {
            JsonOutput::err("audit", JsonError::new("config_error", msg)).print();
        } else {
            eprintln!("error: {msg}");
        }
        return ExitCode::ConfigError.as_i32();
    }

    prefetch_pricing();
    let show_spinner = !globals.quiet && !globals.json && stderr().is_terminal();
    let spinner: Option<ProgressBar> = if show_spinner {
        let pb = ProgressBar::new_spinner();
        pb.set_style(
            ProgressStyle::with_template("{spinner:.green} {msg}")
                .expect("spinner template")
                .tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈ "),
        );
        pb.set_message(format!(
            "Running prompt audit for run {} with {}...",
            &run.id.0, judge_model
        ));
        pb.enable_steady_tick(Duration::from_millis(120));
        Some(pb)
    } else {
        None
    };

    let audit_max_tokens = config.judge.max_tokens.map(|t| t.max(4096));
    let audit = match run_prompt_audit(
        &prompt_text,
        &run.cases,
        &judge_model,
        audit_max_tokens,
        resolved_judge_endpoint,
    ) {
        Ok(a) => {
            if let Some(ref pb) = spinner {
                pb.finish_and_clear();
            }
            a
        }
        Err(err) => {
            if let Some(ref pb) = spinner {
                pb.finish_and_clear();
            }
            if globals.json {
                JsonOutput::err("audit", JsonError::new("runtime_error", err.to_string())).print();
            } else {
                eprintln!("error: prompt audit failed: {err}");
            }
            return ExitCode::RuntimeError.as_i32();
        }
    };

    let mut run = run;
    run.prompt_audit = Some(audit);

    if !no_save {
        if let Err(err) = persist_run(&run) {
            eprintln!("warn: audit result was not saved to history: {err}");
            eprintln!(
                "hint: rerun `agc audit run {}` to retry the save",
                &run.id.0
            );
        }
    }

    if globals.json {
        let value = serde_json::to_value(&run.prompt_audit).unwrap_or(serde_json::Value::Null);
        JsonOutput::ok("audit", value).print();
    } else {
        println!();
        print_audit(&run);
    }

    ExitCode::Ok.as_i32()
}

fn run_suggest(
    run_id: String,
    apply: bool,
    prompt_path: Option<PathBuf>,
    globals: &GlobalOptions,
) -> i32 {
    let run = match load_run_for_audit(&run_id) {
        Ok(r) => r,
        Err(err) => {
            if globals.json {
                JsonOutput::err(
                    "audit suggest",
                    JsonError::new("not_found", err).with_suggestions(vec![
                        "Run 'agc report list' to see available run IDs.".to_string(),
                    ]),
                )
                .print();
            } else {
                eprintln!("error: {err}");
            }
            return ExitCode::NotFound.as_i32();
        }
    };

    let audit = match &run.prompt_audit {
        Some(a) => a,
        None => {
            let msg = format!(
                "run {} has no stored audit — run `agc audit run {}` first",
                run_id, run_id
            );
            if globals.json {
                JsonOutput::err("audit suggest", JsonError::new("not_found", &*msg)).print();
            } else {
                eprintln!("error: {msg}");
            }
            return ExitCode::NotFound.as_i32();
        }
    };

    if audit.suggested_fixes.is_empty() {
        if globals.json {
            JsonOutput::ok("audit suggest", serde_json::json!({ "suggestions": [] })).print();
        } else {
            println!("no suggestions in the stored audit for run {run_id}");
        }
        return ExitCode::Ok.as_i32();
    }

    if globals.json && !apply {
        let pairs: Vec<serde_json::Value> = audit
            .suggested_fixes
            .iter()
            .enumerate()
            .map(|(i, fix)| {
                let mut obj = serde_json::json!({ "title": fix });
                if let Some(imp) = audit.suggested_implementations.get(i) {
                    obj["implementation"] = serde_json::Value::String(imp.clone());
                }
                obj
            })
            .collect();
        JsonOutput::ok("audit suggest", serde_json::json!({ "suggestions": pairs })).print();
        return ExitCode::Ok.as_i32();
    }

    if !apply {
        println!("Suggestions for run {run_id}:");
        println!();
        for (i, fix) in audit.suggested_fixes.iter().enumerate() {
            println!("  {}. {}", i + 1, fix);
            if let Some(imp) = audit.suggested_implementations.get(i) {
                println!();
                for line in imp.lines() {
                    println!("     {}", line);
                }
                println!();
            }
        }
        println!(
            "hint: run `agc audit suggest {} --apply` to append these to prompt.md",
            run_id
        );
        return ExitCode::Ok.as_i32();
    }

    // --apply: find and write to prompt.md
    let prompt_file = match resolve_prompt_path(prompt_path.as_deref(), &run) {
        Some(p) => p,
        None => {
            let msg = "could not find prompt.md — pass --prompt <path> to specify it";
            if globals.json {
                JsonOutput::err("audit suggest", JsonError::new("not_found", msg)).print();
            } else {
                eprintln!("error: {msg}");
                eprintln!(
                    "hint: looked for fixtures/{}/prompt.md",
                    run.skill_or_agent.as_deref().unwrap_or("<skill>")
                );
            }
            return ExitCode::NotFound.as_i32();
        }
    };

    let existing = match fs::read_to_string(&prompt_file) {
        Ok(s) => s,
        Err(err) => {
            let msg = format!("could not read {}: {err}", prompt_file.display());
            if globals.json {
                JsonOutput::err("audit suggest", JsonError::new("io_error", &*msg)).print();
            } else {
                eprintln!("error: {msg}");
            }
            return ExitCode::RuntimeError.as_i32();
        }
    };

    let date = Utc::now().format("%Y-%m-%d").to_string();
    let mut block = format!(
        "\n<!-- audit:suggestions run={} generated={} -->\n",
        run_id, date
    );
    for (i, fix) in audit.suggested_fixes.iter().enumerate() {
        block.push_str(&format!("<!-- fix {}: {} -->\n", i + 1, fix));
        if let Some(implementation) = audit.suggested_implementations.get(i) {
            block.push_str(implementation.trim());
            block.push_str("\n\n");
        }
    }
    block.push_str("<!-- /audit:suggestions -->\n");

    let updated = format!("{}{}", existing.trim_end_matches('\n'), block);

    if let Err(err) = fs::write(&prompt_file, &updated) {
        let msg = format!("could not write {}: {err}", prompt_file.display());
        if globals.json {
            JsonOutput::err("audit suggest", JsonError::new("io_error", &*msg)).print();
        } else {
            eprintln!("error: {msg}");
        }
        return ExitCode::RuntimeError.as_i32();
    }

    let impl_count = audit
        .suggested_implementations
        .iter()
        .filter(|s| !s.trim().is_empty())
        .count();

    if globals.json {
        JsonOutput::ok(
            "audit suggest",
            serde_json::json!({
                "applied": audit.suggested_fixes.len(),
                "with_implementations": impl_count,
                "prompt_path": prompt_file.display().to_string(),
            }),
        )
        .print();
    } else {
        println!(
            "applied {} suggestion(s) to {} ({} with worked implementations)",
            audit.suggested_fixes.len(),
            prompt_file.display(),
            impl_count,
        );
        println!();
        for (i, fix) in audit.suggested_fixes.iter().enumerate() {
            let has_impl = audit.suggested_implementations.get(i).is_some();
            println!(
                "  {}. {} {}",
                i + 1,
                fix,
                if has_impl { "[+implementation]" } else { "" }
            );
        }
        println!();
        println!("Review and remove the <!-- audit:suggestions --> block after integrating.");
    }

    ExitCode::Ok.as_i32()
}

fn load_run_for_audit(run_ref: &str) -> Result<agentcarousel_core::Run, String> {
    let path = Path::new(run_ref);
    if path.exists() {
        let json_path = if path.is_dir() {
            path.join("run.json")
        } else {
            path.to_path_buf()
        };
        if !json_path.is_file() {
            return Err(format!(
                "expected {} to be a run.json file or a directory containing run.json",
                run_ref
            ));
        }
        let raw = fs::read_to_string(&json_path).map_err(|e| e.to_string())?;
        serde_json::from_str(&raw).map_err(|e| format!("{}: {e}", json_path.display()))
    } else {
        fetch_run(run_ref).map_err(|e| e.to_string())
    }
}

fn resolve_prompt(explicit: Option<&Path>, run: &agentcarousel_core::Run) -> Option<String> {
    if let Some(path) = explicit {
        return fs::read_to_string(path)
            .ok()
            .filter(|s| !s.trim().is_empty());
    }
    let skill = run.skill_or_agent.as_deref()?;
    let candidate = PathBuf::from("fixtures").join(skill).join("prompt.md");
    fs::read_to_string(&candidate)
        .ok()
        .filter(|s| !s.trim().is_empty())
}

fn resolve_prompt_path(explicit: Option<&Path>, run: &agentcarousel_core::Run) -> Option<PathBuf> {
    if let Some(path) = explicit {
        return Some(path.to_path_buf());
    }
    let skill = run.skill_or_agent.as_deref()?;
    let candidate = PathBuf::from("fixtures").join(skill).join("prompt.md");
    candidate.is_file().then_some(candidate)
}