leviath-cli 0.3.10

Command-line interface for Leviath agent framework
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
//! Tests for `lev doctor`.
//!
//! Every test that reaches `Config::load()`, the runs directory, or the data
//! root goes through [`with_env`], which redirects all three at once. No test
//! can make a billed call: the provider registry is always injected, and the
//! isolation clears every provider key from the environment anyway.

use super::*;

use leviath_providers::{
    FinishReason, InferenceResponse, ModelCapabilities, ProviderError, TokenUsage,
};
use leviath_runtime::components::AgentStatus;
use leviath_runtime::control_socket::{ControlId, bind_control_listener, control_id};
use std::path::{Path, PathBuf};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::task::JoinHandle;

// ─── Harness ──────────────────────────────────────────────────────────────────

/// Run `f` with the config path, data root, and runs directory all redirected
/// into one fresh temp root, and every provider key cleared.
///
/// One `temp_env` call, not two: it serializes process-wide and holds its lock
/// across the future, so nesting `with_isolated_config_path_async` inside a
/// second call would deadlock.
async fn with_env<R, Fut>(f: impl FnOnce(PathBuf) -> Fut) -> R
where
    Fut: std::future::Future<Output = R>,
{
    let dir = tempfile::tempdir().expect("a temp dir");
    let root = dir.path().to_path_buf();
    let mut vars = crate::config::config_isolation_vars(&root);
    vars.push(("LEVIATH_HOME", Some(root.clone().into_os_string())));
    vars.push(("LEVIATH_RUNS_DIR", Some(root.join("runs").into_os_string())));
    temp_env::async_with_vars(vars, f(root)).await
}

/// A provider whose single inference is decided up front.
struct StubProvider {
    reply: Result<String, ProviderError>,
}

impl StubProvider {
    fn replying(content: &str) -> Arc<dyn Provider> {
        Arc::new(Self {
            reply: Ok(content.to_string()),
        })
    }

    fn failing(message: &str) -> Arc<dyn Provider> {
        Arc::new(Self {
            reply: Err(ProviderError::ApiError(message.to_string())),
        })
    }
}

#[async_trait::async_trait]
impl Provider for StubProvider {
    async fn infer(
        &self,
        _request: &InferenceRequest,
    ) -> leviath_providers::Result<InferenceResponse> {
        match &self.reply {
            Ok(content) => Ok(InferenceResponse {
                content: content.clone(),
                tool_calls: Vec::new(),
                tokens_used: TokenUsage {
                    prompt_tokens: 12,
                    completion_tokens: 4,
                    total_tokens: 16,
                    cached_tokens: 0,
                    cache_write_tokens: 0,
                },
                finish_reason: FinishReason::Complete,
            }),
            Err(e) => Err(ProviderError::ApiError(e.to_string())),
        }
    }

    async fn count_tokens(&self, text: &str, _model: &str) -> usize {
        text.len()
    }

    fn max_context_tokens(&self, _model: &str) -> usize {
        8192
    }

    fn name(&self) -> &str {
        "stub"
    }

    fn capabilities(&self, _model: &str) -> ModelCapabilities {
        ModelCapabilities::default()
    }
}

/// A registry holding one stub under `name`.
fn registry_with(name: &str, provider: Arc<dyn Provider>) -> ProviderRegistry {
    let mut registry = ProviderRegistry::new();
    registry.register(name.to_string(), provider);
    registry
}

/// Bind a control listener under `dir` and serve `responses` one per incoming
/// connection - the client opens a fresh one per request. Once they run out the
/// listener drops, so a further request fails to connect, which is how the
/// "lost contact" path is driven.
fn scripted_daemon(dir: &Path, responses: Vec<String>) -> (ControlId, JoinHandle<()>) {
    let id = control_id(dir);
    let mut listener = bind_control_listener(&id).expect("bind a fresh control socket");
    let handle = tokio::spawn(async move {
        for line in responses {
            let Ok(Some(stream)) = listener.accept().await else {
                return;
            };
            let (read_half, mut write_half) = tokio::io::split(stream);
            let mut lines = BufReader::new(read_half).lines();
            let _request = lines.next_line().await;
            let _ = write_half.write_all(line.as_bytes()).await;
            let _ = write_half.write_all(b"\n").await;
        }
    });
    (id, handle)
}

const SPAWNED: &str = r#"{"result":"spawned","run_id":"doctor-1-abc"}"#;
const COMPLETE: &str = r#"{"result":"status","status":"Complete"}"#;
const ACTIVE: &str = r#"{"result":"status","status":"Active"}"#;

