rho-coding-agent 1.18.2

A lightweight agent harness inspired by Pi
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
use std::{
    io::{self, IsTerminal},
    num::NonZeroUsize,
    sync::Arc,
    time::Duration,
};

use {
    crate::cli::{Cli, Command, CredentialStoreCommand, OutputFormat},
    crate::credential_store::AppCredentialStore,
    crate::diagnostics::RuntimeDiagnostics,
    crate::herdr::HerdrReporter,
    crate::update,
    rho_providers::model::ModelError,
};

use super::{
    agent_binding::{AgentBinder, AgentInvocation, AgentRole},
    automation, automation_protocol, cli_config,
    config_repository::ConfigRepository,
    interactive, login,
    sdk_config::SdkBootstrapOptions,
    sessions_cli,
};

pub async fn run(cli: Cli) -> anyhow::Result<()> {
    let run_output = match &cli.command {
        Some(Command::Run { output, .. }) => Some(*output),
        _ => None,
    };
    let result = run_inner(cli).await;
    let Err(error) = result else {
        return Ok(());
    };
    if error.downcast_ref::<automation::AutomationExit>().is_some()
        || error
            .downcast_ref::<automation::AutomationInterrupted>()
            .is_some()
    {
        return Err(error);
    }
    if run_output == Some(OutputFormat::Jsonl) {
        automation::emit_startup_failure()?;
        return Err(automation::AutomationExit::new(
            2,
            automation_protocol::TerminalReason::ConfigurationError,
            "configuration failed",
        )
        .into());
    }
    if run_output.is_some() {
        return Err(automation::AutomationExit::new(
            2,
            automation_protocol::TerminalReason::ConfigurationError,
            error.to_string(),
        )
        .into());
    }
    Err(error)
}

async fn run_inner(cli: Cli) -> anyhow::Result<()> {
    cli_config::validate(&cli)?;
    if let EarlyDispatch::Handled(result) = dispatch_early_command(&cli).await? {
        return result;
    }

    let PreparedStartup {
        cli,
        mut config,
        config_repository,
        cwd,
        automation_prompt,
        output_file,
        output,
        max_steps,
        timeout,
        bound_agent,
        bound_reasoning_source,
        provider_refresh,
        store,
    } = prepare_startup(cli).await?;

    validate_terminal_mode(&cli)?;
    cli_config::prepare_model_metadata(&config, &store, &provider_refresh).await;
    cli_config::normalize_reasoning_for_cli(&mut config, bound_reasoning_source)?;
    let herdr = HerdrReporter::from_env();
    if let Some(prompt) = automation_prompt {
        return run_automation_startup(AutomationStartup {
            prompt,
            config: &config,
            config_repository: &config_repository,
            cwd,
            cli: &cli,
            bound_agent,
            output_file,
            output,
            max_steps,
            timeout,
            herdr,
        })
        .await;
    }
    run_interactive_startup(InteractiveStartup {
        cli: &cli,
        config,
        config_repository,
        cwd,
        bound_agent,
        bound_reasoning_source,
        herdr,
    })
    .await
}

enum EarlyDispatch {
    Handled(anyhow::Result<()>),
    Continue,
}

async fn dispatch_early_command(cli: &Cli) -> anyhow::Result<EarlyDispatch> {
    if let Some(Command::CredentialStore { command }) = &cli.command {
        return Ok(EarlyDispatch::Handled(run_credential_store_command(
            command,
            cli.config.clone(),
        )));
    }
    if let Some(Command::Sessions { command }) = &cli.command {
        return Ok(EarlyDispatch::Handled(sessions_cli::run(command)));
    }
    if let Some(Command::Attach { id }) = &cli.command {
        return Ok(EarlyDispatch::Handled(
            crate::tui::run_attachment(id, HerdrReporter::from_env()).await,
        ));
    }
    if matches!(cli.command, Some(Command::Update)) {
        return Ok(EarlyDispatch::Handled(
            update::run_update(env!("CARGO_PKG_VERSION")).await,
        ));
    }
    if let Some(Command::Login {
        provider,
        device_auth,
    }) = &cli.command
    {
        let config_repository = ConfigRepository::new(cli.config.clone());
        let mut config = config_repository.load()?;
        let config_path = absolute_config_path(&config_repository)?;
        ensure_cli_credential_store_choice(&mut config, Some(config_path.clone()))?;
        crate::credential_store::initialize_from_config(&mut config, &config_path)?;
        return Ok(EarlyDispatch::Handled(
            login::run(provider, *device_auth).await,
        ));
    }
    Ok(EarlyDispatch::Continue)
}

