ralph-workflow 0.7.18

PROMPT-driven multi-agent orchestrator for git repos
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
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
// Tests for the agent registry module.

use super::*;
use crate::agents::JsonParserType;

const TEST_SOURCES: &str =
    "local config (.agent/ralph-workflow.toml), global config (~/.config/ralph-workflow.toml), built-in defaults";

fn default_ccs() -> CcsConfig {
    CcsConfig::default()
}

#[test]
fn test_registry_new() {
    let registry = AgentRegistry::new().unwrap();
    // Behavioral test: agents are registered if they resolve
    assert!(registry.resolve_config("claude").is_some());
    assert!(registry.resolve_config("codex").is_some());
}

#[test]
fn test_registry_register() {
    let registry = AgentRegistry::new().unwrap().register(
        "testbot",
        AgentConfig {
            cmd: "testbot run".to_string(),
            output_flag: "--json".to_string(),
            yolo_flag: "--yes".to_string(),
            verbose_flag: String::new(),
            can_commit: true,
            json_parser: JsonParserType::Generic,
            model_flag: None,
            print_flag: String::new(),
            streaming_flag: String::new(),
            session_flag: String::new(),
            env_vars: std::collections::HashMap::new(),
            display_name: None,
        },
    );
    // Behavioral test: registered agent should resolve
    assert!(registry.resolve_config("testbot").is_some());
}

#[test]
fn test_registry_display_name() {
    let registry = AgentRegistry::new()
        .unwrap()
        // Agent without custom display name uses registry key
        .register(
            "claude",
            AgentConfig {
                cmd: "claude -p".to_string(),
                output_flag: "--output-format=stream-json".to_string(),
                yolo_flag: "--dangerously-skip-permissions".to_string(),
                verbose_flag: "--verbose".to_string(),
                can_commit: true,
                json_parser: JsonParserType::Claude,
                model_flag: None,
                print_flag: String::new(),
                streaming_flag: "--include-partial-messages".to_string(),
                session_flag: "--resume {}".to_string(),
                env_vars: std::collections::HashMap::new(),
                display_name: None,
            },
        )
        // Agent with custom display name uses that
        .register(
            "claude",
            AgentConfig {
                cmd: "claude -p".to_string(),
                output_flag: "--output-format=stream-json".to_string(),
                yolo_flag: "--dangerously-skip-permissions".to_string(),
                verbose_flag: "--verbose".to_string(),
                can_commit: true,
                json_parser: JsonParserType::Claude,
                model_flag: None,
                print_flag: String::new(),
                streaming_flag: "--include-partial-messages".to_string(),
                session_flag: "--resume {}".to_string(),
                env_vars: std::collections::HashMap::new(),
                display_name: None,
            },
        );

    // Test display names
    assert_eq!(registry.display_name("claude"), "claude");
    assert_eq!(registry.display_name("ccs/glm"), "ccs-glm");

    // Unknown agent returns the key as-is
    assert_eq!(registry.display_name("unknown"), "unknown");
}

