agentcarousel 0.8.1

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
use agentcarousel::{
    persist_run, CaseId, CaseResult, CaseStatus, ExecutionTrace, Metrics, OverallStatus,
    ProviderErrorMetrics, Run, RunId, RunSummary,
};
use assert_cmd::Command;
use chrono::Utc;
use rusqlite::{params, Connection};
use std::fs;
use std::path::PathBuf;
use std::sync::Mutex;

/// `AGENTCAROUSEL_HISTORY_DB` is process-global; serialize tests that mutate it.
static HISTORY_ENV_LOCK: Mutex<()> = Mutex::new(());

fn workspace_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .and_then(|path| path.parent())
        .map(PathBuf::from)
        .expect("workspace root")
}

/// Seeded runs must match the live bundle version `agc publish` reads from the
/// fixture, so derive it from cases.yaml instead of hardcoding it.
fn github_actions_bundle_version() -> String {
    let cases =
        fs::read_to_string(workspace_root().join("fixtures/github-actions-generator/cases.yaml"))
            .expect("read github-actions-generator cases.yaml");
    cases
        .lines()
        .find_map(|line| line.strip_prefix("bundle_version:"))
        .expect("bundle_version in cases.yaml")
        .trim()
        .to_string()
}

fn temp_paths() -> (PathBuf, PathBuf, PathBuf) {
    let dir = tempfile::tempdir().expect("temp dir");
    let base = dir.keep();
    (
        base.join("history.db"),
        base.join("agentcarousel.toml"),
        base.join("sample-evidence.tar.gz"),
    )
}

fn minimal_run(id: &str, bundle_id: &str, bundle_version: &str) -> Run {
    let case = CaseResult {
        case_id: CaseId("example-skill/positive".to_string()),
        status: CaseStatus::Passed,
        error: None,
        trace: ExecutionTrace {
            steps: Vec::new(),
            final_output: Some("ok".to_string()),
            redacted: false,
        },
        metrics: Metrics {
            total_latency_ms: 1,
            ..Metrics::default()
        },
        eval_scores: None,
        input: vec![],
        discrimination_score: None,
        discrimination_label: None,
        tags: Vec::new(),
    };
    let started_at = Utc::now();
    Run {
        id: RunId(id.to_string()),
        schema_version: 1,
        started_at,
        finished_at: Some(started_at),
        command: "eval".to_string(),
        git_sha: Some("a".repeat(40)),
        agentcarousel_version: env!("CARGO_PKG_VERSION").to_string(),
        config_hash: "none".to_string(),
        cases: vec![case],
        summary: RunSummary {
            total: 1,
            passed: 1,
            failed: 0,
            skipped: 0,
            flaky: 0,
            errored: 0,
            timed_out: 0,
            pass_rate: 1.0,
            mean_latency_ms: 1.0,
            mean_effectiveness_score: Some(0.9),
            provider_errors: ProviderErrorMetrics::default(),
            overall_status: OverallStatus::Pass,
            tokens_in: None,
            tokens_out: None,
            mean_tokens_per_judged_case: None,
            latency_p50_ms: None,
            latency_p95_ms: None,
            latency_p99_ms: None,
            judge_tokens_in: None,
            judge_tokens_out: None,
            gen_cost_usd: None,
            judge_cost_usd: None,
            total_cost_usd: None,
            generator_model: None,
            judge_model: None,
            command_line: None,
        },
        fixture_bundle_id: Some(bundle_id.to_string()),
        fixture_bundle_version: Some(bundle_version.to_string()),
        skill_or_agent: Some("example-skill".to_string()),
        runner_offline: false,
        runner_mock_strict: false,
        runner_mock_only: true,
        prompt_audit: None,
    }
}

fn insert_legacy_malformed_run(history_path: &PathBuf, id: &str) {
    let conn = Connection::open(history_path).expect("open history db");
    conn.execute(
        "CREATE TABLE IF NOT EXISTS runs (
            id TEXT PRIMARY KEY,
            started_at TEXT NOT NULL,
            run_json TEXT NOT NULL
        )",
        [],
    )
    .expect("create runs table");
    // Intentionally missing modern required fields (e.g., provider_errors).
    let legacy_json = r#"{"id":"legacy-run","schema_version":1}"#;
    conn.execute(
        "INSERT OR REPLACE INTO runs (id, started_at, run_json) VALUES (?1, ?2, ?3)",
        params![id, Utc::now().to_rfc3339(), legacy_json],
    )
    .expect("insert malformed run");
}

