everruns-cli 0.18.4

Command-line interface for Everruns — run and manage agents from your terminal
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
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
// Everruns CLI
//
// Design Decision: Use clap derive for ergonomic argument parsing.
// Design Decision: Support text/json output formats for scripting.
// Design Decision: Use everruns-sdk for API client.
// Design Decision: Credential file (platform config dir/everruns/credentials.json) with env var override.

mod auth;
mod browser;
mod commands;
mod output;
mod user_dirs;

use clap::{Parser, Subcommand};
use everruns_sdk::Everruns;

#[derive(Parser)]
#[command(name = "everruns")]
#[command(about = "Everruns CLI - Manage agents, sessions, and conversations")]
#[command(version)]
pub struct Cli {
    /// API key (defaults to EVERRUNS_API_KEY env var, then credential file)
    #[arg(long, env = "EVERRUNS_API_KEY")]
    pub api_key: Option<String>,

    /// API base URL
    #[arg(long, env = "EVERRUNS_API_URL")]
    pub api_url: Option<String>,

    /// Output format
    #[arg(long, short, global = true, default_value = "text", value_parser = ["text", "json", "yaml"])]
    pub output: String,

    /// Suppress non-essential output
    #[arg(long, short, global = true)]
    pub quiet: bool,

    /// Profile name for credential storage
    #[arg(long, global = true, default_value = "default")]
    pub profile: String,

    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Subcommand)]
pub enum Commands {
    /// Interactive login (localhost OAuth callback)
    Login {
        /// Paste API key directly (headless/SSH fallback)
        #[arg(long)]
        token: bool,
    },

    /// Remove stored credentials
    Logout,

    /// Show current user and org
    Status,

    /// Manage organizations
    Orgs {
        #[command(subcommand)]
        command: Option<OrgsCommand>,
    },

    /// Manage agents
    Agents {
        #[command(subcommand)]
        command: commands::agents::AgentsCommand,
    },

    /// Manage provider connections (API keys)
    Connections {
        #[command(subcommand)]
        command: commands::connections::ConnectionsCommand,
    },

    /// Manage capabilities
    Capabilities {
        /// Filter by status
        #[arg(long, default_value = "available", value_parser = ["available", "coming_soon", "all"])]
        status: String,

        #[command(subcommand)]
        command: Option<CapabilitiesCommand>,
    },

    /// Discover plugins
    Plugins {
        #[command(subcommand)]
        command: commands::plugins::PluginsCommands,
    },

    /// Manage skills
    Skills {
        #[command(subcommand)]
        command: commands::skills::SkillsCommands,
    },

    /// Manage knowledge bases
    KnowledgeBases {
        #[command(subcommand)]
        command: commands::knowledge_bases::KnowledgeBasesCommands,
    },

    /// Manage sessions
    Sessions {
        #[command(subcommand)]
        command: commands::sessions::SessionsCommand,
    },

    /// Manage an agent's schedule triggers
    Triggers {
        /// Agent ID that owns the triggers
        #[arg(long)]
        agent: String,
        #[command(subcommand)]
        command: commands::triggers::TriggersCommand,
    },

    /// Manage a session's participants
    Participants {
        /// Session ID that owns the participants
        #[arg(long)]
        session: String,
        #[command(subcommand)]
        command: commands::participants::ParticipantsCommand,
    },

    /// File sync and management
    Files {
        #[command(subcommand)]
        command: commands::files::FilesCommand,
    },

    /// Send a message and stream the response
    Chat {
        /// Message text to send
        message: String,

        /// Session ID (e.g. ses_xxx)
        #[arg(long, short)]
        session: String,

        /// Max wait time in seconds (default: unlimited)
        #[arg(long)]
        timeout: Option<u64>,

        /// Send message and exit immediately without waiting for response
        #[arg(long)]
        no_stream: bool,
    },
}

#[derive(Subcommand)]
pub enum OrgsCommand {
    /// Interactive organization picker
    Select,
}

#[derive(Subcommand)]
pub enum CapabilitiesCommand {
    /// List available capabilities
    List {
        /// Filter by status
        #[arg(long, default_value = "available", value_parser = ["available", "coming_soon", "all"])]
        status: String,
    },
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    everruns_provider::install_default_crypto_provider();

    let cli = Cli::parse();
    let output_format = output::OutputFormat::from_str(&cli.output);