/// Stage a canary manifest under `root` and hand back its path, for the tests
/// that drive `spawn_and_wait` directly.
fn staged(root: &Path) -> PathBuf {
    stage_canary(root, "stub", "m").expect("staging into a fresh temp dir succeeds")
}

/// Write a `meta.json` for `run_id` so the on-disk fallbacks have something to
/// read - one iteration in, and whatever terminal state the test is after.
fn write_meta(run_id: &str, status: leviath_core::run_meta::RunStatus, error: Option<&str>) {
    let dir = crate::runstate::run_dir(run_id);
    std::fs::create_dir_all(&dir).expect("create the run dir");
    let mut meta = leviath_core::run_meta::RunMeta::new(
        run_id.to_string(),
        "doctor".to_string(),
        String::new(),
        PROBE_PROMPT.to_string(),
        None,
        String::new(),
        1,
    );
    meta.status = status;
    meta.error = error.map(str::to_string);
    meta.iteration = 1;
    std::fs::write(
        dir.join("meta.json"),
        serde_json::to_string(&meta).expect("RunMeta serializes"),
    )
    .expect("write meta.json");
}

// ─── format_report ────────────────────────────────────────────────────────────

#[test]
fn format_report_of_nothing_is_a_pass() {
    // Degenerate, but the width computation has to survive it.
    assert!(format_report(&[]).contains("doctor passed"));
}

#[test]
fn format_report_renders_timings_and_the_pass_line() {
    let checks = vec![
        Check::ok("config", "default_provider=stub"),
        Check::ok("inference", "12 in / 4 out / 16 total, replied PONG")
            .timed(Duration::from_millis(1234)),
    ];
    let out = format_report(&checks);
    assert!(out.contains("config     OK"), "columns pad: {out}");
    assert!(out.contains("(1.2s)"), "timing rendered: {out}");
    assert!(out.ends_with("doctor passed\n"), "{out}");
}

#[test]
fn format_report_of_a_failure_has_no_pass_line() {
    let checks = vec![
        Check::ok("config", "fine"),
        Check::fail("resolve", "resolved to 'nope'"),
    ];
    let out = format_report(&checks);
    assert!(out.contains("FAIL"), "{out}");
    assert!(!out.contains("doctor passed"), "{out}");
}

// ─── config_check ─────────────────────────────────────────────────────────────

#[test]
fn config_check_lists_registered_providers_sorted() {
    let config = Config::default();
    let mut registry = registry_with("openrouter", StubProvider::replying("hi"));
    registry.register("anthropic".to_string(), StubProvider::replying("hi"));
    let check = config_check(&config, &registry);
    assert_eq!(check.status, CheckStatus::Ok);
    assert!(
        check.detail.contains("registered: anthropic, openrouter"),
        "got: {}",
        check.detail
    );
}

#[test]
fn config_check_says_none_when_nothing_is_registered() {
    let check = config_check(&Config::default(), &ProviderRegistry::new());
    assert!(
        check.detail.contains("registered: none"),
        "{}",
        check.detail
    );
}

/// A `[rate_limits]` entry naming no provider throttles nothing, and the
/// unknown-key check cannot see it: the table takes arbitrary keys, so the
/// typo deserializes perfectly.
#[test]
fn config_check_names_a_rate_limit_for_a_provider_that_does_not_exist() {
    let mut config = Config::default();
    let limit = leviath_providers::RateLimitConfig {
        requests_per_minute: 10,
        tokens_per_minute: 1000,
    };
    config
        .rate_limits
        .insert("anthropc".to_string(), limit.clone());
    // The control: the correctly spelled one beside it must stay quiet, or the
    // note would just be reporting every rate limit anyone set.
    config.rate_limits.insert("anthropic".to_string(), limit);

    let check = config_check(&config, &ProviderRegistry::new());
    assert_eq!(check.status, CheckStatus::Ok, "not broken wiring");
    assert!(
        check.detail.contains("rate_limits.anthropc"),
        "got: {}",
        check.detail
    );
    assert!(
        !check.detail.contains("rate_limits.anthropic,")
            && !check.detail.ends_with("rate_limits.anthropic"),
        "the real provider must not be reported: {}",
        check.detail
    );
}

/// Two of them read as two, not as "1 key".
#[test]
fn config_check_counts_more_than_one_unread_key() {
    let mut config = Config::default();
    for name in ["anthropc", "opennai"] {
        config.rate_limits.insert(
            name.to_string(),
            leviath_providers::RateLimitConfig {
                requests_per_minute: 10,
                tokens_per_minute: 1000,
            },
        );
    }
    let check = config_check(&config, &ProviderRegistry::new());
    assert!(
        check
            .detail
            .contains("2 keys in config.toml are read by nothing"),
        "got: {}",
        check.detail
    );
}