#[test]
fn test_resolve_from_logfile_name() {
    let registry = AgentRegistry::new()
        .unwrap()
        // Register a CCS agent with slash in name
        .register(
            "ccs/glm",
            AgentConfig {
                cmd: "ccs glm".to_string(),
                output_flag: "--output-format=stream-json".to_string(),
                yolo_flag: "--dangerously-skip-permissions".to_string(),
                verbose_flag: "--verbose".to_string(),
                can_commit: true,
                json_parser: JsonParserType::Claude,
                model_flag: None,
                print_flag: "-p".to_string(),
                streaming_flag: "--include-partial-messages".to_string(),
                session_flag: "--resume {}".to_string(),
                env_vars: std::collections::HashMap::new(),
                display_name: Some("ccs-glm".to_string()),
            },
        )
        // Register a plain agent without slash
        .register(
            "claude",
            AgentConfig {
                cmd: "claude -p".to_string(),
                output_flag: "--output-format=stream-json".to_string(),
                yolo_flag: "--dangerously-skip-permissions".to_string(),
                verbose_flag: "--verbose".to_string(),
                can_commit: true,
                json_parser: JsonParserType::Claude,
                model_flag: None,
                print_flag: String::new(),
                streaming_flag: "--include-partial-messages".to_string(),
                session_flag: "--resume {}".to_string(),
                env_vars: std::collections::HashMap::new(),
                display_name: None,
            },
        )
        // Register an OpenCode agent with multiple slashes
        .register(
            "opencode/anthropic/claude-sonnet-4",
            AgentConfig {
                cmd: "opencode run".to_string(),
                output_flag: "--format json".to_string(),
                yolo_flag: String::new(),
                verbose_flag: "--log-level DEBUG".to_string(),
                can_commit: true,
                json_parser: JsonParserType::OpenCode,
                model_flag: Some("-p anthropic -m claude-sonnet-4".to_string()),
                print_flag: String::new(),
                streaming_flag: String::new(),
                session_flag: "-s {}".to_string(),
                env_vars: std::collections::HashMap::new(),
                display_name: Some("OpenCode (anthropic)".to_string()),
            },
        );

    // Test: Agent names that don't need sanitization
    assert_eq!(
        registry.resolve_from_logfile_name("claude"),
        Some("claude".to_string())
    );

    // Test: CCS agent - sanitized name resolved to registry name
    assert_eq!(
        registry.resolve_from_logfile_name("ccs-glm"),
        Some("ccs/glm".to_string())
    );

    // Test: OpenCode agent - sanitized name resolved to registry name
    assert_eq!(
        registry.resolve_from_logfile_name("opencode-anthropic-claude-sonnet-4"),
        Some("opencode/anthropic/claude-sonnet-4".to_string())
    );

    // Test: Unregistered CCS agent - should still resolve via pattern matching
    assert_eq!(
        registry.resolve_from_logfile_name("ccs-zai"),
        Some("ccs/zai".to_string())
    );

    // Test: Unregistered OpenCode agent - should still resolve via pattern matching
    assert_eq!(
        registry.resolve_from_logfile_name("opencode-google-gemini-pro"),
        Some("opencode/google/gemini-pro".to_string())
    );

    // Test: Unknown agent returns None
    assert_eq!(registry.resolve_from_logfile_name("unknown-agent"), None);
}

#[test]
fn test_registry_available_fallbacks() {
    // Test that available_fallbacks filters to only agents with commands in PATH.
    // Uses system commands (echo, cat) that exist on all systems to avoid
    // creating real executables or modifying PATH.
    let registry = AgentRegistry::new()
        .unwrap()
        // Register agents using commands that exist on all systems
        .register(
            "echo-agent",
            AgentConfig {
                cmd: "echo test".to_string(),
                output_flag: String::new(),
                yolo_flag: String::new(),
                verbose_flag: String::new(),
                can_commit: true,
                json_parser: JsonParserType::Generic,
                model_flag: None,
                print_flag: String::new(),
                streaming_flag: String::new(),
                session_flag: String::new(),
                env_vars: std::collections::HashMap::new(),
                display_name: None,
            },
        )
        .register(
            "cat-agent",
            AgentConfig {
                cmd: "cat --version".to_string(),
                output_flag: String::new(),
                yolo_flag: String::new(),
                verbose_flag: String::new(),
                can_commit: true,
                json_parser: JsonParserType::Generic,
                model_flag: None,
                print_flag: String::new(),
                streaming_flag: String::new(),
                session_flag: String::new(),
                env_vars: std::collections::HashMap::new(),
                display_name: None,
            },
        )
        .register(
            "nonexistent-agent",
            AgentConfig {
                cmd: "this-command-definitely-does-not-exist-xyz123".to_string(),
                output_flag: String::new(),
                yolo_flag: String::new(),
                verbose_flag: String::new(),
                can_commit: true,
                json_parser: JsonParserType::Generic,
                model_flag: None,
                print_flag: String::new(),
                streaming_flag: String::new(),
                session_flag: String::new(),
                env_vars: std::collections::HashMap::new(),
                display_name: None,
            },
        );

    // Set fallback chain using registered agents
    let toml_str = r#"
        [agent_chains]
        shared_dev = ["echo-agent", "nonexistent-agent", "cat-agent"]

        [agent_drains]
        planning = "shared_dev"
        development = "shared_dev"
        analysis = "shared_dev"
        review = "shared_dev"
        fix = "shared_dev"
        commit = "shared_dev"
    "#;
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();
    let registry = registry.apply_unified_config(&unified).unwrap();

    let fallbacks = registry.available_fallbacks(AgentRole::Developer);
    assert!(
        fallbacks.contains(&"echo-agent"),
        "echo-agent should be available"
    );
    assert!(
        fallbacks.contains(&"cat-agent"),
        "cat-agent should be available"
    );
    assert!(
        !fallbacks.contains(&"nonexistent-agent"),
        "nonexistent-agent should not be available"
    );
}

