everruns-cli 0.50.1

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
#![cfg_attr(test, allow(clippy::unwrap_used, clippy::expect_used))]
// Everruns CLI
//
// Design Decision: Use clap derive for ergonomic argument parsing.
// Design Decision: Support text/json output formats for scripting.
// Design Decision: No SDK. Platform commands go through the shared contract
// mapper and POST /v1/commands; the rest is plain HTTP (see contract.rs).
// Design Decision: Credential file (platform config dir/everruns/credentials.json) with env var override.

mod auth;
mod browser;
mod commands;
mod contract;
mod events;
mod output;
#[cfg(test)]
mod reference;
mod user_dirs;

use clap::{CommandFactory, FromArgMatches, Parser, Subcommand};

#[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 (default: the profile you signed
    /// in to last)
    #[arg(long, global = true, env = "EVERRUNS_PROFILE")]
    pub profile: Option<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,

    /// Show who the server says you are, and your token's access
    Whoami,

    /// Organizations you belong to, and which one is active
    Orgs {
        #[command(subcommand)]
        command: Option<OrgsCommand>,
    },

    /// Agent definitions and their configuration
    Agents {
        #[command(subcommand)]
        command: commands::agents::AgentsCommand,
    },

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

    /// Running and archived sessions, their state and participants
    Sessions {
        #[command(subcommand)]
        command: commands::sessions::SessionsCommand,
    },

    /// Sync files between a local folder and a session
    Files {
        #[command(subcommand)]
        command: commands::files::FilesCommand,
    },

    /// Run Everruns on this machine with Docker
    Docker {
        #[command(subcommand)]
        command: commands::docker::DockerCommand,
    },

    /// 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,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Every failure passes through one place that explains the ones a person
    // can act on, such as a read-only token refused a write.
    Box::pin(run()).await.map_err(commands::api::explain)
}