struct PreparedStartup {
    cli: Cli,
    config: crate::config::Config,
    config_repository: ConfigRepository,
    cwd: std::path::PathBuf,
    automation_prompt: Option<String>,
    output_file: Option<std::path::PathBuf>,
    output: OutputFormat,
    max_steps: Option<NonZeroUsize>,
    timeout: Option<Duration>,
    bound_agent: super::agent_binding::BoundAgent,
    bound_reasoning_source: rho_providers::model::ReasoningRequestSource,
    provider_refresh: cli_config::ProviderRefreshStatus,
    store: AppCredentialStore,
}

async fn prepare_startup(cli: Cli) -> anyhow::Result<PreparedStartup> {
    let config_path = cli.config.clone();
    let config_repository = ConfigRepository::new(config_path.clone());
    let mut config = config_repository.load()?;
    let absolute_config = absolute_config_path(&config_repository)?;
    crate::credential_store::initialize_from_config(&mut config, &absolute_config)?;
    let cwd = std::env::current_dir()?;
    let automation_prompt = automation::prompt_for_command(&cli.command)?;
    let (output_file, output, max_steps, timeout) = match &cli.command {
        Some(Command::Run {
            output_file,
            output,
            max_steps,
            timeout,
            ..
        }) => (output_file.clone(), *output, *max_steps, *timeout),
        _ => (None, OutputFormat::Text, None, None),
    };
    let catalog = crate::agent::AgentCatalog::discover(&cwd)?;
    let selected_agent = cli.agent.as_deref().unwrap_or("default");
    let definition = Arc::new(catalog.find(selected_agent)?.definition.clone());

    let store = AppCredentialStore;
    let provider_refresh = cli_config::refresh_model_cache(&cli, &config, &store).await?;
    let mut save_config = cli_config::apply_overrides(&mut config, &cli)?;
    cli_config::prepare_model_metadata(&config, &store, &provider_refresh).await;
    save_config |= cli_config::normalize_reasoning_for_cli(
        &mut config,
        if cli.reasoning.is_some() {
            rho_providers::model::ReasoningRequestSource::Explicit
        } else {
            rho_providers::model::ReasoningRequestSource::PersistedOrDefault
        },
    )?;
    if save_config {
        config_repository.save(&config)?;
    }
    let reasoning_before_binding = config.reasoning;
    let role = if automation_prompt.is_some() {
        AgentRole::AutomationRoot
    } else {
        AgentRole::InteractiveRoot
    };
    let bound_agent = AgentBinder::bind(
        definition,
        AgentInvocation {
            role,
            available_tools: host_capabilities(&cli, &config, role),
        },
        &config,
    )?;
    config = bound_agent.rho_config().cloned().unwrap_or(config);
    let bound_reasoning_source =
        if cli.reasoning.is_some() && config.reasoning == reasoning_before_binding {
            rho_providers::model::ReasoningRequestSource::Explicit
        } else {
            rho_providers::model::ReasoningRequestSource::PersistedOrDefault
        };

    Ok(PreparedStartup {
        cli,
        config,
        config_repository,
        cwd,
        automation_prompt,
        output_file,
        output,
        max_steps,
        timeout,
        bound_agent,
        bound_reasoning_source,
        provider_refresh,
        store,
    })
}

struct AutomationStartup<'a> {
    prompt: String,
    config: &'a crate::config::Config,
    config_repository: &'a ConfigRepository,
    cwd: std::path::PathBuf,
    cli: &'a Cli,
    bound_agent: super::agent_binding::BoundAgent,
    output_file: Option<std::path::PathBuf>,
    output: OutputFormat,
    max_steps: Option<NonZeroUsize>,
    timeout: Option<Duration>,
    herdr: HerdrReporter,
}