#[test]
fn test_validate_agent_chains() {
    let registry = AgentRegistry::new().unwrap();

    // Both chains configured should pass - use apply_unified_config (public API)
    let toml_str = r#"
        [agent_chains]
        shared_dev = ["claude"]
        shared_review = ["codex"]

        [agent_drains]
        planning = "shared_dev"
        development = "shared_dev"
        analysis = "shared_dev"
        review = "shared_review"
        fix = "shared_review"
        commit = "shared_review"
    "#;
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();
    let registry = registry.apply_unified_config(&unified).unwrap();
    assert!(registry.validate_agent_chains(TEST_SOURCES).is_ok());
}

#[test]
fn test_validate_agent_chains_rejects_non_workflow_capable_commit_drain() {
    let registry = AgentRegistry::new().unwrap().register(
        "chat-only",
        AgentConfig {
            cmd: "echo chat-only".to_string(),
            output_flag: String::new(),
            yolo_flag: String::new(),
            verbose_flag: String::new(),
            can_commit: false,
            json_parser: JsonParserType::Generic,
            model_flag: None,
            print_flag: String::new(),
            streaming_flag: String::new(),
            session_flag: String::new(),
            env_vars: std::collections::HashMap::new(),
            display_name: None,
        },
    );

    let toml_str = r#"
        [agent_chains]
        shared_dev = ["codex"]
        shared_review = ["claude"]
        chat_commit = ["chat-only"]

        [agent_drains]
        planning = "shared_dev"
        development = "shared_dev"
        review = "shared_review"
        fix = "shared_review"
        commit = "chat_commit"
        analysis = "shared_dev"
    "#;
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();

    let registry = registry.apply_unified_config(&unified).unwrap();

    let err = registry.validate_agent_chains(TEST_SOURCES).unwrap_err();
    let err_msg = err.to_string();
    assert!(
        err_msg.contains("commit"),
        "error should mention the commit drain: {err_msg}"
    );
    assert!(
        err_msg.contains("can_commit=false"),
        "error should explain the workflow-capability requirement: {err_msg}"
    );
}

#[test]
fn test_validate_agent_chains_returns_typed_error_for_non_workflow_capable_drain() {
    let registry = AgentRegistry::new().unwrap().register(
        "chat-only",
        AgentConfig {
            cmd: "echo chat-only".to_string(),
            output_flag: String::new(),
            yolo_flag: String::new(),
            verbose_flag: String::new(),
            can_commit: false,
            json_parser: JsonParserType::Generic,
            model_flag: None,
            print_flag: String::new(),
            streaming_flag: String::new(),
            session_flag: String::new(),
            env_vars: std::collections::HashMap::new(),
            display_name: None,
        },
    );

    let toml_str = r#"
        [agent_chains]
        shared_dev = ["codex"]
        shared_review = ["claude"]
        chat_commit = ["chat-only"]

        [agent_drains]
        planning = "shared_dev"
        development = "shared_dev"
        review = "shared_review"
        fix = "shared_review"
        commit = "chat_commit"
        analysis = "shared_dev"
    "#;
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();

    let registry = registry.apply_unified_config(&unified).unwrap();

    let err = registry
        .validate_agent_chains(TEST_SOURCES)
        .expect_err("chat-only commit drain should fail with typed error");

    assert!(matches!(
        err,
        AgentChainValidationError::NoWorkflowCapableAgents { .. }
    ));
}

#[test]
fn test_apply_unified_config_named_schema_projects_resolved_drains_into_fallback_compatibility() {
    let registry = AgentRegistry::new().unwrap();

    let toml_str = r#"
        [agent_chains]
        developer = ["codex"]
        reviewer = ["claude"]
        commit = ["opencode"]
        analysis = ["gemini"]

        [agent_drains]
        planning = "developer"
        development = "developer"
        review = "reviewer"
        fix = "reviewer"
        commit = "commit"
        analysis = "analysis"
    "#;
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();

    let registry = registry.apply_unified_config(&unified).unwrap();

    assert_eq!(
        registry.fallback_config().developer,
        vec!["codex"],
        "named drain bindings should project into the compatibility fallback config"
    );
    assert_eq!(registry.fallback_config().reviewer, vec!["claude"]);
    assert_eq!(registry.fallback_config().commit, vec!["opencode"]);
    assert_eq!(registry.fallback_config().analysis, vec!["gemini"]);
}

#[test]
fn test_apply_unified_config_rejects_invalid_named_drain_config() {
    let registry = AgentRegistry::new().unwrap();

    let toml_str = r#"
        [agent_chains]
        shared_dev = ["codex"]

        [agent_drains]
        planning = "missing_chain"
    "#;
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();

    let error = registry
        .apply_unified_config(&unified)
        .expect_err("invalid named drain bindings should fail fast");

    assert!(
        matches!(error, AgentConfigError::InvalidDrainConfig(ref message) if message.contains("missing_chain")),
        "unexpected error: {error}"
    );
}

