ralph 0.1.5

A CLI agent harness for running AI coding agents (Codex, Claude, Pi, Gemini)
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
mod cli;
mod config;
mod harness;
mod install;
mod logging;
mod monitor;
mod process;
mod provider;
mod tmux;
mod usage;

use anyhow::{bail, Context, Result};
use clap::Parser;
use dialoguer::{theme::ColorfulTheme, Select};
use std::io::IsTerminal;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tracing::{info, trace};

use cli::{Cli, Commands};
use config::Config;
use harness::{Harness, Runner};

fn check_harness_available(harness: &Harness) -> Result<()> {
    let cmd = harness.exec_command();
    which::which(cmd).with_context(|| {
        format!(
            "'{}' not found in PATH. Please install it first.",
            harness.command_name()
        )
    })?;
    Ok(())
}

fn resolve_prompt(task: &str) -> Result<String> {
    let path = std::path::Path::new(task);
    if path.exists() && path.is_file() {
        std::fs::read_to_string(path).with_context(|| format!("Failed to read task file: {}", task))
    } else {
        Ok(task.to_string())
    }
}

fn print_example_config() {
    println!(
        r#"# Ralph configuration file (.ralphrc or .ralphrc.toml)
# Place in current directory or home directory

# Agent harness: codex, claude, pi, gemini
harness = "codex"

# Model to use (optional, defaults vary by harness)
# model = "gpt-5.2-codex"

# Default task file
task = "TASK.md"

# Number of iterations (number or "inf")
iterations = "1"

# Enable dangerous mode (skip permissions)
dangerous = true

# Reasoning effort for codex
reasoning_effort = "medium"

# Provider for pi harness (anthropic, openai, google, etc.)
# provider = "anthropic"

# Usage limits
# usage_limit_daily = 80
# usage_limit_weekly = 90
# usage_check_interval = 5
# fallback_harness = "gemini"

# Tmux settings
# tmux = false
# tmux_session_prefix = "ralph"
# tmux_attach = false

# Monitor settings
# monitor_interval = "5m"
# monitor_harness = "claude"
"#
    );
}

fn list_harnesses() {
    println!("Available harnesses:");
    println!();
    println!("  codex   - OpenAI Codex CLI agent (default model: gpt-5.2-codex)");
    println!("  claude  - Anthropic Claude CLI agent (default model: claude-opus-4-5-20251101)");
    println!("  pi      - Pi CLI agent (default provider: anthropic, model: claude-opus-4-5)");
    println!("  gemini  - Google Gemini CLI agent (default model: auto-gemini-3)");
}