async fn run_automation_startup(startup: AutomationStartup<'_>) -> anyhow::Result<()> {
    let diagnostics = bind_agent_diagnostics(startup.config, &startup.bound_agent);
    automation::run(
        startup.prompt,
        automation::Startup {
            config: startup.config,
            config_path: absolute_config_path(startup.config_repository)?,
            cwd: startup.cwd,
            no_system_prompt: startup.cli.no_system_prompt,
            no_tools: startup.cli.no_tools,
            no_subagents: startup.cli.no_subagents,
            usage_purpose: "agent",
            parent_session_id: None,
            agent: startup.bound_agent,
            output_file: startup.output_file,
            output: startup.output,
            max_steps: startup.max_steps,
            timeout: startup.timeout,
            diagnostics,
            herdr: startup.herdr,
            host_input: None,
        },
    )
    .await
}

struct InteractiveStartup<'a> {
    cli: &'a Cli,
    config: crate::config::Config,
    config_repository: ConfigRepository,
    cwd: std::path::PathBuf,
    bound_agent: super::agent_binding::BoundAgent,
    bound_reasoning_source: rho_providers::model::ReasoningRequestSource,
    herdr: HerdrReporter,
}

async fn run_interactive_startup(startup: InteractiveStartup<'_>) -> anyhow::Result<()> {
    let diagnostics = bind_agent_diagnostics(&startup.config, &startup.bound_agent);

    let pending_update_notice = startup
        .config
        .check_for_updates
        .then(|| tokio::spawn(update::update_notice(env!("CARGO_PKG_VERSION"))));

    let sdk_options = SdkBootstrapOptions::from_config(&startup.config, &startup.cwd)?;
    let credentials = rho_providers::auth::provider_credentials::ApplicationCredentialSource::new(
        Arc::new(AppCredentialStore),
    );
    let provider_result = rho_providers::providers::build_sdk_provider_with_source(
        sdk_options.provider,
        &credentials,
    );
    let (missing_auth_error, missing_auth_model_error) = match provider_result {
        Ok(_) => (None, None),
        Err(error) if is_interactive_startup_unavailable_error(&error) => {
            (Some(error.to_string()), Some(error))
        }
        Err(error) => return Err(error.into()),
    };
    interactive::run(interactive::Startup {
        cli: startup.cli,
        config: startup.config,
        config_path: absolute_config_path(&startup.config_repository)?,
        config_repository: startup.config_repository,
        cwd: startup.cwd,
        missing_auth_error,
        missing_auth_model_error,
        pending_update_notice,
        diagnostics,
        herdr: startup.herdr,
        agent: startup.bound_agent,
        reasoning_source: startup.bound_reasoning_source,
    })
    .await
}

fn bind_agent_diagnostics(
    config: &crate::config::Config,
    agent: &super::agent_binding::BoundAgent,
) -> RuntimeDiagnostics {
    let diagnostics = RuntimeDiagnostics::new(config);
    diagnostics.update_agent(agent.id().as_str(), &agent.fingerprint().to_string());
    diagnostics
}

fn ensure_cli_credential_store_choice(
    config: &mut crate::config::Config,
    config_path: Option<std::path::PathBuf>,
) -> anyhow::Result<()> {
    use rho_providers::credentials::CredentialStoreBackend;
    use std::io::{self, IsTerminal, Write};

    let Some(request) = crate::credential_store::choice_request(config) else {
        return Ok(());
    };

    if !io::stdin().is_terminal() || !io::stderr().is_terminal() {
        anyhow::bail!(
            "credential store is unset; set it before non-interactive login with \
`rho credential-store set os|file`, behavior.credential_store in config.toml, \
or RHO_CREDENTIAL_STORE=os|file"
        );
    }

    let backends = request.available_backends();
    if backends.is_empty() {
        anyhow::bail!(
            "no credential store backend is available (os: {}; file: {})",
            request.os.detail,
            request.file.detail
        );
    }

    eprintln!("Choose where Rho stores provider credentials:");
    eprintln!("This is saved to config and used for future logins on this machine.");
    if request.os.available {
        eprintln!("  [1] OS credential store (recommended)");
    } else {
        eprintln!(
            "  [1] OS credential store (unavailable: {})",
            request.os.detail
        );
    }
    if request.file.available {
        eprintln!("  [2] Local file under ~/.rho/credentials (not encrypted at rest)");
    } else {
        eprintln!("  [2] Local file (unavailable: {})", request.file.detail);
    }
    let default_backend = request
        .default_backend()
        .unwrap_or(CredentialStoreBackend::Os);
    let default_hint = match default_backend {
        CredentialStoreBackend::Os => "1",
        CredentialStoreBackend::File => "2",
    };
    eprint!("Choice [1/2 or os/file] (default {default_hint}): ");
    io::stderr().flush()?;

    let mut answer = String::new();
    io::stdin().read_line(&mut answer)?;
    let backend = match answer.trim() {
        "" => default_backend,
        "1" | "os" | "OS" => CredentialStoreBackend::Os,
        "2" | "file" | "FILE" => CredentialStoreBackend::File,
        other => {
            anyhow::bail!("unrecognized credential store choice '{other}'; expected 1/os or 2/file")
        }
    };
    if !backends.contains(&backend) {
        let detail = request.detail_for(backend);
        anyhow::bail!(
            "{} credential store is unavailable: {detail}",
            backend.as_str()
        );
    }

    let path = crate::credential_store::set_backend(backend, config_path)?;
    config.credential_store = Some(backend);
    eprintln!(
        "credential store set to {} in {}",
        backend.as_str(),
        path.display()
    );
    Ok(())
}