#[test]
fn test_apply_unified_config_keeps_drain_defaults_when_named_chains_use_shared_names() {
    let registry = AgentRegistry::new().unwrap();

    let toml_str = r#"
        [agent_chains]
        shared_dev = ["codex", "claude"]
        shared_review = ["claude", "opencode"]

        [general]
        max_retries = 7
        retry_delay_ms = 2500
        backoff_multiplier = 3.0
        max_backoff_ms = 90000
        max_cycles = 5

        [general.provider_fallback]
        opencode = ["-m opencode/glm-4.7-free"]

        [agent_drains]
        planning = "shared_dev"
        development = "shared_dev"
        review = "shared_review"
        fix = "shared_review"
    "#;
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();

    let registry = registry.apply_unified_config(&unified).unwrap();

    let commit = registry
        .resolved_drain(crate::agents::AgentDrain::Commit)
        .expect("commit drain should inherit the bound review chain");
    let analysis = registry
        .resolved_drain(crate::agents::AgentDrain::Analysis)
        .expect("analysis drain should inherit the bound development chain");

    assert_eq!(commit.chain_name, "shared_review");
    assert_eq!(
        commit.agents,
        vec!["claude".to_string(), "opencode".to_string()]
    );
    assert_eq!(analysis.chain_name, "shared_dev");
    assert_eq!(
        analysis.agents,
        vec!["codex".to_string(), "claude".to_string()]
    );
    assert_eq!(registry.resolved_drains().max_retries, 7);
    assert_eq!(registry.resolved_drains().retry_delay_ms, 2_500);
    assert!((registry.resolved_drains().backoff_multiplier - 3.0).abs() < f64::EPSILON);
    assert_eq!(registry.resolved_drains().max_backoff_ms, 90_000);
    assert_eq!(registry.resolved_drains().max_cycles, 5);
    assert_eq!(
        registry.resolved_drains().provider_fallback.get("opencode"),
        Some(&vec!["-m opencode/glm-4.7-free".to_string()])
    );
}

#[test]
fn test_load_from_file_metadata_only_legacy_agent_chain_preserves_provider_fallback(
) -> Result<(), Box<dyn std::error::Error>> {
    let tmp = tempfile::tempdir().unwrap();
    let config_path = tmp.path().join("agents.toml");
    std::fs::write(
        &config_path,
        r#"
[agent_chain]
max_retries = 7

[agent_chain.provider_fallback]
opencode = ["-m opencode/glm-4.7-free"]
"#,
    )?;

    let registry = AgentRegistry::new()?.load_from_file(&config_path)?;

    assert_eq!(registry.resolved_drains().max_retries, 7);
    assert_eq!(
        registry.resolved_drains().provider_fallback.get("opencode"),
        Some(&vec!["-m opencode/glm-4.7-free".to_string()])
    );
    Ok(())
}

#[test]
fn test_available_fallbacks_for_drain_preserves_distinct_review_and_fix_bindings() {
    let registry = AgentRegistry::new().unwrap();

    let toml_str = r#"
        [agent_chains]
        review_chain = ["claude"]
        fix_chain = ["codex"]

        [agent_drains]
        planning = "review_chain"
        development = "review_chain"
        review = "review_chain"
        fix = "fix_chain"
        commit = "review_chain"
        analysis = "review_chain"
    "#;
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();

    let registry = registry.apply_unified_config(&unified).unwrap();

    assert_eq!(
        registry
            .resolved_drain(crate::agents::AgentDrain::Review)
            .map(|b| b.agents.as_slice())
            .unwrap_or_default(),
        &["claude"]
    );
    assert_eq!(
        registry
            .resolved_drain(crate::agents::AgentDrain::Fix)
            .map(|b| b.agents.as_slice())
            .unwrap_or_default(),
        &["codex"]
    );
}

#[test]
fn test_apply_unified_config_accepts_legacy_agent_chain_schema() {
    let registry = AgentRegistry::new().unwrap();
    let toml_str = "\n[agent_chain]\ndeveloper = []\nreviewer = []\n";
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();
    registry
        .apply_unified_config(&unified)
        .expect("legacy agent_chain should remain compatible");
}

#[test]
fn test_apply_unified_config_suggests_agent_chains_for_singular_typo() {
    let registry = AgentRegistry::new().unwrap();
    let toml_str = r#"
[agent_chain]
shared_dev = ["claude"]

[agent_drains]
planning = "shared_dev"
development = "shared_dev"
"#;
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();
    let err = registry
        .apply_unified_config(&unified)
        .expect_err("singular agent_chain typo should be rejected")
        .to_string();

    assert!(
        err.contains("did you mean [agent_chains]?"),
        "error should suggest the plural named chain section: {err}"
    );
}