#[test]
fn publish_dry_run_auto_selects_latest_matching_run() {
    let _guard = HISTORY_ENV_LOCK.lock().expect("history lock");
    let (history_path, config_path, evidence_path) = temp_paths();
    std::env::set_var("AGENTCAROUSEL_HISTORY_DB", &history_path);
    persist_run(&minimal_run(
        "bundle-registry-run-match",
        "agentcarousel/github-actions-generator",
        &github_actions_bundle_version(),
    ))
    .expect("persist run");

    fs::write(
        &config_path,
        format!(
            r#"[report]
history_db = "{}"
"#,
            history_path.display()
        ),
    )
    .expect("write config");
    fs::write(&evidence_path, b"fake").expect("write evidence");

    let root = workspace_root();
    let assert = Command::cargo_bin("agentcarousel")
        .unwrap()
        .current_dir(&root)
        .args([
            "publish",
            "--config",
            config_path.to_str().expect("config path"),
            "fixtures/github-actions-generator",
            "--url",
            "https://registry.example.test",
            "--dry-run",
            "--evidence",
            evidence_path.to_str().expect("evidence path"),
        ])
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    assert!(
        stdout.contains("bundle-registry-run-match"),
        "expected selected run id in output, got: {stdout:?}"
    );
    let registry_id = format!(
        "github-actions-generator-{}",
        github_actions_bundle_version()
    );
    assert!(
        stdout.contains(&registry_id),
        "expected registry bundle id {registry_id} in output, got: {stdout:?}"
    );

    std::env::remove_var("AGENTCAROUSEL_HISTORY_DB");
}

#[test]
fn publish_dry_run_all_runs_lists_multiple_run_ids() {
    let _guard = HISTORY_ENV_LOCK.lock().expect("history lock");
    let (history_path, config_path, evidence_path) = temp_paths();
    std::env::set_var("AGENTCAROUSEL_HISTORY_DB", &history_path);
    persist_run(&minimal_run(
        "bundle-registry-run-match-a",
        "agentcarousel/github-actions-generator",
        &github_actions_bundle_version(),
    ))
    .expect("persist run a");
    persist_run(&minimal_run(
        "bundle-registry-run-match-b",
        "agentcarousel/github-actions-generator",
        &github_actions_bundle_version(),
    ))
    .expect("persist run b");

    fs::write(
        &config_path,
        format!(
            r#"[report]
history_db = "{}"
"#,
            history_path.display()
        ),
    )
    .expect("write config");
    fs::write(&evidence_path, b"fake").expect("write evidence");

    let root = workspace_root();
    let assert = Command::cargo_bin("agentcarousel")
        .unwrap()
        .current_dir(&root)
        .args([
            "publish",
            "--config",
            config_path.to_str().expect("config path"),
            "fixtures/github-actions-generator",
            "--url",
            "https://registry.example.test",
            "--dry-run",
            "--all-runs",
            "--limit",
            "2",
        ])
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    assert!(
        stdout.contains("\"run_count\": 2"),
        "expected run count in output, got: {stdout:?}"
    );
    assert!(
        stdout.contains("bundle-registry-run-match-a")
            || stdout.contains("bundle-registry-run-match-b"),
        "expected matching run ids in output, got: {stdout:?}"
    );
    std::env::remove_var("AGENTCAROUSEL_HISTORY_DB");
}