    // Commands that don't need authentication
    match &cli.command {
        Commands::Login { token } => {
            return Box::pin(commands::login::run(
                cli.api_url.as_deref(),
                *token,
                &cli.profile,
            ))
            .await;
        }
        Commands::Logout => {
            return commands::logout::run(&cli.profile);
        }
        Commands::Status => {
            return commands::status::run(&cli.profile);
        }
        Commands::Orgs { command } => {
            return match command {
                Some(OrgsCommand::Select) => {
                    Box::pin(commands::orgs::run_select(&cli.profile)).await
                }
                None => Box::pin(commands::orgs::run_list(output_format, &cli.profile)).await,
            };
        }
        _ => {}
    }

    // Resolve credentials: CLI flags > env var > credential file
    let creds = auth::resolve_credentials(
        cli.api_key.as_deref(),
        cli.api_url.as_deref(),
        Some(&cli.profile),
    )?;
    let api_key = creds.api_key;
    let api_url = creds.api_url;
    let org_id = creds.org_id;

    // Build SDK client
    let client = if let Some(org_id) = org_id.as_deref() {
        Everruns::with_base_url_and_org_id(&api_key, &api_url, org_id)?
    } else {
        Everruns::with_base_url(&api_key, &api_url)?
    };

    // Each arm's future is boxed rather than awaited inline. Without it every
    // command's state machine is inlined into main's, which the optimizer then
    // duplicates across codegen units — main::{{closure}} and its drop glue cost
    // ~200 KiB of the release binary. One allocation per process is free here:
    // the CLI runs exactly one command and exits.
    match cli.command {
        Commands::Agents { command } => {
            Box::pin(commands::agents::run(
                command,
                &client,
                &api_url,
                &api_key,
                org_id.as_deref(),
                output_format,
                cli.quiet,
            ))
            .await
        }
        Commands::Connections { command } => {
            Box::pin(commands::connections::run(
                command,
                &client,
                &api_url,
                &api_key,
                output_format,
                cli.quiet,
            ))
            .await
        }
        Commands::Capabilities { status, command } => {
            let status = match &command {
                Some(CapabilitiesCommand::List { status }) => status.clone(),
                None => status,
            };
            Box::pin(commands::capabilities::run(&client, output_format, &status)).await
        }
        Commands::Plugins { command } => {
            Box::pin(commands::plugins::run(
                command,
                &api_url,
                &api_key,
                org_id.as_deref(),
                output_format,
            ))
            .await
        }
        Commands::Skills { command } => {
            Box::pin(commands::skills::run(
                command,
                &api_url,
                &api_key,
                org_id.as_deref(),
                output_format,
            ))
            .await
        }
        Commands::KnowledgeBases { command } => {
            Box::pin(commands::knowledge_bases::run(
                command,
                &api_url,
                &api_key,
                org_id.as_deref(),
                output_format,
            ))
            .await
        }
        Commands::Sessions { command } => {
            Box::pin(commands::sessions::run(
                command,
                &client,
                &api_url,
                &api_key,
                org_id.as_deref(),
                output_format,
                cli.quiet,
            ))
            .await
        }
        Commands::Triggers { agent, command } => {
            Box::pin(commands::triggers::run(
                command,
                agent,
                &api_url,
                &api_key,
                org_id.as_deref(),
                output_format,
                cli.quiet,
            ))
            .await
        }
        Commands::Participants { session, command } => {
            Box::pin(commands::participants::run(
                command,
                session,
                &api_url,
                &api_key,
                org_id.as_deref(),
                output_format,
                cli.quiet,
            ))
            .await
        }
        Commands::Files { command } => {
            Box::pin(commands::files::run(
                command,
                &api_url,
                &api_key,
                org_id.as_deref(),
                output_format,
                cli.quiet,
            ))
            .await
        }
        Commands::Chat {
            message,
            session,
            timeout,
            no_stream,
        } => {
            Box::pin(commands::chat::run(
                &client,
                output_format,
                cli.quiet,
                message,
                session,
                timeout,
                no_stream,
            ))
            .await
        }
        // Already handled above
        Commands::Login { .. } | Commands::Logout | Commands::Status | Commands::Orgs { .. } => {
            unreachable!()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::CommandFactory;

    #[test]
    fn test_cli_parse_agents_list() {
        let cli = Cli::try_parse_from(["everruns", "agents", "list"]).unwrap();
        assert!(matches!(cli.command, Commands::Agents { .. }));
        assert_eq!(cli.output, "text");
        assert!(!cli.quiet);
    }

    #[test]
    fn test_cli_parse_agents_get() {
        let cli = Cli::try_parse_from(["everruns", "agents", "get", "agt_123"]).unwrap();
        if let Commands::Agents { command } = cli.command {
            if let commands::agents::AgentsCommand::Get { agent_id } = command {
                assert_eq!(agent_id, "agt_123");
            } else {
                panic!("Expected Get command");
            }
        } else {
            panic!("Expected Agents command");
        }
    }

    #[test]
    fn test_cli_parse_sessions_create() {
        let cli = Cli::try_parse_from([
            "everruns",
            "sessions",
            "create",
            "--harness",
            "harness_abc",
            "--agent",
            "agt_abc",
            "--title",
            "Test Session",
        ])
        .unwrap();
        if let Commands::Sessions { command } = cli.command {
            if let commands::sessions::SessionsCommand::Create {
                harness,
                agent,
                title,
                model,
                locale,
                agent_identity,
                system_prompt,
                tags,
                capabilities,
                hints,
                hints_json,
                network_allow,
                network_block,
                max_iterations,
                secrets,
                budget_limits,
                budget_soft_limits,
            } = command
            {
                assert_eq!(harness, Some("harness_abc".to_string()));
                assert_eq!(agent, Some("agt_abc".to_string()));
                assert_eq!(title, Some("Test Session".to_string()));
                assert_eq!(model, None);
                assert_eq!(locale, None);
                assert_eq!(agent_identity, None);
                assert_eq!(system_prompt, None);
                assert!(tags.is_empty());
                assert!(capabilities.is_empty());
                assert!(hints.is_empty());
                assert_eq!(hints_json, None);
                assert!(network_allow.is_empty());
                assert!(network_block.is_empty());
                assert_eq!(max_iterations, None);
                assert!(secrets.is_empty());
                assert!(budget_limits.is_empty());
                assert!(budget_soft_limits.is_empty());
            } else {
                panic!("Expected Create command");
            }
        } else {
            panic!("Expected Sessions command");
        }
    }

    #[test]
    fn test_cli_parse_sessions_create_no_harness() {
        let cli = Cli::try_parse_from(["everruns", "sessions", "create"]).unwrap();
        if let Commands::Sessions { command } = cli.command {
            if let commands::sessions::SessionsCommand::Create {
                harness, secrets, ..
            } = command
            {
                assert_eq!(harness, None); // org default
                assert!(secrets.is_empty());
            } else {
                panic!("Expected Create command");
            }
        } else {
            panic!("Expected Sessions command");
        }
    }

    #[test]
    fn test_cli_parse_sessions_create_with_secrets() {
        let cli = Cli::try_parse_from([
            "everruns",
            "sessions",
            "create",
            "--agent",
            "agt_abc",
            "--secret",
            "KEY1=value1",
            "--secret",
            "KEY2=value2",
        ])
        .unwrap();
        if let Commands::Sessions { command } = cli.command {
            if let commands::sessions::SessionsCommand::Create { secrets, .. } = command {
                assert_eq!(secrets.len(), 2);
                assert_eq!(secrets[0], "KEY1=value1");
                assert_eq!(secrets[1], "KEY2=value2");
            } else {
                panic!("Expected Create command");
            }
        } else {
            panic!("Expected Sessions command");
        }
    }

    #[test]
    fn test_cli_parse_sessions_watch() {
        let cli = Cli::try_parse_from(["everruns", "sessions", "watch", "ses_abc"]).unwrap();
        if let Commands::Sessions { command } = cli.command {
            if let commands::sessions::SessionsCommand::Watch { session } = command {
                assert_eq!(session, "ses_abc");
            } else {
                panic!("Expected Watch command");
            }
        } else {
            panic!("Expected Sessions command");
        }
    }

    #[test]
    fn test_cli_parse_triggers_create() {
        let cli = Cli::try_parse_from([
            "everruns",
            "triggers",
            "--agent",
            "agent_abc",
            "create",
            "--cron",
            "30 * * * *",
            "--timezone",
            "America/Chicago",
            "--session-mode",
            "session-per-invocation",
            "--message",
            "Prepare report",
        ])
        .unwrap();
        if let Commands::Triggers { agent, command } = cli.command {
            assert_eq!(agent, "agent_abc");
            assert!(matches!(
                command,
                commands::triggers::TriggersCommand::Create {
                    session_mode: commands::triggers::SessionMode::SessionPerInvocation,
                    ..
                }
            ));
        } else {
            panic!("Expected Triggers command");
        }
    }

    #[test]
    fn test_cli_parse_trigger_run_now() {
        let cli = Cli::try_parse_from([
            "everruns",
            "triggers",
            "--agent",
            "agent_abc",
            "run-now",
            "trg_abc",
        ])
        .unwrap();
        assert!(matches!(
            cli.command,
            Commands::Triggers {
                command: commands::triggers::TriggersCommand::RunNow { .. },
                ..
            }
        ));
    }

    #[test]
    fn test_cli_parse_participants_add() {
        let cli = Cli::try_parse_from([
            "everruns",
            "participants",
            "--session",
            "session_abc",
            "add",
            "--agent",
            "agent_guest",
        ])
        .unwrap();
        if let Commands::Participants { session, command } = cli.command {
            assert_eq!(session, "session_abc");
            assert!(matches!(
                command,
                commands::participants::ParticipantsCommand::Add { agent }
                    if agent == "agent_guest"
            ));
        } else {
            panic!("Expected Participants command");
        }
    }

    #[test]
    fn test_cli_parse_sessions_create_new_fields() {
        let cli = Cli::try_parse_from([
            "everruns",
            "sessions",
            "create",
            "--agent",
            "agent_abc",
            "--agent-identity",
            "identity_abc",
            "--system-prompt",
            "Be concise",
            "--locale",
            "uk-UA",
            "--tag",
            "debugging",
            "--capability",
            "web_fetch={\"timeout\":10}",
            "--hint",
            "setup_connection=true",
            "--hints-json",
            "{\"rich_media\":true}",
            "--network-allow",
            "api.example.com",
            "--network-block",
            "internal.example.com",
            "--max-iterations",
            "8",
        ])
        .unwrap();
        if let Commands::Sessions { command } = cli.command {
            if let commands::sessions::SessionsCommand::Create {
                agent_identity,
                system_prompt,
                locale,
                tags,
                capabilities,
                hints,
                hints_json,
                network_allow,
                network_block,
                max_iterations,
                ..
            } = command
            {
                assert_eq!(agent_identity, Some("identity_abc".to_string()));
                assert_eq!(system_prompt, Some("Be concise".to_string()));
                assert_eq!(locale, Some("uk-UA".to_string()));
                assert_eq!(tags, vec!["debugging".to_string()]);
                assert_eq!(capabilities, vec!["web_fetch={\"timeout\":10}".to_string()]);
                assert_eq!(hints, vec!["setup_connection=true".to_string()]);
                assert_eq!(hints_json, Some("{\"rich_media\":true}".to_string()));
                assert_eq!(network_allow, vec!["api.example.com".to_string()]);
                assert_eq!(network_block, vec!["internal.example.com".to_string()]);
                assert_eq!(max_iterations, Some(8));
            } else {
                panic!("Expected Create command");
            }
        } else {
            panic!("Expected Sessions command");
        }
    }

    #[test]
    fn test_cli_parse_chat() {
        let cli = Cli::try_parse_from(["everruns", "chat", "--session", "ses_xyz", "Hello world"])
            .unwrap();
        if let Commands::Chat {
            message,
            session,
            timeout,
            no_stream,
        } = cli.command
        {
            assert_eq!(message, "Hello world");
            assert_eq!(session, "ses_xyz");
            assert_eq!(timeout, None); // default: no timeout
            assert!(!no_stream);
        } else {
            panic!("Expected Chat command");
        }
    }

    #[test]
    fn test_cli_parse_chat_with_options() {
        let cli = Cli::try_parse_from([
            "everruns",
            "chat",
            "--session",
            "ses_xyz",
            "--timeout",
            "60",
            "--no-stream",
            "Test message",
        ])
        .unwrap();
        if let Commands::Chat {
            message,
            session,
            timeout,
            no_stream,
        } = cli.command
        {
            assert_eq!(message, "Test message");
            assert_eq!(session, "ses_xyz");
            assert_eq!(timeout, Some(60));
            assert!(no_stream);
        } else {
            panic!("Expected Chat command");
        }
    }

    #[test]
    fn test_cli_parse_output_format() {
        let cli = Cli::try_parse_from(["everruns", "-o", "json", "agents", "list"]).unwrap();
        assert_eq!(cli.output, "json");

        let cli = Cli::try_parse_from(["everruns", "-o", "yaml", "agents", "list"]).unwrap();
        assert_eq!(cli.output, "yaml");
    }

    #[test]
    fn test_cli_parse_quiet_flag() {
        let cli = Cli::try_parse_from(["everruns", "-q", "agents", "list"]).unwrap();
        assert!(cli.quiet);

        let cli = Cli::try_parse_from(["everruns", "--quiet", "agents", "list"]).unwrap();
        assert!(cli.quiet);
    }

    #[test]
    fn test_cli_parse_capabilities_bare() {
        let cli = Cli::try_parse_from(["everruns", "capabilities"]).unwrap();
        if let Commands::Capabilities { command, status } = cli.command {
            assert_eq!(status, "available");
            assert!(command.is_none()); // bare defaults to list with available
        } else {
            panic!("Expected Capabilities command");
        }
    }

    #[test]
    fn test_cli_parse_capabilities_bare_status() {
        let cli = Cli::try_parse_from(["everruns", "capabilities", "--status", "all"]).unwrap();
        if let Commands::Capabilities { command, status } = cli.command {
            assert!(command.is_none());
            assert_eq!(status, "all");
        } else {
            panic!("Expected Capabilities command");
        }
    }

    #[test]
    fn test_cli_parse_capabilities_list() {
        let cli = Cli::try_parse_from(["everruns", "capabilities", "list"]).unwrap();
        if let Commands::Capabilities {
            status: _,
            command: Some(CapabilitiesCommand::List { status }),
        } = cli.command
        {
            assert_eq!(status, "available"); // default
        } else {
            panic!("Expected Capabilities List command");
        }
    }

    #[test]
    fn test_cli_parse_capabilities_list_status() {
        let cli =
            Cli::try_parse_from(["everruns", "capabilities", "list", "--status", "all"]).unwrap();
        if let Commands::Capabilities {
            status: _,
            command: Some(CapabilitiesCommand::List { status }),
        } = cli.command
        {
            assert_eq!(status, "all");
        } else {
            panic!("Expected Capabilities List command");
        }
    }

    #[test]
    fn test_cli_parse_files_sync() {
        let cli = Cli::try_parse_from([
            "everruns",
            "files",
            "sync",
            "--session",
            "ses_abc",
            "--interval",
            "5",
            "--conflict",
            "local-wins",
            "--verbose",
            "/tmp/mydir",
        ])
        .unwrap();
        if let Commands::Files { command } = cli.command {
            if let commands::files::FilesCommand::Sync {
                session,
                local_dir,
                interval,
                conflict,
                verbose,
                ..
            } = command
            {
                assert_eq!(session, "ses_abc");
                assert_eq!(local_dir, "/tmp/mydir");
                assert_eq!(interval, 5);
                assert_eq!(conflict, "local-wins");
                assert!(verbose);
            } else {
                panic!("Expected Sync command");
            }
        } else {
            panic!("Expected Files command");
        }
    }

    #[test]
    fn test_cli_parse_files_push() {
        let cli = Cli::try_parse_from([
            "everruns",
            "files",
            "push",
            "--session",
            "ses_xyz",
            "--dry-run",
        ])
        .unwrap();
        if let Commands::Files { command } = cli.command {
            if let commands::files::FilesCommand::Push {
                session, dry_run, ..
            } = command
            {
                assert_eq!(session, "ses_xyz");
                assert!(dry_run);
            } else {
                panic!("Expected Push command");
            }
        } else {
            panic!("Expected Files command");
        }
    }

    #[test]
    fn test_cli_parse_files_pull() {
        let cli = Cli::try_parse_from([
            "everruns",
            "files",
            "pull",
            "--session",
            "ses_xyz",
            "--delete",
        ])
        .unwrap();
        if let Commands::Files { command } = cli.command {
            if let commands::files::FilesCommand::Pull {
                session, delete, ..
            } = command
            {
                assert_eq!(session, "ses_xyz");
                assert!(delete);
            } else {
                panic!("Expected Pull command");
            }
        } else {
            panic!("Expected Files command");
        }
    }

    #[test]
    fn test_cli_parse_files_ls() {
        let cli = Cli::try_parse_from([
            "everruns",
            "files",
            "ls",
            "--session",
            "ses_xyz",
            "-r",
            "-l",
            "/src",
        ])
        .unwrap();
        if let Commands::Files { command } = cli.command {
            if let commands::files::FilesCommand::Ls {
                session,
                path,
                recursive,
                long,
            } = command
            {
                assert_eq!(session, "ses_xyz");
                assert_eq!(path, "/src");
                assert!(recursive);
                assert!(long);
            } else {
                panic!("Expected Ls command");
            }
        } else {
            panic!("Expected Files command");
        }
    }

    #[test]
    fn test_cli_invalid_output_format() {
        let result = Cli::try_parse_from(["everruns", "-o", "invalid", "agents", "list"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_cli_missing_required_args() {
        // Chat requires --session
        let result = Cli::try_parse_from(["everruns", "chat", "Hello"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_cli_help_available() {
        // Verify help can be generated without panic
        let _ = Cli::command().render_help();
    }

    #[test]
    fn test_cli_parse_login() {
        let cli = Cli::try_parse_from(["everruns", "login"]).unwrap();
        if let Commands::Login { token } = cli.command {
            assert!(!token);
        } else {
            panic!("Expected Login command");
        }
    }

    #[test]
    fn test_cli_parse_login_token() {
        let cli = Cli::try_parse_from(["everruns", "login", "--token"]).unwrap();
        if let Commands::Login { token } = cli.command {
            assert!(token);
        } else {
            panic!("Expected Login command");
        }
    }

    #[test]
    fn test_cli_parse_logout() {
        let cli = Cli::try_parse_from(["everruns", "logout"]).unwrap();
        assert!(matches!(cli.command, Commands::Logout));
    }

    #[test]
    fn test_cli_parse_status() {
        let cli = Cli::try_parse_from(["everruns", "status"]).unwrap();
        assert!(matches!(cli.command, Commands::Status));
    }

    #[test]
    fn test_cli_parse_orgs() {
        let cli = Cli::try_parse_from(["everruns", "orgs"]).unwrap();
        assert!(matches!(cli.command, Commands::Orgs { command: None }));
    }

    #[test]
    fn test_cli_parse_orgs_select() {
        let cli = Cli::try_parse_from(["everruns", "orgs", "select"]).unwrap();
        if let Commands::Orgs {
            command: Some(OrgsCommand::Select),
        } = cli.command
        {
            // ok
        } else {
            panic!("Expected Orgs Select command");
        }
    }

    #[test]
    fn test_cli_parse_profile() {
        let cli = Cli::try_parse_from(["everruns", "--profile", "staging", "status"]).unwrap();
        assert_eq!(cli.profile, "staging");
    }

    #[test]
    fn test_cli_parse_connections_set() {
        let cli = Cli::try_parse_from([
            "everruns",
            "connections",
            "set",
            "daytona",
            "--api-key-stdin",
        ])
        .unwrap();
        if let Commands::Connections { command } = cli.command {
            if let commands::connections::ConnectionsCommand::Set {
                provider,
                api_key_stdin,
            } = command
            {
                assert_eq!(provider, "daytona");
                assert!(api_key_stdin);
            } else {
                panic!("Expected Set command");
            }
        } else {
            panic!("Expected Connections command");
        }
    }

    #[test]
    fn test_cli_parse_connections_list() {
        let cli = Cli::try_parse_from(["everruns", "connections", "list"]).unwrap();
        assert!(matches!(
            cli.command,
            Commands::Connections {
                command: commands::connections::ConnectionsCommand::List
            }
        ));
    }

    #[test]
    fn test_cli_parse_connections_remove() {
        let cli = Cli::try_parse_from(["everruns", "connections", "remove", "daytona"]).unwrap();
        if let Commands::Connections { command } = cli.command {
            if let commands::connections::ConnectionsCommand::Remove { provider } = command {
                assert_eq!(provider, "daytona");
            } else {
                panic!("Expected Remove command");
            }
        } else {
            panic!("Expected Connections command");
        }
    }

    // Regression tests for fix(cli): avoid API key in connections set args (#1518).
    //
    // The old `--api-key <value>` flag leaked the provider secret into shell
    // history and `ps` output. The fix removed the flag entirely and routed
    // input through `--api-key-stdin` or the interactive password prompt.
    // These tests lock in that contract at the clap-parser boundary.

    #[test]
    fn connections_set_rejects_legacy_api_key_flag() {
        // Passing `--api-key <value>` must now fail parsing so the secret
        // cannot end up in argv / shell history.
        let result = Cli::try_parse_from([
            "everruns",
            "connections",
            "set",
            "daytona",
            "--api-key",
            "leaked_secret_value",
        ]);
        assert!(
            result.is_err(),
            "--api-key argv flag must be rejected to prevent argv/shell-history leaks"
        );
    }

    #[test]
    fn connections_set_defaults_api_key_stdin_to_false_for_interactive_prompt() {
        // Without `--api-key-stdin`, the flag is false and `run` drops into
        // the dialoguer Password prompt — the secret never touches argv.
        let cli = Cli::try_parse_from(["everruns", "connections", "set", "daytona"]).unwrap();
        if let Commands::Connections { command } = cli.command {
            if let commands::connections::ConnectionsCommand::Set {
                provider,
                api_key_stdin,
            } = command
            {
                assert_eq!(provider, "daytona");
                assert!(
                    !api_key_stdin,
                    "omitting --api-key-stdin must default to interactive prompt"
                );
            } else {
                panic!("Expected Set command");
            }
        } else {
            panic!("Expected Connections command");
        }
    }

    #[test]
    fn connections_set_rejects_positional_api_key_after_provider() {
        // A stray positional arg after `provider` must not bind to anything;
        // clap should reject it rather than silently consuming the secret.
        let result = Cli::try_parse_from([
            "everruns",
            "connections",
            "set",
            "daytona",
            "leaked_secret_value",
        ]);
        assert!(
            result.is_err(),
            "extra positional args after provider must be rejected"
        );
    }

    #[test]
    fn connections_set_requires_provider() {
        let result = Cli::try_parse_from(["everruns", "connections", "set"]);
        assert!(result.is_err(), "provider arg is required");
    }
    #[test]
    fn parses_plugin_lifecycle_commands() {
        use crate::commands::plugins::PluginsCommands;

        let cli = Cli::try_parse_from([
            "everruns",
            "plugins",
            "install",
            "marketplace-id",
            "plugin-name",
        ])
        .unwrap();
        assert!(matches!(
            cli.command,
            Commands::Plugins {
                command: PluginsCommands::Install { marketplace_id, plugin_name }
            } if marketplace_id == "marketplace-id" && plugin_name == "plugin-name"
        ));

        let cli = Cli::try_parse_from(["everruns", "plugins", "uninstall", "plugin-id"]).unwrap();
        assert!(matches!(
            cli.command,
            Commands::Plugins { command: PluginsCommands::Uninstall { id } } if id == "plugin-id"
        ));
    }

    #[test]
    fn parses_skill_lifecycle_commands() {
        use crate::commands::skills::SkillsCommands;
        let cli =
            Cli::try_parse_from(["everruns", "skills", "create", "skills/demo/SKILL.md"]).unwrap();
        assert!(matches!(
            cli.command,
            Commands::Skills { command: SkillsCommands::Create { path } }
                if path == std::path::Path::new("skills/demo/SKILL.md")
        ));

        let cli = Cli::try_parse_from(["everruns", "skills", "delete", "skill-id"]).unwrap();
        assert!(matches!(
            cli.command,
            Commands::Skills { command: SkillsCommands::Delete { id } } if id == "skill-id"
        ));
    }

    #[test]
    fn parses_knowledge_base_lifecycle_commands() {
        use crate::commands::knowledge_bases::KnowledgeBasesCommands;

        let cli = Cli::try_parse_from([
            "everruns",
            "knowledge-bases",
            "create",
            "Runbooks",
            "--description",
            "Operations guides",
            "--embedding-model-id",
            "model-id",
        ])
        .unwrap();
        assert!(matches!(
            cli.command,
            Commands::KnowledgeBases {
                command: KnowledgeBasesCommands::Create {
                    name,
                    description: Some(description),
                    embedding_model_id: Some(embedding_model_id),
                }
            } if name == "Runbooks" && description == "Operations guides" && embedding_model_id == "model-id"
        ));

        let cli = Cli::try_parse_from(["everruns", "knowledge-bases", "delete", "kb-id"]).unwrap();
        assert!(matches!(
            cli.command,
            Commands::KnowledgeBases { command: KnowledgeBasesCommands::Delete { id } } if id == "kb-id"
        ));
    }
}