#[test]
fn test_ccs_aliases_registration() {
    // Test that CCS aliases are registered correctly
    let mut aliases = HashMap::new();
    aliases.insert(
        "work".to_string(),
        CcsAliasConfig {
            cmd: "ccs work".to_string(),
            ..CcsAliasConfig::default()
        },
    );
    aliases.insert(
        "personal".to_string(),
        CcsAliasConfig {
            cmd: "ccs personal".to_string(),
            ..CcsAliasConfig::default()
        },
    );
    aliases.insert(
        "gemini".to_string(),
        CcsAliasConfig {
            cmd: "ccs gemini".to_string(),
            ..CcsAliasConfig::default()
        },
    );

    let registry = AgentRegistry::new()
        .unwrap()
        .set_ccs_aliases(&aliases, default_ccs());

    // CCS aliases should be registered as agents - behavioral test: they resolve
    assert!(registry.resolve_config("ccs/work").is_some());
    assert!(registry.resolve_config("ccs/personal").is_some());
    assert!(registry.resolve_config("ccs/gemini").is_some());

    // Get should return valid config
    let config = registry.resolve_config("ccs/work").unwrap();
    // When claude binary is found, it replaces "ccs work" with the path to claude
    assert!(
        config.cmd.ends_with("claude") || config.cmd == "ccs work",
        "cmd should be 'ccs work' or a path ending with 'claude', got: {}",
        config.cmd
    );
    assert!(config.can_commit);
    assert_eq!(config.json_parser, JsonParserType::Claude);
}

#[test]
fn test_ccs_in_fallback_chain() {
    // Test that CCS aliases can be used in fallback chains.
    // Uses `echo` command which exists on all systems to avoid creating
    // real executables or modifying PATH.
    let mut aliases = HashMap::new();
    aliases.insert(
        "work".to_string(),
        CcsAliasConfig {
            cmd: "echo work".to_string(),
            ..CcsAliasConfig::default()
        },
    );

    let registry = AgentRegistry::new()
        .unwrap()
        .set_ccs_aliases(&aliases, default_ccs())
        // Register a system command agent for comparison
        .register(
            "echo-agent",
            AgentConfig {
                cmd: "echo test".to_string(),
                output_flag: String::new(),
                yolo_flag: String::new(),
                verbose_flag: String::new(),
                can_commit: true,
                json_parser: JsonParserType::Generic,
                model_flag: None,
                print_flag: String::new(),
                streaming_flag: String::new(),
                session_flag: String::new(),
                env_vars: std::collections::HashMap::new(),
                display_name: None,
            },
        );

    // Set fallback chain with CCS alias using apply_unified_config (public API)
    let toml_str = r#"
        [agent_chains]
        shared_dev = ["ccs/work", "echo-agent"]
        shared_review = ["echo-agent"]

        [agent_drains]
        planning = "shared_dev"
        development = "shared_dev"
        analysis = "shared_dev"
        review = "shared_review"
        fix = "shared_review"
        commit = "shared_review"
    "#;
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();
    let registry = registry.apply_unified_config(&unified).unwrap();

    // ccs/work should be in available fallbacks (since echo is in PATH)
    let fallbacks = registry.available_fallbacks(AgentRole::Developer);
    assert!(
        fallbacks.contains(&"ccs/work"),
        "ccs/work should be available"
    );
    assert!(
        fallbacks.contains(&"echo-agent"),
        "echo-agent should be available"
    );

    // Validate chains should pass
    assert!(registry.validate_agent_chains(TEST_SOURCES).is_ok());
}

#[test]
fn test_ccs_aliases_with_registry_constructor() {
    let registry = AgentRegistry::new()
        .unwrap()
        .set_ccs_aliases(&HashMap::new(), default_ccs());

    // Should have built-in agents - behavioral test: they resolve
    assert!(registry.resolve_config("claude").is_some());
    assert!(registry.resolve_config("codex").is_some());

    // Now test with actual aliases
    let mut aliases = HashMap::new();
    aliases.insert(
        "work".to_string(),
        CcsAliasConfig {
            cmd: "ccs work".to_string(),
            ..CcsAliasConfig::default()
        },
    );

    let registry2 = AgentRegistry::new()
        .unwrap()
        .set_ccs_aliases(&aliases, default_ccs());
    // Behavioral test: CCS alias should resolve
    assert!(registry2.resolve_config("ccs/work").is_some());
}

