visdom-cli 0.29.5

Visdom developer CLI: checks a project's Visdom wiring and launches the coding agent
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
//! Is this project's agent session going to reach AI Tracing?

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

use tracevault_cli::agent::Agent;
use tracevault_cli::commands::init::{has_tracevault_hooks, POST_COMMIT_MARKER, PRE_PUSH_MARKER};
use tracevault_cli::config::TracevaultConfig;

use super::{CheckContext, CredentialState};
use crate::finding::{Finding, Fix, FixAction, Severity};
use crate::mode::TracingMode;

const COMPONENT: &str = "tracing";

pub fn check(ctx: &CheckContext) -> Vec<Finding> {
    if ctx.mode == TracingMode::Gateway {
        return vec![Finding::new(
            COMPONENT,
            "tracing.mode",
            Severity::Warn,
            "this project traces through the AI Gateway, which this visdom version does not \
             support yet — upgrade visdom",
        )];
    }
    let mut findings = vec![auth(ctx)];
    let (Some(project_dir), Some(root)) = (&ctx.workdir.checkout, &ctx.workdir.primary) else {
        findings.push(Finding::new(
            COMPONENT,
            "tracing.repo",
            Severity::Info,
            "not in a repository: only sessions captured by a global install are traced here",
        ));
        return findings;
    };

    let global = global_hook_file(ctx);
    let global_installed = matches!(has_tracevault_hooks(&global), Ok(true));
    let init = || Fix {
        description: format!(
            "run `visdom trace init --agent {}` in {} (writes .tracevault/, the {} hooks and \
             the git hooks)",
            ctx.agent.tool_name(),
            root.display(),
            ctx.agent.label()
        ),
        action: FixAction::TracingInit {
            agent: ctx.agent,
            dir: root.clone(),
        },
    };

    let config = TracevaultConfig::try_load(root);
    let own_setup = !matches!(config, Ok(None));
    findings.push(if let Err(e) = &config {
        // It is there but the tracing library cannot use it; `init` refuses
        // to replace it, so there is nothing to offer.
        Finding::new(
            COMPONENT,
            "tracing.repo",
            Severity::Unknown,
            format!(
                "cannot use {} — {e}; fix or remove it",
                TracevaultConfig::config_path(root).display()
            ),
        )
    } else if own_setup {
        Finding::new(
            COMPONENT,
            "tracing.repo",
            Severity::Ok,
            format!("tracing is set up in {}", root.display()),
        )
    } else if global_installed {
        Finding::new(
            COMPONENT,
            "tracing.repo",
            Severity::Ok,
            "no .tracevault/ here: sessions are captured by the global install and bound to \
             the repository by its origin remote",
        )
    } else {
        Finding::new(
            COMPONENT,
            "tracing.repo",
            Severity::Error,
            format!("tracing is not set up in {}", root.display()),
        )
        .with_fix(init())
    });

    let hooks = agent_hooks(ctx, project_dir, root, &global);
    findings.push(if hooks.severity == Severity::Error {
        hooks.with_fix(init())
    } else {
        hooks
    });

    let git_hooks = missing_git_hooks(root);
    if let Err(unreadable) = &git_hooks {
        findings.push(Finding::new(
            COMPONENT,
            "tracing.git_hooks",
            Severity::Unknown,
            format!("could not tell whether the git hooks are installed — {unreadable}"),
        ));
    } else if let Ok(Some(missing)) = &git_hooks {
        let message = format!(
            "git {missing} hook{} not installed in {}: commits are not linked to sessions and \
             policies are not checked before a push",
            if missing.contains(" and ") { "s" } else { "" },
            root.join(".git/hooks").display()
        );
        findings.push(if global_installed && !own_setup {
            // The global install has no git hooks, by design: say so once,
            // without offering to turn the repository into a per-repo setup
            // at every launch.
            Finding::new(
                COMPONENT,
                "tracing.git_hooks",
                Severity::Info,
                format!(
                    "{message} (`visdom trace init` in {} adds them)",
                    root.display()
                ),
            )
        } else {
            Finding::new(COMPONENT, "tracing.git_hooks", Severity::Warn, message).with_fix(init())
        });
    } else {
        findings.push(Finding::new(
            COMPONENT,
            "tracing.git_hooks",
            Severity::Ok,
            "git hooks installed (pre-push, post-commit)",
        ));
    }
    findings
}