#[test]
fn publish_dry_run_all_runs_skips_unreadable_history_rows() {
    let _guard = HISTORY_ENV_LOCK.lock().expect("history lock");
    let (history_path, config_path, _) = temp_paths();
    std::env::set_var("AGENTCAROUSEL_HISTORY_DB", &history_path);
    insert_legacy_malformed_run(&history_path, "legacy-malformed-run");
    persist_run(&minimal_run(
        "bundle-registry-run-match-good",
        "agentcarousel/github-actions-generator",
        &github_actions_bundle_version(),
    ))
    .expect("persist valid run");

    fs::write(
        &config_path,
        format!(
            r#"[report]
history_db = "{}"
"#,
            history_path.display()
        ),
    )
    .expect("write config");

    let root = workspace_root();
    let assert = Command::cargo_bin("agentcarousel")
        .unwrap()
        .current_dir(&root)
        .args([
            "publish",
            "--config",
            config_path.to_str().expect("config path"),
            "fixtures/github-actions-generator",
            "--url",
            "https://registry.example.test",
            "--dry-run",
            "--all-runs",
        ])
        .assert()
        .success();
    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    assert!(
        stdout.contains("bundle-registry-run-match-good"),
        "expected valid run id in output, got: {stdout:?}"
    );
    assert!(
        stderr.contains("skipping unreadable run legacy-malformed-run"),
        "expected unreadable run warning, got: {stderr:?}"
    );
    std::env::remove_var("AGENTCAROUSEL_HISTORY_DB");
}

#[test]
fn publish_rejects_all_runs_with_single_evidence_path() {
    let (_, _, evidence_path) = temp_paths();
    fs::write(&evidence_path, b"fake").expect("write evidence");
    let root = workspace_root();
    let assert = Command::cargo_bin("agentcarousel")
        .unwrap()
        .current_dir(&root)
        .args([
            "publish",
            "fixtures/github-actions-generator",
            "--url",
            "https://registry.example.test",
            "--all-runs",
            "--evidence",
            evidence_path.to_str().expect("evidence path"),
        ])
        .assert()
        .failure();
    // In JSON mode (stdout not a TTY in tests), errors go to stdout as JSON envelope
    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    let combined = format!("{stdout}{stderr}");
    assert!(
        combined.contains("cannot combine --all-runs with --evidence"),
        "expected argument guidance, got stdout: {stdout:?}, stderr: {stderr:?}"
    );
}

#[test]
fn publish_fails_fast_when_token_missing() {
    let _guard = HISTORY_ENV_LOCK.lock().expect("history lock");
    let (history_path, _config_path, _) = temp_paths();
    std::env::set_var("AGENTCAROUSEL_HISTORY_DB", &history_path);
    persist_run(&minimal_run(
        "bundle-registry-run-token-missing",
        "agentcarousel/github-actions-generator",
        &github_actions_bundle_version(),
    ))
    .expect("persist run");
    std::env::remove_var("AGENTCAROUSEL_API_TOKEN");
    let root = workspace_root();
    let assert = Command::cargo_bin("agentcarousel")
        .unwrap()
        .current_dir(&root)
        .args([
            "publish",
            "fixtures/github-actions-generator",
            "--url",
            "https://registry.example.test",
        ])
        .assert()
        .failure();
    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    let combined = format!("{stdout}{stderr}");
    assert!(
        combined.contains("registry token missing"),
        "expected missing token guidance, got stdout: {stdout:?}, stderr: {stderr:?}"
    );
    std::env::remove_var("AGENTCAROUSEL_HISTORY_DB");
}

#[test]
fn publish_dry_run_errors_when_no_matching_run_exists() {
    let _guard = HISTORY_ENV_LOCK.lock().expect("history lock");
    let (history_path, config_path, evidence_path) = temp_paths();
    fs::write(
        &config_path,
        format!(
            r#"[report]
history_db = "{}"
"#,
            history_path.display()
        ),
    )
    .expect("write config");
    fs::write(&evidence_path, b"fake").expect("write evidence");

    let root = workspace_root();
    let assert = Command::cargo_bin("agentcarousel")
        .unwrap()
        .current_dir(&root)
        .args([
            "publish",
            "--config",
            config_path.to_str().expect("config path"),
            "fixtures/github-actions-generator",
            "--url",
            "https://registry.example.test",
            "--dry-run",
            "--evidence",
            evidence_path.to_str().expect("evidence path"),
        ])
        .assert()
        .failure();
    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    let combined = format!("{stdout}{stderr}");
    assert!(
        combined.contains("no run found for bundle"),
        "expected run selection error, got stdout: {stdout:?}, stderr: {stderr:?}"
    );
}