#[test]
fn test_list_includes_ccs_aliases() {
    let mut aliases = HashMap::new();
    aliases.insert(
        "work".to_string(),
        CcsAliasConfig {
            cmd: "ccs work".to_string(),
            ..CcsAliasConfig::default()
        },
    );
    aliases.insert(
        "personal".to_string(),
        CcsAliasConfig {
            cmd: "ccs personal".to_string(),
            ..CcsAliasConfig::default()
        },
    );

    let registry = AgentRegistry::new()
        .unwrap()
        .set_ccs_aliases(&aliases, default_ccs());

    let all_agents = registry.list();

    assert_eq!(
        all_agents
            .iter()
            .filter(|(name, _)| name.starts_with("ccs/"))
            .count(),
        2
    );
}

#[test]
fn test_resolve_fuzzy_exact_match() {
    let registry = AgentRegistry::new().unwrap();
    assert_eq!(registry.resolve_fuzzy("claude"), Some("claude".to_string()));
    assert_eq!(registry.resolve_fuzzy("codex"), Some("codex".to_string()));
}

#[test]
fn test_resolve_fuzzy_ccs_unregistered() {
    let registry = AgentRegistry::new().unwrap();
    // ccs/<unregistered> should return as-is for direct execution
    assert_eq!(
        registry.resolve_fuzzy("ccs/random"),
        Some("ccs/random".to_string())
    );
    assert_eq!(
        registry.resolve_fuzzy("ccs/unregistered"),
        Some("ccs/unregistered".to_string())
    );
}

#[test]
fn test_resolve_fuzzy_typos() {
    let registry = AgentRegistry::new().unwrap();
    // Test common typos
    assert_eq!(registry.resolve_fuzzy("claud"), Some("claude".to_string()));
    assert_eq!(registry.resolve_fuzzy("CLAUD"), Some("claude".to_string()));
}

#[test]
fn test_resolve_fuzzy_codex_variations() {
    let registry = AgentRegistry::new().unwrap();
    // Test codex variations
    assert_eq!(registry.resolve_fuzzy("codeex"), Some("codex".to_string()));
    assert_eq!(registry.resolve_fuzzy("code-x"), Some("codex".to_string()));
    assert_eq!(registry.resolve_fuzzy("CODEEX"), Some("codex".to_string()));
}

#[test]
fn test_resolve_fuzzy_cursor_variations() {
    let registry = AgentRegistry::new().unwrap();
    // Test cursor variations
    assert_eq!(registry.resolve_fuzzy("crusor"), Some("cursor".to_string()));
    assert_eq!(registry.resolve_fuzzy("CRUSOR"), Some("cursor".to_string()));
}

#[test]
fn test_resolve_fuzzy_gemini_variations() {
    let registry = AgentRegistry::new().unwrap();
    // Test gemini variations
    assert_eq!(registry.resolve_fuzzy("gemeni"), Some("gemini".to_string()));
    assert_eq!(registry.resolve_fuzzy("gemni"), Some("gemini".to_string()));
    assert_eq!(registry.resolve_fuzzy("GEMENI"), Some("gemini".to_string()));
}

#[test]
fn test_resolve_fuzzy_qwen_variations() {
    let registry = AgentRegistry::new().unwrap();
    // Test qwen variations
    assert_eq!(registry.resolve_fuzzy("quen"), Some("qwen".to_string()));
    assert_eq!(registry.resolve_fuzzy("quwen"), Some("qwen".to_string()));
    assert_eq!(registry.resolve_fuzzy("QUEN"), Some("qwen".to_string()));
}

#[test]
fn test_resolve_fuzzy_aider_variations() {
    let registry = AgentRegistry::new().unwrap();
    // Test aider variations
    assert_eq!(registry.resolve_fuzzy("ader"), Some("aider".to_string()));
    assert_eq!(registry.resolve_fuzzy("ADER"), Some("aider".to_string()));
}

#[test]
fn test_resolve_fuzzy_vibe_variations() {
    let registry = AgentRegistry::new().unwrap();
    // Test vibe variations
    assert_eq!(registry.resolve_fuzzy("vib"), Some("vibe".to_string()));
    assert_eq!(registry.resolve_fuzzy("VIB"), Some("vibe".to_string()));
}

#[test]
fn test_resolve_fuzzy_cline_variations() {
    let registry = AgentRegistry::new().unwrap();
    // Test cline variations
    assert_eq!(registry.resolve_fuzzy("kline"), Some("cline".to_string()));
    assert_eq!(registry.resolve_fuzzy("KLINE"), Some("cline".to_string()));
}