#[allow(clippy::too_many_arguments)]
fn apply_usage_limits(
    harness: &mut Harness,
    model: &mut String,
    provider: &mut String,
    runner: &mut Runner,
    usage_limit_daily: Option<u8>,
    usage_limit_weekly: Option<u8>,
    fallback_harness: Option<Harness>,
    using_fallback: &mut bool,
    dangerous: bool,
    reasoning: &str,
) -> Result<bool> {
    let check = usage::check_usage_limits(
        harness.command_name(),
        usage_limit_daily,
        usage_limit_weekly,
    );

    for warning in check.warnings {
        eprintln!("Usage check warning: {}", warning);
    }

    if !check.exceeded {
        return Ok(true);
    }

    let reason = check.reasons.join(", ");

    if let Some(fallback) = fallback_harness {
        if !*using_fallback && fallback != *harness {
            check_harness_available(&fallback)?;
            *harness = fallback;
            *model = harness.default_model().to_string();
            if *harness == Harness::Pi && provider.is_empty() {
                *provider = harness.default_provider().to_string();
            }
            *runner = Runner::new(
                *harness,
                model.clone(),
                dangerous,
                reasoning.to_string(),
                provider.clone(),
            );
            *using_fallback = true;
            eprintln!(
                "Usage limit reached ({}). Switching to fallback harness: {}",
                reason,
                harness.command_name()
            );
            return Ok(true);
        }
    }

    eprintln!("Usage limit reached ({}). Stopping.", reason);
    Ok(false)
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    // Handle --log-file flag early (before logging init)
    if cli.log_file {
        println!("{}", logging::current_log_file().display());
        return Ok(());
    }

    // Initialize logging (keep guard alive for program duration)
    let _log_guard = logging::init_logging(cli.verbose, cli.log_stderr)?;

    info!(version = env!("CARGO_PKG_VERSION"), "ralph starting");
    trace!(?cli, "parsed CLI arguments");

    // Handle subcommands first
    if let Some(cmd) = &cli.command {
        match cmd {
            Commands::Providers { json } => {
                let providers = provider::detect_all_providers();
                if *json {
                    provider::print_providers_json(&providers)?;
                } else {
                    provider::print_providers(&providers);
                }
                return Ok(());
            }
            Commands::Usage { provider, json } => {
                let usage_list = if provider == "all" {
                    usage::get_all_usage()
                } else {
                    match usage::get_provider_usage(provider) {
                        Some(info) => vec![info],
                        None => {
                            eprintln!("Unknown provider: {}", provider);
                            return Ok(());
                        }
                    }
                };

                if *json {
                    usage::print_usage_json(&usage_list)?;
                } else {
                    usage::print_usage(&usage_list);
                }
                return Ok(());
            }
            Commands::Monitor {
                monitor_harness,
                monitor_model,
                monitor_interval,
                inner_harness,
                inner_model,
                task,
            } => {
                let config = Config::load()?.unwrap_or_default();

                let monitor_h = Harness::from_str(monitor_harness)?;
                let inner_h = Harness::from_str(inner_harness)?;

                check_harness_available(&monitor_h)?;
                check_harness_available(&inner_h)?;

                let monitor_m = monitor_model
                    .clone()
                    .unwrap_or_else(|| monitor_h.default_model().to_string());
                let inner_m = inner_model
                    .clone()
                    .unwrap_or_else(|| inner_h.default_model().to_string());

                let task_str = task
                    .clone()
                    .or(cli.task.clone())
                    .or(config.task.clone())
                    .unwrap_or_else(|| "TASK.md".to_string());
                let prompt = resolve_prompt(&task_str)?;

                let interval = monitor::parse_duration(monitor_interval)?;

                let dangerous = if cli.safe {
                    false
                } else {
                    config.dangerous.unwrap_or(cli.dangerous)
                };
                let reasoning = config.reasoning_effort.unwrap_or(cli.reasoning.clone());
                let provider_str = cli
                    .provider
                    .clone()
                    .or(config.provider.clone())
                    .unwrap_or_else(|| inner_h.default_provider().to_string());

                let monitor_config = monitor::MonitorConfig {
                    monitor_harness: monitor_h,
                    monitor_model: monitor_m,
                    monitor_interval: interval,
                    inner_harness: inner_h,
                    inner_model: inner_m,
                    task: prompt,
                    dangerous,
                    reasoning_effort: reasoning,
                    provider: provider_str,
                };

                return monitor::run_monitor(monitor_config).await;
            }
            Commands::Install { agent, all, list } => {
                install::run_install(agent.clone(), *all, *list)?;
                return Ok(());
            }
            Commands::Ps { json, all } => {
                let processes = process::list_processes();
                if *json {
                    process::print_processes_json(&processes)?;
                } else {
                    process::print_processes(&processes, *all);
                }
                return Ok(());
            }
            Commands::Kill {
                all,
                dir,
                harness,
                pid,
            } => {
                if let Some(pid) = pid {
                    if process::kill_process(*pid)? {
                        println!("Killed process {}", pid);
                    } else {
                        println!("Failed to kill process {}", pid);
                    }
                } else if *all {
                    let (killed, total) = process::kill_all_processes()?;
                    println!("Killed {}/{} tracked processes", killed, total);
                } else if let Some(dir) = dir {
                    let (killed, total) = process::kill_processes_by_dir(dir)?;
                    println!("Killed {}/{} processes in {}", killed, total, dir);
                } else if let Some(harness) = harness {
                    let (killed, total) = process::kill_processes_by_harness(harness)?;
                    println!("Killed {}/{} {} processes", killed, total, harness);
                } else {
                    // Default: kill processes in current directory
                    let cwd = std::env::current_dir()?.display().to_string();
                    let (killed, total) = process::kill_processes_by_dir(&cwd)?;
                    println!("Killed {}/{} processes in current directory", killed, total);
                }
                return Ok(());
            }
            Commands::Cleanup {
                discover,
                kill_orphans,
            } => {
                // Clean stale entries
                let cleaned = process::cleanup_registry()?;
                println!("Cleaned {} stale process entries", cleaned);

                if *discover {
                    let orphans = process::discover_orphan_processes()?;
                    if orphans.is_empty() {
                        println!("No orphaned agent processes found");
                    } else {
                        println!("Found {} orphaned processes:", orphans.len());
                        process::print_processes(&orphans, false);

                        if *kill_orphans {
                            for proc in &orphans {
                                if process::kill_process(proc.pid).unwrap_or(false) {
                                    println!("Killed orphan {}", proc.pid);
                                }
                            }
                        }
                    }
                }
                return Ok(());
            }
            Commands::Logs {
                lines,
                follow,
                path,
                clear,
            } => {
                let log_file = logging::current_log_file();

                if *path {
                    println!("{}", log_file.display());
                    return Ok(());
                }

                if *clear {
                    let log_dir = logging::log_dir();
                    if log_dir.exists() {
                        for entry in std::fs::read_dir(&log_dir)? {
                            let entry = entry?;
                            if entry
                                .path()
                                .extension()
                                .map(|e| e == "log")
                                .unwrap_or(false)
                                || entry.file_name().to_string_lossy().starts_with("ralph.")
                            {
                                std::fs::remove_file(entry.path())?;
                                println!("Removed: {}", entry.path().display());
                            }
                        }
                    }
                    println!("Logs cleared");
                    return Ok(());
                }

                if !log_file.exists() {
                    println!("No log file found at: {}", log_file.display());
                    println!("Run ralph with -v to enable logging");
                    return Ok(());
                }

                if *follow {
                    // Use tail -f for following
                    let status = std::process::Command::new("tail")
                        .args(["-f", &log_file.to_string_lossy()])
                        .status()?;
                    if !status.success() {
                        bail!("tail command failed");
                    }
                } else {
                    // Read and display last N lines
                    let content = std::fs::read_to_string(&log_file)?;
                    let all_lines: Vec<&str> = content.lines().collect();
                    let start = if *lines == 0 || *lines >= all_lines.len() {
                        0
                    } else {
                        all_lines.len() - *lines
                    };
                    for line in &all_lines[start..] {
                        println!("{}", line);
                    }
                }
                return Ok(());
            }
            Commands::Sessions { list, attach, json } => {
                if !tmux::tmux_available() {
                    bail!("tmux is not available. Please install tmux first.");
                }

                let sessions = tmux::list_ralph_sessions()?;
                if sessions.is_empty() {
                    println!("No ralph tmux sessions found.");
                    return Ok(());
                }

                if *json {
                    tmux::print_sessions_json(&sessions)?;
                    return Ok(());
                }

                if let Some(name) = attach {
                    eprintln!("Attaching to {} (detach with Ctrl+b d)...", name);
                    tmux::attach_session(name)?;
                    return Ok(());
                }

                if *list || !std::io::stdin().is_terminal() || !std::io::stdout().is_terminal() {
                    tmux::print_sessions(&sessions);
                    return Ok(());
                }

                let labels: Vec<String> = sessions.iter().map(tmux::session_label).collect();
                let selection = Select::with_theme(&ColorfulTheme::default())
                    .with_prompt("Select a ralph tmux session")
                    .items(&labels)
                    .default(0)
                    .interact_opt()?;

                if let Some(index) = selection {
                    let name = &sessions[index].name;
                    eprintln!("Attaching to {} (detach with Ctrl+b d)...", name);
                    tmux::attach_session(name)?;
                }

                return Ok(());
            }
        }
    }

    if cli.list_harnesses {
        list_harnesses();
        return Ok(());
    }

    if cli.init {
        print_example_config();
        return Ok(());
    }

    let config = Config::load()?.unwrap_or_default();

    let harness_str = cli
        .harness
        .or(config.harness)
        .unwrap_or_else(|| "codex".to_string());
    let harness = Harness::from_str(&harness_str)?;

    check_harness_available(&harness)?;

    let model = cli
        .model
        .or(config.model)
        .unwrap_or_else(|| harness.default_model().to_string());

    let dangerous = if cli.safe {
        false
    } else {
        config.dangerous.unwrap_or(cli.dangerous)
    };

    let reasoning = config.reasoning_effort.unwrap_or(cli.reasoning);

    let provider_str = cli
        .provider
        .or(config.provider)
        .unwrap_or_else(|| harness.default_provider().to_string());

    let usage_limit_daily = cli.usage_limit_daily.or(config.usage_limit_daily);
    let usage_limit_weekly = cli.usage_limit_weekly.or(config.usage_limit_weekly);
    let usage_check_interval = cli.usage_check_interval.or(config.usage_check_interval);
    let fallback_harness_str = cli.fallback_harness.or(config.fallback_harness);
    let fallback_harness = match &fallback_harness_str {
        Some(name) => Some(Harness::from_str(name)?),
        None => None,
    };

    let task = cli
        .task
        .or(config.task)
        .unwrap_or_else(|| "TASK.md".to_string());

    let prompt = resolve_prompt(&task)?;

    let iterations = config.iterations.unwrap_or(cli.iterations);

    // Determine if we should use tmux
    let use_tmux = if cli.no_tmux {
        false
    } else if cli.tmux {
        true
    } else {
        config.tmux.unwrap_or(false)
    };

    let tmux_attach = cli.tmux_attach || config.tmux_attach.unwrap_or(false);
    let tmux_prefix = config
        .tmux_session_prefix
        .unwrap_or_else(|| "ralph".to_string());

    // If using tmux, start in tmux session
    if use_tmux && tmux::tmux_available() {
        let base_session_name = cli
            .tmux_session
            .unwrap_or_else(|| tmux::generate_session_name(&tmux_prefix, harness.command_name()));
        let session_name = tmux::unique_session_name(&base_session_name);
        if session_name != base_session_name {
            eprintln!(
                "Session name '{}' already exists, using '{}'",
                base_session_name, session_name
            );
        }

        // Build args to pass to ralph (running without tmux inside)
        let mut args = vec![
            "-H".to_string(),
            harness_str,
            "-m".to_string(),
            model,
            "-n".to_string(),
            iterations,
            "--reasoning".to_string(),
            reasoning,
        ];

        if dangerous {
            args.push("--dangerous".to_string());
        } else {
            args.push("--safe".to_string());
        }

        if !provider_str.is_empty() {
            args.push("--provider".to_string());
            args.push(provider_str);
        }

        if let Some(limit) = usage_limit_daily {
            args.push("--usage-limit-daily".to_string());
            args.push(limit.to_string());
        }

        if let Some(limit) = usage_limit_weekly {
            args.push("--usage-limit-weekly".to_string());
            args.push(limit.to_string());
        }

        if let Some(interval) = usage_check_interval {
            args.push("--usage-check-interval".to_string());
            args.push(interval.to_string());
        }

        if let Some(name) = fallback_harness_str {
            args.push("--fallback-harness".to_string());
            args.push(name);
        }

        args.push("--no-tmux".to_string()); // Prevent nesting
        args.push(task);

        return tmux::start_in_tmux(&session_name, "ralph", &args, tmux_attach);
    }

    // Run directly (no tmux)
    let mut current_harness = harness;
    let mut current_model = model;
    let mut current_provider = provider_str;
    let mut runner = Runner::new(
        current_harness,
        current_model.clone(),
        dangerous,
        reasoning.clone(),
        current_provider.clone(),
    );

    let running = Arc::new(AtomicBool::new(true));
    let r = running.clone();

    ctrlc::set_handler(move || {
        eprintln!("\nInterrupted. Cleaning up tracked processes...");
        r.store(false, Ordering::SeqCst);

        // Kill all tracked processes before exiting
        if let Ok((killed, total)) = process::kill_all_processes() {
            if total > 0 {
                eprintln!("Killed {}/{} tracked processes", killed, total);
            }
        }

        std::process::exit(130);
    })?;

    let mut using_fallback = false;
    let should_continue = apply_usage_limits(
        &mut current_harness,
        &mut current_model,
        &mut current_provider,
        &mut runner,
        usage_limit_daily,
        usage_limit_weekly,
        fallback_harness,
        &mut using_fallback,
        dangerous,
        &reasoning,
    )?;
    if !should_continue {
        return Ok(());
    }

    if iterations == "inf" {
        let interval = usage_check_interval.unwrap_or(0);
        let mut iteration = 0u32;
        while running.load(Ordering::SeqCst) {
            runner.run(&prompt).await?;
            iteration = iteration.saturating_add(1);
            if interval > 0 && iteration.is_multiple_of(interval) {
                let should_continue = apply_usage_limits(
                    &mut current_harness,
                    &mut current_model,
                    &mut current_provider,
                    &mut runner,
                    usage_limit_daily,
                    usage_limit_weekly,
                    fallback_harness,
                    &mut using_fallback,
                    dangerous,
                    &reasoning,
                )?;
                if !should_continue {
                    break;
                }
            }
        }
    } else {
        let count: u32 = iterations
            .parse()
            .with_context(|| format!("Invalid iteration count: {}", iterations))?;
        if count == 0 {
            bail!("Iteration count must be at least 1");
        }
        for i in 0..count {
            if !running.load(Ordering::SeqCst) {
                break;
            }
            if count > 1 {
                eprintln!("--- Iteration {}/{} ---", i + 1, count);
            }
            runner.run(&prompt).await?;
            if let Some(interval) = usage_check_interval {
                if interval > 0 && (i + 1) % interval == 0 {
                    let should_continue = apply_usage_limits(
                        &mut current_harness,
                        &mut current_model,
                        &mut current_provider,
                        &mut runner,
                        usage_limit_daily,
                        usage_limit_weekly,
                        fallback_harness,
                        &mut using_fallback,
                        dangerous,
                        &reasoning,
                    )?;
                    if !should_continue {
                        break;
                    }
                }
            }
        }
    }

    Ok(())
}