#[test]
fn config_check_is_quiet_when_every_rate_limit_names_a_real_provider() {
    let mut config = Config::default();
    config.rate_limits.insert(
        "openrouter".to_string(),
        leviath_providers::RateLimitConfig {
            requests_per_minute: 10,
            tokens_per_minute: 1000,
        },
    );
    let check = config_check(&config, &ProviderRegistry::new());
    assert!(
        !check.detail.contains("read by nothing"),
        "got: {}",
        check.detail
    );
}

// ─── resolve_check ────────────────────────────────────────────────────────────

#[test]
fn resolve_check_uses_the_configured_default() {
    let config = Config {
        default_provider: "stub".to_string(),
        default_model: Some("m-1".to_string()),
        ..Config::default()
    };
    let registry = registry_with("stub", StubProvider::replying("hi"));
    let (check, resolved) = resolve_check(&config, None, &registry);
    assert_eq!(check.status, CheckStatus::Ok);
    assert_eq!(check.detail, "stub / m-1");
    let resolved = resolved.expect("a registered provider resolves");
    assert_eq!(
        (resolved.provider_name.as_str(), resolved.model.as_str()),
        ("stub", "m-1")
    );
}

#[test]
fn resolve_check_honours_a_provider_slash_model_override() {
    let config = Config {
        default_provider: "anthropic".to_string(),
        ..Config::default()
    };
    let registry = registry_with("stub", StubProvider::replying("hi"));
    let (check, resolved) = resolve_check(&config, Some("stub/m-2"), &registry);
    assert_eq!(check.detail, "stub / m-2");
    assert!(resolved.is_some());
}

#[test]
fn resolve_check_reports_an_unconfigured_provider_and_what_was_tried() {
    // The exact shape of the incident this command exists for: nothing is
    // registered, so the chain falls through to its hard-coded last resort.
    let config = Config {
        default_provider: "openrouter".to_string(),
        ..Config::default()
    };
    let (check, resolved) = resolve_check(&config, None, &ProviderRegistry::new());
    assert_eq!(check.status, CheckStatus::Fail);
    assert!(check.detail.contains("not configured"), "{}", check.detail);
    assert!(check.detail.contains("openrouter"), "{}", check.detail);
    assert!(resolved.is_none());
}

#[test]
fn resolve_check_says_so_when_the_configured_default_never_wins() {
    // `default_provider = "openrouter"` with no `default_model`: the resolver
    // has no model to send, falls through to its hard-coded last resort, and
    // used to report `resolve OK anthropic / claude-sonnet-4-6` with nothing
    // anywhere saying the configured provider had been passed over.
    let config = Config {
        default_provider: "openrouter".to_string(),
        default_model: None,
        ..Config::default()
    };
    let mut registry = registry_with("openrouter", StubProvider::replying("hi"));
    registry.register("anthropic".to_string(), StubProvider::replying("hi"));
    let (check, _) = resolve_check(&config, None, &registry);
    assert_eq!(check.status, CheckStatus::Ok);
    assert!(
        check.detail.contains("no default_model"),
        "{}",
        check.detail
    );
    assert!(check.detail.contains("openrouter"), "{}", check.detail);
}

#[test]
fn resolve_check_is_quiet_when_the_configured_default_does_win() {
    let config = Config {
        default_provider: "openrouter".to_string(),
        default_model: Some("openai/gpt-4o-mini".to_string()),
        ..Config::default()
    };
    let registry = registry_with("openrouter", StubProvider::replying("hi"));
    let (check, _) = resolve_check(&config, None, &registry);
    assert_eq!(check.detail, "openrouter / openai/gpt-4o-mini");
}

#[test]
fn resolve_check_does_not_second_guess_an_unregistered_default_provider() {
    // Landing somewhere other than a default provider that has no key is not
    // news - the `config` line already lists what is registered, and this
    // check's fail arm covers the case where nothing usable is left. Saying it
    // again here would put a note on every install with a stale provider name
    // in its config.
    for default_model in [None, Some("m-1".to_string())] {
        let config = Config {
            default_provider: "ghost".to_string(),
            default_model,
            ..Config::default()
        };
        let registry = registry_with("anthropic", StubProvider::replying("hi"));
        let (check, _) = resolve_check(&config, None, &registry);
        assert_eq!(check.status, CheckStatus::Ok);
        assert!(!check.detail.contains("note:"), "{}", check.detail);
    }
}

#[test]
fn resolve_check_stays_quiet_under_an_explicit_model_override() {
    // `--model` is the caller overriding on purpose; telling them their
    // default was passed over is noise.
    let config = Config {
        default_provider: "openrouter".to_string(),
        default_model: None,
        ..Config::default()
    };
    let registry = registry_with("stub", StubProvider::replying("hi"));
    let (check, _) = resolve_check(&config, Some("stub/m-2"), &registry);
    assert_eq!(check.detail, "stub / m-2");
}