#[test]
fn test_resolve_fuzzy_ccs_dash_to_slash() {
    let registry = AgentRegistry::new().unwrap();
    // Test ccs- to ccs/ conversion (even for unregistered aliases)
    assert_eq!(
        registry.resolve_fuzzy("ccs-random"),
        Some("ccs/random".to_string())
    );
    assert_eq!(
        registry.resolve_fuzzy("ccs-test"),
        Some("ccs/test".to_string())
    );
}

#[test]
fn test_resolve_fuzzy_underscore_replacement() {
    // Test underscore to dash/slash replacement
    // Note: These test the pattern, actual agents may not exist
    let result = AgentRegistry::get_fuzzy_alternatives("my_agent");
    assert!(result.contains(&"my_agent".to_string()));
    assert!(result.contains(&"my-agent".to_string()));
    assert!(result.contains(&"my/agent".to_string()));
}

#[test]
fn test_resolve_fuzzy_unknown() {
    let registry = AgentRegistry::new().unwrap();
    // Unknown agent should return None
    assert_eq!(registry.resolve_fuzzy("totally-unknown"), None);
}

#[test]
fn test_apply_unified_config_does_not_inherit_env_vars() {
    // Regression test for CCS env vars leaking between agents.
    // Ensures that when apply_unified_config merges agent configurations,
    // env_vars from the existing agent are NOT inherited into the merged agent.
    let registry = AgentRegistry::new()
        .unwrap()
        // First, manually register a "claude" agent with some env vars (simulating
        // a previously-loaded agent with CCS env vars or manually-specified vars)
        .register(
            "claude",
            AgentConfig {
                cmd: "claude -p".to_string(),
                output_flag: "--output-format=stream-json".to_string(),
                yolo_flag: "--dangerously-skip-permissions".to_string(),
                verbose_flag: "--verbose".to_string(),
                can_commit: true,
                json_parser: JsonParserType::Claude,
                model_flag: None,
                print_flag: String::new(),
                streaming_flag: "--include-partial-messages".to_string(),
                session_flag: "--resume {}".to_string(),
                // Simulate CCS env vars from a previous load
                env_vars: {
                    let mut vars = std::collections::HashMap::new();
                    vars.insert(
                        "ANTHROPIC_BASE_URL".to_string(),
                        "https://api.z.ai/api/anthropic".to_string(),
                    );
                    vars.insert(
                        "ANTHROPIC_AUTH_TOKEN".to_string(),
                        "test-token-glm".to_string(),
                    );
                    vars.insert("ANTHROPIC_MODEL".to_string(), "glm-4.7".to_string());
                    vars
                },
                display_name: None,
            },
        );

    // Verify the "claude" agent has the GLM env vars
    let claude_config = registry.resolve_config("claude").unwrap();
    assert_eq!(claude_config.env_vars.len(), 3);
    assert_eq!(
        claude_config.env_vars.get("ANTHROPIC_BASE_URL"),
        Some(&"https://api.z.ai/api/anthropic".to_string())
    );

    // Now apply a unified config that overrides the "claude" agent
    // (simulating user's ~/.config/ralph-workflow.toml with [agents.claude])
    // Create a minimal GeneralConfig via Default for UnifiedConfig
    // Note: We can't directly construct UnifiedConfig with Default because agents is not Default
    // So we'll create it by deserializing from a TOML string
    let toml_str = r#"
        [general]
        verbosity = 2
        interactive = true
        isolation_mode = true

        [agents.claude]
        cmd = "claude -p"
        display_name = "My Custom Claude"
    "#;
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();

    // Apply the unified config
    let registry = registry.apply_unified_config(&unified).unwrap();

    // Verify that the "claude" agent's env_vars are now empty (NOT inherited)
    let claude_config_after = registry.resolve_config("claude").unwrap();
    assert_eq!(
        claude_config_after.env_vars.len(),
        0,
        "env_vars should NOT be inherited from the existing agent when unified config is applied"
    );
    assert_eq!(
        claude_config_after.display_name,
        Some("My Custom Claude".to_string()),
        "display_name should be updated from the unified config"
    );
}