fn run_credential_store_command(
    command: &CredentialStoreCommand,
    config_path: Option<std::path::PathBuf>,
) -> anyhow::Result<()> {
    match command {
        CredentialStoreCommand::Probe { backend } => {
            let result = crate::credential_store::probe(*backend);
            if result.available {
                println!("available: {}", result.detail);
                Ok(())
            } else {
                anyhow::bail!(result.detail)
            }
        }
        CredentialStoreCommand::Status => {
            // Saved config policy only (ignore RHO_CREDENTIAL_STORE).
            match crate::credential_store::saved_policy_backend(config_path.as_deref())? {
                None => println!("unset"),
                Some(backend) => println!("{}", backend.as_str()),
            }
            Ok(())
        }
        CredentialStoreCommand::Set { backend } => {
            let path = crate::credential_store::set_backend(*backend, config_path)?;
            println!(
                "credential store set to {} in {}",
                backend.as_str(),
                path.display()
            );
            Ok(())
        }
    }
}

fn host_capabilities(
    cli: &Cli,
    config: &crate::config::Config,
    role: AgentRole,
) -> crate::agent::AgentCapabilities {
    use crate::agent::ToolCapability;

    if cli.no_tools {
        return crate::agent::AgentCapabilities::default();
    }
    let mut tools = crate::agent::AgentCapabilities::all_host_tools();
    if !crate::tools::web::access_tools(config).is_available() {
        tools.remove(&ToolCapability::WebSearch);
    }
    #[cfg(windows)]
    tools.remove(&ToolCapability::Bash);
    #[cfg(not(windows))]
    tools.remove(&ToolCapability::Powershell);
    if cli.no_subagents || !config.enable_subagents {
        tools.remove(&ToolCapability::Agent);
        tools.remove(&ToolCapability::Agents);
    }
    if role != AgentRole::InteractiveRoot {
        tools.remove(&ToolCapability::Questionnaire);
    }
    #[cfg(debug_assertions)]
    if std::env::var_os("RHO_TUI_TEST_MODE").as_deref() == Some(std::ffi::OsStr::new("matrix")) {
        tools.insert(ToolCapability::Extension(
            crate::tools::tui_fixture::NAME.into(),
        ));
    }
    tools
}

fn absolute_config_path(repository: &ConfigRepository) -> anyhow::Result<std::path::PathBuf> {
    let path = repository.configured_path()?;
    if path.is_absolute() {
        Ok(path)
    } else {
        Ok(std::env::current_dir()?.join(path))
    }
}

fn validate_terminal_mode(cli: &Cli) -> anyhow::Result<()> {
    if cli.command.is_none() && (!io::stdin().is_terminal() || !io::stdout().is_terminal()) {
        anyhow::bail!(
            "rho's default mode is the interactive TUI; use `rho run` for non-interactive automation"
        );
    }
    Ok(())
}

fn is_interactive_startup_unavailable_error(error: &ModelError) -> bool {
    matches!(
        error,
        ModelError::MissingApiKey
            | ModelError::MissingCodexAuth
            | ModelError::MissingAnthropicApiKey
            | ModelError::MissingGithubCopilotAuth
            | ModelError::MissingXaiApiKey
            | ModelError::MissingXaiAuth
            | ModelError::Credentials(_)
            | ModelError::UnsupportedProvider(_)
    )
}

#[cfg(test)]
#[path = "bootstrap_tests.rs"]
mod tests;