// ─── inference_check ──────────────────────────────────────────────────────────

#[tokio::test]
async fn inference_check_reports_usage_and_the_echo() {
    let provider = StubProvider::replying("PONG");
    let check = inference_check(provider.as_ref(), "m").await;
    assert_eq!(check.status, CheckStatus::Ok);
    assert!(
        check
            .detail
            .contains("12 in / 4 out / 16 total, replied PONG"),
        "got: {}",
        check.detail
    );
    assert!(check.elapsed_ms.is_some());
}

#[tokio::test]
async fn inference_check_passes_even_without_the_expected_word() {
    // The call is what is being checked. What the model chose to say is a note,
    // not a verdict - otherwise this command would be flaky across providers.
    let provider = StubProvider::replying("Sure! Hello there.");
    let check = inference_check(provider.as_ref(), "m").await;
    assert_eq!(check.status, CheckStatus::Ok);
    assert!(
        check.detail.contains("no PONG in the reply"),
        "{}",
        check.detail
    );
}

#[tokio::test]
async fn inference_check_reports_the_provider_error_verbatim() {
    let raw = r#"HTTP 402 Payment Required: {"error":{"message":"credit balance too low"}}"#;
    let provider = StubProvider::failing(raw);
    let check = inference_check(provider.as_ref(), "m").await;
    assert_eq!(check.status, CheckStatus::Fail);
    assert!(
        check.detail.contains("credit balance too low") && check.detail.contains("402"),
        "the body is the diagnosis and must survive: {}",
        check.detail
    );
}

// ─── The canary blueprint ─────────────────────────────────────────────────────

#[test]
fn the_canary_manifest_is_a_valid_blueprint() {
    let manifest = canary_manifest("openrouter", "anthropic/claude-sonnet-4.5");
    let blueprint =
        leviath_core::manifest::parse_manifest(&manifest).expect("the canary manifest parses");
    blueprint.validate().expect("the canary manifest validates");
    assert_eq!(blueprint.stages.len(), 1, "one stage, one turn");
    let stage = &blueprint.stages[0];
    assert_eq!(stage.max_iterations, Some(1));
    assert!(
        stage.available_tools.is_empty(),
        "a probe with a file tool would be judged for not having used it"
    );
    assert!(stage.transitions.is_none(), "nothing to transition to");
    assert_eq!(stage.model.provider(), "openrouter");
    assert_eq!(stage.model.model(), "anthropic/claude-sonnet-4.5");
}

#[test]
fn the_canary_manifest_escapes_hostile_names() {
    // Provider names come from config, and a quote in one must not be able to
    // close the TOML literal and inject the rest of the file.
    let manifest = canary_manifest("ev\"il", "m\\1");
    let blueprint =
        leviath_core::manifest::parse_manifest(&manifest).expect("an escaped name still parses");
    assert_eq!(blueprint.stages[0].model.provider(), "ev\"il");
    assert_eq!(blueprint.stages[0].model.model(), "m\\1");
}

#[test]
fn stage_canary_reports_a_root_it_cannot_write_into() {
    let dir = tempfile::tempdir().expect("a temp dir");
    let blocker = dir.path().join("not-a-dir");
    std::fs::write(&blocker, b"i am a file").expect("write the blocker");
    let err = stage_canary(&blocker, "stub", "m").expect_err("a file is not a directory");
    assert!(!err.to_string().is_empty());
}

#[test]
fn stage_canary_reports_a_manifest_it_cannot_write() {
    // The directory is fine; the manifest path itself is occupied by a
    // directory, so the write is what fails.
    let dir = tempfile::tempdir().expect("a temp dir");
    std::fs::create_dir_all(dir.path().join("doctor").join("agent.leviath"))
        .expect("occupy the manifest path");
    let err = stage_canary(dir.path(), "stub", "m").expect_err("cannot write over a directory");
    assert!(!err.to_string().is_empty());
}

// ─── Terminal-status reporting ────────────────────────────────────────────────

#[tokio::test]
async fn finished_reads_the_iteration_count_off_disk() {
    with_env(|_root| async {
        write_meta("r-ok", leviath_core::run_meta::RunStatus::Complete, None);
        let DaemonOutcome::Complete(detail) = finished("r-ok", &AgentStatus::Complete) else {
            panic!("a Complete run reports complete");
        };
        assert!(detail.contains("1 iteration(s)"), "{detail}");
    })
    .await;
}