fn auth(ctx: &CheckContext) -> Finding {
    const ID: &str = "tracing.auth";
    match (&ctx.credential, &ctx.server_url) {
        (CredentialState::Present, Some(url)) => {
            Finding::new(COMPONENT, ID, Severity::Ok, format!("logged in to {url}"))
        }
        (CredentialState::Present, None) => {
            Finding::new(COMPONENT, ID, Severity::Ok, "credential found")
        }
        (CredentialState::Unreadable(path), _) => Finding::new(
            COMPONENT,
            ID,
            Severity::Unknown,
            format!("could not tell whether you are logged in — {path} cannot be read or parsed"),
        ),
        (CredentialState::Unusable(reason), _) => {
            Finding::new(COMPONENT, ID, Severity::Error, reason.clone())
        }
        (CredentialState::Missing, Some(url)) => Finding::new(
            COMPONENT,
            ID,
            Severity::Error,
            format!("not logged in to {url}"),
        )
        .with_fix(Fix {
            description: format!(
                "run `visdom login --server-url {url}` (signs you in through the browser)"
            ),
            action: FixAction::Login {
                server_url: url.clone(),
            },
        }),
        (CredentialState::Missing, None) => Finding::new(
            COMPONENT,
            ID,
            Severity::Error,
            "not logged in — run `visdom login --server-url <url>`",
        ),
    }
}

/// The user-level hook file of the agent, which applies to every session.
fn global_hook_file(ctx: &CheckContext) -> PathBuf {
    match ctx.agent {
        Agent::Codex => ctx.codex_home.join("hooks.json"),
        _ => ctx.home.join(".claude").join("settings.json"),
    }
}

/// Whether the agent's hooks are installed anywhere the agent loads them
/// from. An `Error` finding means they are not; the caller attaches the fix.
fn agent_hooks(ctx: &CheckContext, project_dir: &Path, root: &Path, global: &Path) -> Finding {
    const ID: &str = "tracing.agent_hooks";
    let names: &[&str] = match ctx.agent {
        Agent::Codex => &[".codex/hooks.json"],
        _ => &[".claude/settings.json", ".claude/settings.local.json"],
    };
    // The checkout the agent starts in, then the primary checkout (where
    // `init` writes them), then the global install.
    let mut files: Vec<PathBuf> = Vec::new();
    for dir in [project_dir, root] {
        for name in names {
            let file = dir.join(name);
            if !files.contains(&file) {
                files.push(file);
            }
        }
    }
    files.push(global.to_path_buf());

    let mut unreadable = Vec::new();
    for file in &files {
        match has_tracevault_hooks(file) {
            Ok(true) => {
                return Finding::new(
                    COMPONENT,
                    ID,
                    Severity::Ok,
                    format!("{} hooks installed ({})", ctx.agent.label(), file.display()),
                )
            }
            Ok(false) => {}
            Err(e) => unreadable.push(format!("{}: {e}", file.display())),
        }
    }
    if !unreadable.is_empty() {
        return Finding::new(
            COMPONENT,
            ID,
            Severity::Unknown,
            format!(
                "could not tell whether the {} hooks are installed — {}",
                ctx.agent.label(),
                unreadable.join("; ")
            ),
        );
    }
    Finding::new(
        COMPONENT,
        ID,
        Severity::Error,
        format!(
            "{} hooks are not installed: sessions in this project are not traced",
            ctx.agent.label()
        ),
    )
}