#[test]
fn test_resolve_config_does_not_share_env_vars_between_agents() {
    // Regression test for the exact bug scenario:
    // 1. User runs Ralph with ccs/glm agent (with GLM env vars)
    // 2. User then runs Ralph with claude agent
    // 3. Claude should NOT have GLM env vars
    //
    // This test verifies that resolve_config() returns independent AgentConfig
    // instances with separate env_vars HashMaps - i.e., modifications to one
    // agent's env_vars don't affect another agent's config.
    let registry = AgentRegistry::new()
        .unwrap()
        // Register ccs/glm with GLM environment variables
        .register(
            "ccs/glm",
            AgentConfig {
                cmd: "ccs glm".to_string(),
                output_flag: "--output-format=stream-json".to_string(),
                yolo_flag: "--dangerously-skip-permissions".to_string(),
                verbose_flag: "--verbose".to_string(),
                can_commit: true,
                json_parser: JsonParserType::Claude,
                model_flag: None,
                print_flag: "-p".to_string(),
                streaming_flag: "--include-partial-messages".to_string(),
                session_flag: "--resume {}".to_string(),
                env_vars: {
                    let mut vars = std::collections::HashMap::new();
                    vars.insert(
                        "ANTHROPIC_BASE_URL".to_string(),
                        "https://api.z.ai/api/anthropic".to_string(),
                    );
                    vars.insert(
                        "ANTHROPIC_AUTH_TOKEN".to_string(),
                        "test-token-glm".to_string(),
                    );
                    vars.insert("ANTHROPIC_MODEL".to_string(), "glm-4.7".to_string());
                    vars
                },
                display_name: Some("ccs-glm".to_string()),
            },
        )
        // Register claude with empty env_vars (typical configuration)
        .register(
            "claude",
            AgentConfig {
                cmd: "claude -p".to_string(),
                output_flag: "--output-format=stream-json".to_string(),
                yolo_flag: "--dangerously-skip-permissions".to_string(),
                verbose_flag: "--verbose".to_string(),
                can_commit: true,
                json_parser: JsonParserType::Claude,
                model_flag: None,
                print_flag: String::new(),
                streaming_flag: "--include-partial-messages".to_string(),
                session_flag: "--resume {}".to_string(),
                env_vars: std::collections::HashMap::new(),
                display_name: None,
            },
        );

    // Resolve ccs/glm config first
    let glm_config = registry.resolve_config("ccs/glm").unwrap();
    assert_eq!(glm_config.env_vars.len(), 3);
    assert_eq!(
        glm_config.env_vars.get("ANTHROPIC_BASE_URL"),
        Some(&"https://api.z.ai/api/anthropic".to_string())
    );

    // Resolve claude config
    let claude_config = registry.resolve_config("claude").unwrap();
    assert_eq!(
        claude_config.env_vars.len(),
        0,
        "claude agent should have empty env_vars"
    );

    // Resolve ccs/glm again to ensure we get a fresh clone
    let glm_config2 = registry.resolve_config("ccs/glm").unwrap();
    assert_eq!(glm_config2.env_vars.len(), 3);

    // Modify the first GLM config's env_vars
    // This should NOT affect the second GLM config if cloning is deep
    drop(glm_config);

    // Verify claude still has empty env_vars after another resolve
    let claude_config2 = registry.resolve_config("claude").unwrap();
    assert_eq!(
        claude_config2.env_vars.len(),
        0,
        "claude agent env_vars should remain independent"
    );
}

// --- Typed error variant tests for AgentChainValidationError ---

#[test]
fn test_validate_agent_chains_returns_no_chain_configured_when_all_drains_have_empty_agents() {
    // A legacy [agent_chain] with all empty role arrays forces from_legacy to create
    // all 6 drains with empty agent lists. validate_agent_chains then fires NoChainConfigured
    // because has_any_binding is false (no drain has any agents).
    let toml_str = r#"
        [agent_chain]
        developer = []
    "#;
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();
    let registry = AgentRegistry::new().unwrap().apply_unified_config(&unified).unwrap();

    let err = registry
        .validate_agent_chains("test-sources")
        .expect_err("registry with no agents should produce NoChainConfigured");

    assert!(
        matches!(err, AgentChainValidationError::NoChainConfigured { .. }),
        "expected NoChainConfigured variant, got: {err:?}"
    );
}

#[test]
fn test_validate_agent_chains_returns_empty_drain_chain_when_some_drains_have_no_agents() {
    // A legacy [agent_chain] with developer non-empty but reviewer empty results in:
    //   Planning/Development → ["claude"] (non-empty)
    //   Review/Fix/Commit/Analysis → [] (empty)
    // has_any_binding = true, but the first empty drain (Review) fires EmptyDrainChain.
    let toml_str = r#"
        [agent_chain]
        developer = ["claude"]
    "#;
    let unified: crate::config::UnifiedConfig = toml::from_str(toml_str).unwrap();
    let registry = AgentRegistry::new().unwrap().apply_unified_config(&unified).unwrap();

    let err = registry
        .validate_agent_chains("test-sources")
        .expect_err("registry with partial drain coverage should produce EmptyDrainChain");

    assert!(
        matches!(err, AgentChainValidationError::EmptyDrainChain { .. }),
        "expected EmptyDrainChain variant, got: {err:?}"
    );
}