#[tokio::test]
async fn finished_reports_an_errored_run() {
    with_env(|_root| async {
        let outcome = finished(
            "r-err",
            &AgentStatus::Error {
                message: "no usable provider".to_string(),
            },
        );
        let DaemonOutcome::Failed(detail) = outcome else {
            panic!("an errored run fails the check");
        };
        assert!(detail.contains("no usable provider"), "{detail}");
    })
    .await;
}

#[tokio::test]
async fn finished_reports_any_other_terminal_status() {
    with_env(|_root| async {
        let DaemonOutcome::Failed(detail) = finished("r-x", &AgentStatus::Cancelled) else {
            panic!("a cancelled probe is not a pass");
        };
        assert!(detail.contains("cancelled"), "{detail}");
    })
    .await;
}

#[tokio::test]
async fn reaped_falls_back_to_a_finished_meta() {
    with_env(|_root| async {
        write_meta(
            "r-reaped",
            leviath_core::run_meta::RunStatus::Complete,
            None,
        );
        let DaemonOutcome::Complete(detail) = reaped("r-reaped") else {
            panic!("a completed run on disk is a pass");
        };
        assert!(detail.contains("Complete after 1 iteration(s)"), "{detail}");
    })
    .await;
}

#[tokio::test]
async fn reaped_surfaces_a_recorded_error() {
    with_env(|_root| async {
        write_meta(
            "r-bad",
            leviath_core::run_meta::RunStatus::Error,
            Some("provider refused"),
        );
        let DaemonOutcome::Failed(detail) = reaped("r-bad") else {
            panic!("a recorded error fails the check");
        };
        assert!(detail.contains("provider refused"), "{detail}");
    })
    .await;
}

#[tokio::test]
async fn reaped_treats_an_unfinished_run_as_a_lost_one() {
    with_env(|_root| async {
        write_meta("r-live", leviath_core::run_meta::RunStatus::Running, None);
        let DaemonOutcome::Failed(detail) = reaped("r-live") else {
            panic!("a run the daemon forgot mid-flight is a handoff failure");
        };
        assert!(detail.contains("vanished"), "{detail}");
    })
    .await;
}

#[tokio::test]
async fn reaped_treats_a_run_with_no_meta_as_a_lost_one() {
    with_env(|_root| async {
        let DaemonOutcome::Failed(detail) = reaped("r-never") else {
            panic!("a run with nothing on disk is a handoff failure");
        };
        assert!(detail.contains("vanished"), "{detail}");
    })
    .await;
}

// ─── wait_for_run ─────────────────────────────────────────────────────────────

/// Drive `wait_for_run` against a scripted daemon.
async fn wait_against(responses: Vec<&str>, timeout: Duration) -> DaemonOutcome {
    let responses: Vec<String> = responses.into_iter().map(str::to_string).collect();
    with_env(|root| async move {
        let (id, _server) = scripted_daemon(&root, responses);
        wait_for_run(
            &ControlClient::new(id),
            "r-wait",
            timeout,
            Duration::from_millis(1),
        )
        .await
    })
    .await
}

#[tokio::test]
async fn wait_for_run_returns_as_soon_as_the_run_is_terminal() {
    let DaemonOutcome::Complete(detail) =
        wait_against(vec![COMPLETE], Duration::from_secs(5)).await
    else {
        panic!("a Complete status ends the wait");
    };
    assert!(detail.contains("r-wait"), "{detail}");
}

#[tokio::test]
async fn wait_for_run_polls_until_the_run_finishes() {
    let outcome = wait_against(vec![ACTIVE, ACTIVE, COMPLETE], Duration::from_secs(5)).await;
    assert!(matches!(outcome, DaemonOutcome::Complete(_)));
}

#[tokio::test]
async fn wait_for_run_gives_up_on_a_run_that_never_moves() {
    // A zero deadline means the very first poll is also the last one.
    let DaemonOutcome::Failed(detail) = wait_against(vec![ACTIVE], Duration::ZERO).await else {
        panic!("a run that never leaves 'active' is a wedged handoff");
    };
    assert!(detail.contains("still 'active'"), "{detail}");
}