/// `"pre-push"`, `"post-commit"` or `"pre-push and post-commit"` — whichever
/// lacks the tracing block — or `None` when both have it. A hook file that
/// exists but cannot be read is an error naming it: it may well have the block.
fn missing_git_hooks(root: &Path) -> Result<Option<String>, String> {
    let hooks = root.join(".git").join("hooks");
    let mut missing: Vec<&str> = Vec::new();
    for (name, marker) in [
        ("pre-push", PRE_PUSH_MARKER),
        ("post-commit", POST_COMMIT_MARKER),
    ] {
        let path = hooks.join(name);
        match std::fs::read(&path) {
            Ok(bytes) if String::from_utf8_lossy(&bytes).contains(marker) => {}
            Ok(_) => missing.push(name),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => missing.push(name),
            Err(e) => return Err(format!("{}: {e}", path.display())),
        }
    }
    Ok((!missing.is_empty()).then(|| missing.join(" and ")))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::agents::Workdir;
    use std::fs;
    use tempfile::TempDir;

    struct Fixture {
        repo: TempDir,
        home: TempDir,
    }

    impl Fixture {
        fn new() -> Self {
            let repo = TempDir::new().unwrap();
            let status = std::process::Command::new("git")
                .args(["init", "-q"])
                .current_dir(repo.path())
                .status()
                .unwrap();
            assert!(status.success());
            Fixture {
                repo,
                home: TempDir::new().unwrap(),
            }
        }

        fn ctx(&self, agent: Agent) -> CheckContext {
            CheckContext {
                workdir: Workdir::checkout(self.repo.path()),
                home: self.home.path().to_path_buf(),
                codex_home: self.home.path().join(".codex"),
                agent,
                mode: TracingMode::Hooks,
                server_url: Some("https://tv.example.com".to_string()),
                credential: CredentialState::Present,
            }
        }

        fn write(&self, rel: &str, content: &str) {
            let path = self.repo.path().join(rel);
            fs::create_dir_all(path.parent().unwrap()).unwrap();
            fs::write(path, content).unwrap();
        }

        fn wire_config(&self) {
            self.write(".tracevault/config.toml", "server_url = \"https://tv\"\n");
        }

        fn wire_git_hooks(&self) {
            self.write(
                ".git/hooks/pre-push",
                &format!("#!/bin/sh\n{PRE_PUSH_MARKER}\n"),
            );
            self.write(
                ".git/hooks/post-commit",
                &format!("#!/bin/sh\n{POST_COMMIT_MARKER}\n"),
            );
        }
    }

    const CLAUDE_HOOKS: &str = r#"{"hooks":{"Stop":[{"matcher":"","hooks":[{"type":"command","command":"tracevault stream --event stop"}]}]}}"#;
    const CODEX_HOOKS: &str = r#"{"hooks":{"Stop":[{"matcher":"","hooks":[{"type":"command","command":"tracevault stream --event stop --agent codex"}]}]}}"#;

    fn by_id<'a>(findings: &'a [Finding], id: &str) -> &'a Finding {
        findings
            .iter()
            .find(|f| f.id == id)
            .unwrap_or_else(|| panic!("no finding {id} in {findings:?}"))
    }

    fn is_init(finding: &Finding, agent: Agent, dir: &Path) -> bool {
        matches!(&finding.fix, Some(Fix { action: FixAction::TracingInit { agent: a, dir: d }, .. })
            if *a == agent && d == dir)
    }

    #[test]
    fn fully_wired_repo_is_all_ok() {
        let fx = Fixture::new();
        fx.wire_config();
        fx.wire_git_hooks();
        fx.write(".claude/settings.local.json", CLAUDE_HOOKS);
        let findings = check(&fx.ctx(Agent::ClaudeCode));
        assert_eq!(findings.len(), 4, "{findings:?}");
        for finding in &findings {
            assert_eq!(finding.severity, Severity::Ok, "{finding:?}");
            assert!(finding.fix.is_none());
        }
    }

    #[test]
    fn outside_repo_is_info_without_fix() {
        let fx = Fixture::new();
        let mut ctx = fx.ctx(Agent::ClaudeCode);
        ctx.workdir = Workdir::plain(fx.home.path());
        let findings = check(&ctx);
        let ids: Vec<&str> = findings.iter().map(|f| f.id).collect();
        assert_eq!(ids, ["tracing.auth", "tracing.repo"]);
        let repo = by_id(&findings, "tracing.repo");
        assert_eq!(repo.severity, Severity::Info);
        assert!(repo.message.contains("not in a repository"));
        assert!(findings.iter().all(|f| f.fix.is_none()));
    }

    #[test]
    fn missing_agent_hooks_offers_init() {
        let fx = Fixture::new();
        fx.wire_config();
        fx.wire_git_hooks();
        let findings = check(&fx.ctx(Agent::ClaudeCode));
        let hooks = by_id(&findings, "tracing.agent_hooks");
        assert_eq!(hooks.severity, Severity::Error);
        assert!(
            is_init(hooks, Agent::ClaudeCode, fx.repo.path()),
            "{hooks:?}"
        );
        assert!(hooks
            .fix
            .as_ref()
            .unwrap()
            .description
            .contains("visdom trace init --agent claude-code"));
        assert_eq!(by_id(&findings, "tracing.repo").severity, Severity::Ok);
    }

    #[test]
    fn nothing_wired_offers_one_shared_init() {
        let fx = Fixture::new();
        let findings = check(&fx.ctx(Agent::ClaudeCode));
        let fixes: Vec<&FixAction> = findings
            .iter()
            .filter_map(|f| f.fix.as_ref().map(|fix| &fix.action))
            .collect();
        assert_eq!(fixes.len(), 3, "repo, agent hooks, git hooks: {findings:?}");
        assert!(fixes.iter().all(|a| *a == fixes[0]), "one init fixes all");
    }

    #[test]
    fn global_claude_hooks_satisfy_agent_hooks() {
        let fx = Fixture::new();
        fs::create_dir_all(fx.home.path().join(".claude")).unwrap();
        fs::write(fx.home.path().join(".claude/settings.json"), CLAUDE_HOOKS).unwrap();
        let findings = check(&fx.ctx(Agent::ClaudeCode));
        assert_eq!(
            by_id(&findings, "tracing.agent_hooks").severity,
            Severity::Ok
        );
        // With a global install a repo needs no .tracevault/ of its own.
        let repo = by_id(&findings, "tracing.repo");
        assert_eq!(repo.severity, Severity::Ok);
        assert!(repo.message.contains("global install"), "{repo:?}");
        // The global install has no git hooks: worth knowing, nothing to fix.
        let git = by_id(&findings, "tracing.git_hooks");
        assert_eq!(git.severity, Severity::Info);
        assert!(git.fix.is_none());
        assert!(findings.iter().all(|f| f.fix.is_none()), "{findings:?}");
    }

    #[test]
    fn codex_checks_codex_files_not_claude() {
        let fx = Fixture::new();
        fx.wire_config();
        fx.write(".claude/settings.json", CLAUDE_HOOKS);
        let findings = check(&fx.ctx(Agent::Codex));
        let hooks = by_id(&findings, "tracing.agent_hooks");
        assert_eq!(hooks.severity, Severity::Error);
        assert!(is_init(hooks, Agent::Codex, fx.repo.path()));

        fx.write(".codex/hooks.json", CODEX_HOOKS);
        let findings = check(&fx.ctx(Agent::Codex));
        assert_eq!(
            by_id(&findings, "tracing.agent_hooks").severity,
            Severity::Ok
        );

        // And the other way round: Codex hooks do not serve Claude Code.
        let other = Fixture::new();
        other.write(".codex/hooks.json", CODEX_HOOKS);
        let findings = check(&other.ctx(Agent::ClaudeCode));
        assert_eq!(
            by_id(&findings, "tracing.agent_hooks").severity,
            Severity::Error
        );
    }

    #[test]
    fn global_codex_hooks_are_read_from_codex_home() {
        let fx = Fixture::new();
        fs::create_dir_all(fx.home.path().join(".codex")).unwrap();
        fs::write(fx.home.path().join(".codex/hooks.json"), CODEX_HOOKS).unwrap();
        let findings = check(&fx.ctx(Agent::Codex));
        assert_eq!(
            by_id(&findings, "tracing.agent_hooks").severity,
            Severity::Ok
        );
    }

    #[test]
    fn unreadable_hook_file_is_unknown_without_fix() {
        let fx = Fixture::new();
        fx.wire_config();
        fx.write(".claude/settings.json", "{ not json");
        let findings = check(&fx.ctx(Agent::ClaudeCode));
        let hooks = by_id(&findings, "tracing.agent_hooks");
        assert_eq!(hooks.severity, Severity::Unknown);
        assert!(hooks.fix.is_none());
        assert!(hooks.message.contains("settings.json"), "{hooks:?}");
    }

    #[test]
    fn missing_git_hooks_is_warn() {
        let fx = Fixture::new();
        fx.wire_config();
        fx.write(".claude/settings.json", CLAUDE_HOOKS);
        let findings = check(&fx.ctx(Agent::ClaudeCode));
        let git = by_id(&findings, "tracing.git_hooks");
        assert_eq!(git.severity, Severity::Warn);
        assert!(
            git.message.contains("pre-push and post-commit hooks"),
            "{git:?}"
        );
        assert!(is_init(git, Agent::ClaudeCode, fx.repo.path()));

        // A hook file without the tracing block does not count.
        fx.write(".git/hooks/pre-push", "#!/bin/sh\nexit 0\n");
        fx.write(
            ".git/hooks/post-commit",
            &format!("#!/bin/sh\n{POST_COMMIT_MARKER}\n"),
        );
        let findings = check(&fx.ctx(Agent::ClaudeCode));
        let git = by_id(&findings, "tracing.git_hooks");
        assert!(git.message.contains("git pre-push hook not"), "{git:?}");
    }

    #[test]
    fn malformed_tracing_config_is_unknown_without_fix() {
        let fx = Fixture::new();
        fx.wire_git_hooks();
        fx.write(".claude/settings.json", CLAUDE_HOOKS);
        fx.write(".tracevault/config.toml", "server_url = [truncated");
        let findings = check(&fx.ctx(Agent::ClaudeCode));
        let repo = by_id(&findings, "tracing.repo");
        assert_eq!(repo.severity, Severity::Unknown, "{repo:?}");
        assert!(repo.fix.is_none());
        assert!(repo.message.contains("config.toml"), "{repo:?}");
    }

    #[test]
    fn unreadable_git_hook_is_unknown_without_fix() {
        let fx = Fixture::new();
        fx.wire_config();
        fx.wire_git_hooks();
        fx.write(".claude/settings.json", CLAUDE_HOOKS);
        // A directory where the hook file should be: it exists, and reading
        // it fails (for every user, root included).
        fs::remove_file(fx.repo.path().join(".git/hooks/pre-push")).unwrap();
        fs::create_dir(fx.repo.path().join(".git/hooks/pre-push")).unwrap();
        let findings = check(&fx.ctx(Agent::ClaudeCode));
        let git = by_id(&findings, "tracing.git_hooks");
        assert_eq!(git.severity, Severity::Unknown, "{git:?}");
        assert!(git.fix.is_none());
        assert!(git.message.contains("pre-push"), "{git:?}");

        // Not UTF-8 is still readable: the marker is found in it.
        fs::remove_dir(fx.repo.path().join(".git/hooks/pre-push")).unwrap();
        let mut bytes = vec![0xff, 0xfe, b'\n'];
        bytes.extend_from_slice(PRE_PUSH_MARKER.as_bytes());
        fs::write(fx.repo.path().join(".git/hooks/pre-push"), bytes).unwrap();
        let findings = check(&fx.ctx(Agent::ClaudeCode));
        assert_eq!(by_id(&findings, "tracing.git_hooks").severity, Severity::Ok);
    }

    /// In a linked worktree the tracing state belongs to the primary checkout:
    /// that is where it is looked for and where `init` is offered.
    #[test]
    fn linked_worktree_checks_the_primary_checkout() {
        let fx = Fixture::new();
        let worktree = TempDir::new().unwrap();
        let mut ctx = fx.ctx(Agent::ClaudeCode);
        ctx.workdir = Workdir {
            start_dir: worktree.path().to_path_buf(),
            checkout: Some(worktree.path().to_path_buf()),
            primary: Some(fx.repo.path().to_path_buf()),
        };

        let findings = check(&ctx);
        let hooks = by_id(&findings, "tracing.agent_hooks");
        assert!(
            is_init(hooks, Agent::ClaudeCode, fx.repo.path()),
            "{hooks:?}"
        );

        fx.wire_config();
        fx.wire_git_hooks();
        fx.write(".claude/settings.local.json", CLAUDE_HOOKS);
        let findings = check(&ctx);
        assert!(
            findings.iter().all(|f| f.severity == Severity::Ok),
            "{findings:?}"
        );
    }

    #[test]
    fn no_credentials_is_error_with_login_fix() {
        let fx = Fixture::new();
        let mut ctx = fx.ctx(Agent::ClaudeCode);
        ctx.credential = CredentialState::Missing;
        let findings = check(&ctx);
        let auth = by_id(&findings, "tracing.auth");
        assert_eq!(auth.severity, Severity::Error);
        assert_eq!(
            auth.fix.as_ref().map(|f| &f.action),
            Some(&FixAction::Login {
                server_url: "https://tv.example.com".to_string()
            })
        );

        // Without a known server there is nothing to run, only to say.
        ctx.server_url = None;
        let findings = check(&ctx);
        let auth = by_id(&findings, "tracing.auth");
        assert_eq!(auth.severity, Severity::Error);
        assert!(auth.fix.is_none());
        assert!(auth.message.contains("visdom login --server-url <url>"));
    }

    #[test]
    fn unreadable_credential_file_is_unknown_without_login_fix() {
        let fx = Fixture::new();
        let mut ctx = fx.ctx(Agent::ClaudeCode);
        ctx.credential = CredentialState::Unreadable("/cfg/tracevault/credentials.json".into());
        let findings = check(&ctx);
        let auth = by_id(&findings, "tracing.auth");
        assert_eq!(auth.severity, Severity::Unknown);
        assert!(auth.fix.is_none());
        assert!(auth.message.contains("credentials.json"), "{auth:?}");
    }

    #[test]
    fn unusable_credential_is_error_with_its_reason() {
        let fx = Fixture::new();
        let mut ctx = fx.ctx(Agent::ClaudeCode);
        ctx.credential = CredentialState::Unusable("logged in to another server".to_string());
        let findings = check(&ctx);
        let auth = by_id(&findings, "tracing.auth");
        assert_eq!(auth.severity, Severity::Error);
        assert_eq!(auth.message, "logged in to another server");
        assert!(auth.fix.is_none());
    }

    /// An API key from the environment is a credential like any other: the
    /// wiring reports it as `Present`, and the check asks for no login.
    #[test]
    fn api_key_env_counts_as_credential() {
        let fx = Fixture::new();
        let mut ctx = fx.ctx(Agent::ClaudeCode);
        ctx.credential = CredentialState::Present;
        ctx.server_url = None;
        let findings = check(&ctx);
        let auth = by_id(&findings, "tracing.auth");
        assert_eq!(auth.severity, Severity::Ok);
        assert!(auth.fix.is_none());
    }

    #[test]
    fn gateway_mode_is_reported_unsupported() {
        let fx = Fixture::new();
        let mut ctx = fx.ctx(Agent::ClaudeCode);
        ctx.mode = TracingMode::Gateway;
        let findings = check(&ctx);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].id, "tracing.mode");
        assert_eq!(findings[0].severity, Severity::Warn);
        assert!(findings[0].fix.is_none());
    }
}