async fn run() -> anyhow::Result<()> {
    everruns_contracts::install_default_crypto_provider();

    // The CLI's own tree, plus every contract command it does not hand-write.
    // Parsing happens once, against the merged tree, so a mounted command gets
    // the same global flags and the same help as a hand-written one.
    let root = contract::augment(Cli::command());
    let matches = root.clone().get_matches();

    let output_format = output::OutputFormat::from_str(
        matches
            .get_one::<String>("output")
            .map(String::as_str)
            .unwrap_or("text"),
    );

    // A contract command is decided before credentials are resolved, so a
    // hand-written command that needs none (`login`, `status`) is not made to
    // produce one.
    if let Some((contract, leaf)) = contract::selected(&matches) {
        let creds = auth::resolve_credentials(
            matches.get_one::<String>("api_key").map(String::as_str),
            matches.get_one::<String>("api_url").map(String::as_str),
            matches.get_one::<String>("profile").map(String::as_str),
        )?;
        return Box::pin(contract::dispatch(
            contract,
            leaf,
            &creds.api_url,
            &creds.api_key,
            creds.org_id.as_deref(),
            output_format,
        ))
        .await;
    }

    let cli = Cli::from_arg_matches(&matches)?;

    if let Commands::Agents { command } = &cli.command
        && let Some(result) = commands::agents::run_local(command, output_format)
    {
        return result;
    }

    // Commands that don't need authentication
    match &cli.command {
        Commands::Login { token } => {
            // Signing in names its profile, `default` unless one is given;
            // it then becomes the current one.
            return Box::pin(commands::login::run(
                cli.api_url.as_deref(),
                *token,
                cli.profile.as_deref().unwrap_or("default"),
            ))
            .await;
        }
        Commands::Logout => {
            return commands::logout::run(&auth::profile_name(cli.profile.as_deref()));
        }
        Commands::Status => {
            return commands::status::run(&auth::profile_name(cli.profile.as_deref()));
        }
        Commands::Orgs { command } => {
            let profile = auth::profile_name(cli.profile.as_deref());
            return match command {
                Some(OrgsCommand::Select) => Box::pin(commands::orgs::run_select(&profile)).await,
                None => Box::pin(commands::orgs::run_list(output_format, &profile)).await,
            };
        }
        Commands::Docker { .. } => {
            let Commands::Docker { command } = cli.command else {
                unreachable!()
            };
            return Box::pin(commands::docker::run(command)).await;
        }
        _ => {}
    }

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

    let client = commands::api::ApiClient::new(&api_url, &api_key, org_id.as_deref());

    // 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,
                &api_url,
                &api_key,
                org_id.as_deref(),
                output_format,
                cli.quiet,
            ))
            .await
        }
        Commands::Connections { command } => {
            Box::pin(commands::connections::run(
                command,
                &api_url,
                &api_key,
                output_format,
                cli.quiet,
            ))
            .await
        }
        Commands::Sessions { command } => {
            Box::pin(commands::sessions::run(
                command,
                client,
                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::Whoami => Box::pin(commands::whoami::run(&client, output_format)).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 { .. }
        | Commands::Docker { .. } => {
            unreachable!()
        }
    }
}

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

    /// Regression: export's file flag used to share the global `--output`
    /// id, so the format default ("text") became the file path.
    #[test]
    fn sessions_export_writes_stdout_unless_out_is_given() {
        let cli = Cli::try_parse_from(["everruns", "sessions", "export", "ses_1"]).unwrap();
        let Commands::Sessions {
            command: commands::sessions::SessionsCommand::Export { out, .. },
        } = cli.command
        else {
            panic!("expected sessions export");
        };
        assert_eq!(out, None);

        let cli = Cli::try_parse_from([
            "everruns", "-o", "json", "sessions", "export", "ses_1", "--out", "a.jsonl",
        ])
        .unwrap();
        assert_eq!(cli.output, "json");
        let Commands::Sessions {
            command: commands::sessions::SessionsCommand::Export { out, .. },
        } = cli.command
        else {
            panic!("expected sessions export");
        };
        assert_eq!(out.as_deref(), Some("a.jsonl"));
    }

    // 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"
        );
    }
}

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

    /// The command line this CLI ships, as a diffable rendering: the commands
    /// written by hand here, and the contract commands mounted beside them.
    ///
    /// Humans and their scripts already type these words, so what they depend
    /// on is pinned rather than described. It guards in both directions now: a
    /// hand-written flag that moves shows up as a changed line, and so does a
    /// command added or re-spelled in the control plane, which reaches this
    /// binary through the contract without anyone editing this crate.
    const GOLDEN: &str = include_str!("../contract.golden");

    #[test]
    fn the_shipped_command_line_has_not_changed() {
        let rendered = everruns_cli_contract::render::tree(&contract::augment(Cli::command()));

        if std::env::var("UPDATE_CLI_CONTRACT").is_ok() {
            std::fs::write(
                concat!(env!("CARGO_MANIFEST_DIR"), "/contract.golden"),
                format!("{rendered}\n"),
            )
            .expect("write contract golden");
            return;
        }

        if let Some(report) = everruns_cli_contract::render::diff(GOLDEN, &rendered) {
            panic!(
                "the CLI's command line changed:\n\n{report}\nEvery line here is \
                 something a person or a script already types. If the change is \
                 deliberate, run `UPDATE_CLI_CONTRACT=1 cargo test -p everruns-cli \
                 the_shipped_command_line` and say in the commit message what moved \
                 and why it is safe."
            );
        }
    }
}

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

    /// The generated CLI reference in `docs/reference/cli/` matches this binary.
    ///
    /// The pages are what a web search for an `everruns` command lands on, so
    /// a stale page sends a reader (person or agent) to a flag that moved.
    #[test]
    fn the_cli_reference_pages_are_current() {
        let root = concat!(env!("CARGO_MANIFEST_DIR"), "/../..");
        let dir = std::path::Path::new(root).join(reference::DIR);
        let pages = reference::pages(&contract::augment(Cli::command()));

        if std::env::var("UPDATE_CLI_REFERENCE").is_ok() {
            if dir.exists() {
                std::fs::remove_dir_all(&dir).expect("clear reference dir");
            }
            std::fs::create_dir_all(&dir).expect("create reference dir");
            for (name, text) in &pages {
                std::fs::write(dir.join(name), text).expect("write reference page");
            }
            return;
        }

        let mut stale = Vec::new();
        for (name, text) in &pages {
            match std::fs::read_to_string(dir.join(name)) {
                Ok(on_disk) if on_disk == *text => {}
                _ => stale.push(name.clone()),
            }
        }
        let on_disk: Vec<String> = std::fs::read_dir(&dir)
            .map(|entries| {
                entries
                    .filter_map(Result::ok)
                    .map(|entry| entry.file_name().to_string_lossy().to_string())
                    .filter(|name| !pages.contains_key(name))
                    .collect()
            })
            .unwrap_or_default();
        stale.extend(on_disk);
        assert!(
            stale.is_empty(),
            "docs/reference/cli is stale ({stale:?}). Run \
             `UPDATE_CLI_REFERENCE=1 cargo test -p everruns-cli the_cli_reference`."
        );
    }
}