#[tokio::test]
async fn wait_for_run_falls_back_to_disk_when_the_run_is_reaped() {
    let outcome = wait_against(
        vec![r#"{"result":"status","status":null}"#],
        Duration::from_secs(5),
    )
    .await;
    // Nothing was written for `r-wait`, so the fallback reports it lost.
    let DaemonOutcome::Failed(detail) = outcome else {
        panic!("an unknown run with no meta is a failure");
    };
    assert!(detail.contains("vanished"), "{detail}");
}

#[tokio::test]
async fn wait_for_run_rejects_an_unexpected_response() {
    let DaemonOutcome::Failed(detail) =
        wait_against(vec![r#"{"result":"ok","ok":true}"#], Duration::from_secs(5)).await
    else {
        panic!("a nonsense reply is a failure");
    };
    assert!(detail.contains("unexpected daemon response"), "{detail}");
}

#[tokio::test]
async fn wait_for_run_reports_a_lost_connection() {
    // No scripted responses: the listener is already gone when we connect.
    let DaemonOutcome::Failed(detail) = wait_against(vec![], Duration::from_secs(5)).await else {
        panic!("an unreachable daemon is a failure");
    };
    assert!(detail.contains("lost contact"), "{detail}");
}

// ─── spawn_and_wait ───────────────────────────────────────────────────────────

/// Drive `spawn_and_wait` against a scripted daemon, with the canary staged
/// into the isolated root.
async fn spawn_against(responses: Vec<&str>) -> DaemonOutcome {
    let responses: Vec<String> = responses.into_iter().map(str::to_string).collect();
    with_env(|root| async move {
        let (id, _server) = scripted_daemon(&root, responses);
        let manifest = staged(&root);
        spawn_and_wait(
            &ControlClient::new(id),
            &manifest,
            &root,
            Duration::from_secs(5),
            Duration::from_millis(1),
        )
        .await
    })
    .await
}

#[tokio::test]
async fn spawn_and_wait_completes_a_healthy_handoff() {
    let outcome = spawn_against(vec![SPAWNED, COMPLETE]).await;
    assert!(matches!(outcome, DaemonOutcome::Complete(_)), "healthy");
}

#[tokio::test]
async fn spawn_and_wait_reports_a_refused_spawn() {
    let DaemonOutcome::Failed(detail) = spawn_against(vec![
        r#"{"result":"error","message":"stage 'ping' has no usable provider"}"#,
    ])
    .await
    else {
        panic!("a refused spawn fails the check");
    };
    assert!(detail.contains("no usable provider"), "{detail}");
}

#[tokio::test]
async fn spawn_and_wait_rejects_an_unexpected_spawn_response() {
    let DaemonOutcome::Failed(detail) = spawn_against(vec![r#"{"result":"ok","ok":true}"#]).await
    else {
        panic!("a nonsense spawn reply fails the check");
    };
    assert!(
        detail.contains("unexpected daemon response to spawn"),
        "{detail}"
    );
}

#[tokio::test]
async fn spawn_and_wait_reports_an_unreachable_daemon() {
    let DaemonOutcome::Failed(detail) = spawn_against(vec![]).await else {
        panic!("an unreachable daemon fails the check");
    };
    assert!(detail.contains("not reachable"), "{detail}");
}

#[tokio::test]
async fn spawn_and_wait_reports_a_manifest_it_cannot_resolve() {
    let outcome = with_env(|root| async move {
        let (id, _server) = scripted_daemon(&root, Vec::new());
        spawn_and_wait(
            &ControlClient::new(id),
            &root.join("nowhere").join("agent.leviath"),
            &root,
            Duration::from_secs(5),
            Duration::from_millis(1),
        )
        .await
    })
    .await;
    let DaemonOutcome::Failed(detail) = outcome else {
        panic!("a missing manifest fails before the daemon is contacted");
    };
    assert!(
        detail.contains("could not build the spawn request"),
        "{detail}"
    );
}

#[tokio::test]
async fn spawn_and_wait_leaves_nothing_behind() {
    // The whole point of a canary is that it cleans up after itself.
    let leftovers = with_env(|root| async move {
        let (id, _server) = scripted_daemon(&root, vec![SPAWNED.to_string(), COMPLETE.to_string()]);
        let manifest = staged(&root);
        let _ = spawn_and_wait(
            &ControlClient::new(id),
            &manifest,
            &root,
            Duration::from_secs(5),
            Duration::from_millis(1),
        )
        .await;
        crate::runstate::run_dir("doctor-1-abc").exists()
    })
    .await;
    assert!(!leftovers, "the probe run must not survive the check");
}

// ─── daemon_check ─────────────────────────────────────────────────────────────

#[tokio::test]
async fn daemon_check_passes_on_a_healthy_daemon() {
    let check = with_env(|root| async move {
        let (id, _server) = scripted_daemon(&root, vec![SPAWNED.to_string(), COMPLETE.to_string()]);
        daemon_check(
            &ControlClient::new(id),
            "stub",
            "m",
            Duration::from_secs(5),
            Duration::from_millis(1),
            &root,
        )
        .await
    })
    .await;
    assert_eq!(check.status, CheckStatus::Ok);
    assert!(check.elapsed_ms.is_some());
}

#[tokio::test]
async fn daemon_check_fails_when_the_daemon_is_unreachable() {
    let check = with_env(|root| async move {
        let (id, _server) = scripted_daemon(&root, Vec::new());
        daemon_check(
            &ControlClient::new(id),
            "stub",
            "m",
            Duration::from_secs(5),
            Duration::from_millis(1),
            &root,
        )
        .await
    })
    .await;
    assert_eq!(check.status, CheckStatus::Fail);
}

#[tokio::test]
async fn daemon_check_fails_when_the_probe_cannot_be_staged() {
    let check = with_env(|root| async move {
        let (id, _server) = scripted_daemon(&root, Vec::new());
        let blocker = root.join("blocked");
        std::fs::write(&blocker, b"i am a file").expect("write the blocker");
        daemon_check(
            &ControlClient::new(id),
            "stub",
            "m",
            Duration::from_secs(5),
            Duration::from_millis(1),
            &blocker,
        )
        .await
    })
    .await;
    assert_eq!(check.status, CheckStatus::Fail);
    assert!(
        check.detail.contains("could not stage a probe agent"),
        "{}",
        check.detail
    );
}

// ─── cleanup_run ──────────────────────────────────────────────────────────────

#[tokio::test]
async fn cleanup_run_removes_the_run_and_its_saved_state() {
    with_env(|root| async move {
        write_meta("r-clean", leviath_core::run_meta::RunStatus::Complete, None);
        let state = root.join(".leviath").join("state").join("r-clean");
        std::fs::create_dir_all(&state).expect("create the saved state dir");
        cleanup_run("r-clean");
        assert!(!crate::runstate::run_dir("r-clean").exists());
        assert!(!state.exists());
    })
    .await;
}

// ─── run_checks ───────────────────────────────────────────────────────────────

/// A registry builder that ignores the config and always yields `registry`.
fn always(
    registry: ProviderRegistry,
) -> impl Fn(&Config) -> Result<ProviderRegistry, leviath_providers::ProviderError> {
    move |_| Ok(registry.clone())
}

/// Write the smallest `config.toml` that parses, naming `provider`/`model` as
/// the defaults and appending `providers_extra` under `[providers]`.
fn write_config(root: &Path, provider: &str, model: Option<&str>, providers_extra: &str) {
    let model_line = model
        .map(|m| format!("default_model = \"{m}\"\n"))
        .unwrap_or_default();
    std::fs::write(
        root.join("config.toml"),
        format!(
            "agent_paths = []\ndefault_provider = \"{provider}\"\n{model_line}\
             \n[providers]\n{providers_extra}"
        ),
    )
    .expect("write config.toml");
}

#[tokio::test]
async fn run_checks_reports_a_config_that_will_not_parse() {
    let checks = with_env(|root| async move {
        std::fs::write(root.join("config.toml"), "this is not = = toml").expect("write");
        let build = always(ProviderRegistry::new());
        run_checks(&DoctorArgs::default(), &build, DaemonTarget::Skip).await
    })
    .await;
    assert_eq!(checks.len(), 1, "nothing runs after a broken config");
    assert_eq!(checks[0].name, "config");
    assert_eq!(checks[0].status, CheckStatus::Fail);
}

#[tokio::test]
async fn run_checks_stops_at_an_unconfigured_provider() {
    let checks = with_env(|root| async move {
        write_config(&root, "openrouter", None, "");
        let build = always(ProviderRegistry::new());
        run_checks(&DoctorArgs::default(), &build, DaemonTarget::Skip).await
    })
    .await;
    assert_eq!(checks.len(), 2, "no inference is attempted: {checks:?}");
    assert_eq!(checks[1].name, "resolve");
    assert_eq!(checks[1].status, CheckStatus::Fail);
}

#[tokio::test]
async fn run_checks_stops_at_a_failing_inference() {
    let checks = with_env(|root| async move {
        write_config(&root, "stub", Some("m"), "");
        let build = always(registry_with("stub", StubProvider::failing("HTTP 402")));
        run_checks(&DoctorArgs::default(), &build, DaemonTarget::Skip).await
    })
    .await;
    assert_eq!(checks.len(), 3, "the daemon is never contacted: {checks:?}");
    assert_eq!(checks[2].status, CheckStatus::Fail);
}

#[tokio::test]
async fn run_checks_skips_the_daemon_when_asked_to() {
    let checks = with_env(|root| async move {
        write_config(&root, "stub", Some("m"), "");
        let build = always(registry_with("stub", StubProvider::replying("PONG")));
        run_checks(&DoctorArgs::default(), &build, DaemonTarget::Skip).await
    })
    .await;
    assert_eq!(checks.len(), 3, "--no-daemon stops after the inference");
    assert!(checks.iter().all(|c| c.status == CheckStatus::Ok));
}

#[tokio::test]
async fn run_checks_reports_a_daemon_that_would_not_start() {
    // The credentials are fine and the report says so; the daemon is the
    // problem, and the report says that too. Answering neither - which is what
    // aborting on the auto-start failure would do - is the thing to avoid.
    let checks = with_env(|root| async move {
        write_config(&root, "stub", Some("m"), "");
        let build = always(registry_with("stub", StubProvider::replying("PONG")));
        let target = DaemonTarget::Unavailable("did not start within 5s".to_string());
        run_checks(&DoctorArgs::default(), &build, target).await
    })
    .await;
    assert_eq!(checks.len(), 4, "{checks:?}");
    assert!(checks[..3].iter().all(|c| c.status == CheckStatus::Ok));
    assert_eq!(checks[3].status, CheckStatus::Fail);
    assert!(checks[3].detail.contains("did not start"), "{checks:?}");
}

#[tokio::test]
async fn run_checks_runs_all_four_against_a_healthy_daemon() {
    let checks = with_env(|root| async move {
        // A key of the wrong shape, so the warning branch runs too.
        write_config(
            &root,
            "stub",
            Some("m"),
            "anthropic_api_key = \"not-a-real-shape\"\n",
        );
        let build = always(registry_with("stub", StubProvider::replying("PONG")));
        let (id, _server) = scripted_daemon(&root, vec![SPAWNED.to_string(), COMPLETE.to_string()]);
        let client = ControlClient::new(id);
        run_checks(
            &DoctorArgs::default(),
            &build,
            DaemonTarget::Client(&client),
        )
        .await
    })
    .await;
    assert_eq!(checks.len(), 4, "{checks:?}");
    assert!(
        checks.iter().all(|c| c.status == CheckStatus::Ok),
        "{checks:?}"
    );
}

// ─── execute ──────────────────────────────────────────────────────────────────

#[tokio::test]
async fn execute_prints_a_table_and_succeeds() {
    let result = with_env(|root| async move {
        write_config(&root, "stub", Some("m"), "");
        let build = always(registry_with("stub", StubProvider::replying("PONG")));
        execute_with_registry(DoctorArgs::default(), &build, DaemonTarget::Skip).await
    })
    .await;
    assert!(result.is_ok(), "{result:?}");
}

#[tokio::test]
async fn execute_prints_json_when_asked() {
    let result = with_env(|root| async move {
        write_config(&root, "stub", Some("m"), "");
        let build = always(registry_with("stub", StubProvider::replying("PONG")));
        let args = DoctorArgs {
            json: true,
            ..DoctorArgs::default()
        };
        execute_with_registry(args, &build, DaemonTarget::Skip).await
    })
    .await;
    assert!(result.is_ok(), "{result:?}");
}

#[tokio::test]
async fn execute_names_the_check_that_failed() {
    let err = with_env(|root| async move {
        write_config(&root, "stub", Some("m"), "");
        let build = always(registry_with("stub", StubProvider::failing("HTTP 402")));
        execute_with_registry(DoctorArgs::default(), &build, DaemonTarget::Skip)
            .await
            .expect_err("a failing inference fails the command")
    })
    .await;
    assert_eq!(err.to_string(), "doctor failed at: inference");
}

#[tokio::test]
async fn execute_wires_the_real_registry_builder() {
    // `execute` differs from `execute_with_registry` only in that builder, and
    // with no credentials in the isolated env it resolves to nothing usable -
    // which is the answer, and proves the production seam is connected.
    let err = with_env(|root| async move {
        write_config(&root, "nothing-here", None, "");
        execute(DoctorArgs::default(), DaemonTarget::Skip)
            .await
            .expect_err("no configured provider fails the command")
    })
    .await;
    assert_eq!(err.to_string(), "doctor failed at: resolve");
}

/// A registry builder that fails the way a machine with an unreadable root
/// certificate store would.
fn cannot_build(_config: &Config) -> Result<ProviderRegistry, leviath_providers::ProviderError> {
    Err(leviath_providers::ProviderError::ClientBuild(
        "no roots".to_string(),
    ))
}

#[tokio::test]
async fn a_registry_that_will_not_build_is_reported_as_a_failed_check() {
    let checks = with_env(|root| async move {
        write_config(&root, "anthropic", None, "");
        run_checks(&DoctorArgs::default(), &cannot_build, DaemonTarget::Skip).await
    })
    .await;
    let providers = checks
        .iter()
        .find(|c| c.name == "providers")
        .expect("a providers check");
    assert!(
        providers
            .detail
            .contains("could not build any provider client")
    );
}