octorus 0.6.2

A TUI tool for GitHub PR review, designed for Helix editor users
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
use anyhow::Result;
use clap::{Parser, Subcommand};
use crossterm::{
    execute,
    terminal::{disable_raw_mode, LeaveAlternateScreen},
};
use notify::{Config, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use std::ffi::OsString;
use std::io;
use std::panic;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

// Use modules from the library crate
use octorus::app::RefreshRequest;
use octorus::{app, cache, config, github, headless, loader, syntax};

// init/update/migrate are only used by the binary, not needed for benchmarks
mod init;
mod local_comments;
mod migrate;
mod update;

#[derive(Parser, Debug)]
#[command(name = "or")]
#[command(
    about = "TUI for GitHub PRs, issues, local diffs, and Git Ops. AI-powered automated review cycles."
)]
#[command(version)]
struct Args {
    #[command(subcommand)]
    command: Option<Commands>,

    /// Repository name (e.g., "owner/repo"). Auto-detected from current directory if omitted.
    #[arg(short, long)]
    repo: Option<String>,

    /// Pull request number. Shows PR list if flag only (no number).
    #[arg(short, long, conflicts_with = "local", num_args = 0..=1, default_missing_value = "0")]
    pr: Option<u32>,

    /// Start AI Rally mode directly
    #[arg(long, default_value = "false")]
    ai_rally: bool,

    /// Show local git diff against current HEAD (no GitHub PR fetch)
    #[arg(long, default_value = "false", conflicts_with = "pr")]
    local: bool,

    /// Issue number. Shows issue detail directly if provided, issue list if flag only.
    #[arg(short, long, conflicts_with_all = ["pr", "local"], num_args = 0..=1, default_missing_value = "0")]
    issue: Option<u32>,

    /// Start in Git Ops view directly
    #[arg(long, default_value = "false")]
    git_ops: bool,

    /// Auto-focus changed file when local diff updates (for local mode)
    #[arg(long, default_value = "false")]
    auto_focus: bool,

    /// Working directory for AI agents (default: current directory)
    #[arg(long)]
    working_dir: Option<String>,

    /// Accept local .octorus/ overrides for AI settings in headless mode.
    /// Without this flag, headless AI Rally will refuse to run if the local config
    /// overrides security-sensitive keys (ai.reviewer, ai.reviewee, ai.*_additional_tools,
    /// ai.auto_post, ai.prompt_dir) or local prompt files are detected in .octorus/prompts/.
    #[arg(long, default_value = "false")]
    accept_local_overrides: bool,

    /// Write JSON result to a file (in addition to stdout).
    /// Useful when running as a background task where stdout may not be captured.
    #[arg(long)]
    output: Option<String>,
}

#[derive(Subcommand, Debug)]
enum Commands {
    /// Initialize configuration files and prompt templates
    Init {
        /// Force overwrite existing files
        #[arg(long, default_value = "false")]
        force: bool,
        /// Create local .octorus/ config in project root
        #[arg(long, default_value = "false")]
        local: bool,
    },
    /// Remove AI Rally session data
    Clean,
    /// Show saved local comments for the current worktree
    LocalComments {
        /// Repository name (e.g., "owner/repo"). Auto-detected if omitted.
        #[arg(short, long)]
        repo: Option<String>,

        /// Working directory used to scope local comments (defaults to current directory)
        #[arg(long)]
        working_dir: Option<String>,

        /// Maximum number of newest comments to show
        #[arg(short, long, default_value_t = 20)]
        limit: usize,

        /// Print JSON instead of plain text
        #[arg(long, default_value = "false")]
        json: bool,

        /// Show all comments, including resolved ones
        #[arg(long, conflicts_with_all = ["resolved", "purge"])]
        all: bool,

        /// Show only resolved comments
        #[arg(long, conflicts_with_all = ["all", "purge"])]
        resolved: bool,

        /// Delete all local comments for this (repo, working_dir) pair
        #[arg(long, default_value = "false", conflicts_with_all = ["all", "resolved", "json", "limit"])]
        purge: bool,
    },
    /// Update saved local comments for the current worktree
    UpdateLocalComment {
        /// Repository name (e.g., "owner/repo"). Auto-detected if omitted.
        #[arg(short, long)]
        repo: Option<String>,

        /// Working directory used to scope local comments (defaults to current directory)
        #[arg(long)]
        working_dir: Option<String>,

        /// Mark the specified comments as resolved
        #[arg(long, conflicts_with = "reopen")]
        resolve: bool,

        /// Reopen the specified comments
        #[arg(long, conflicts_with = "resolve")]
        reopen: bool,

        /// One or more local comment IDs to update
        #[arg(required = true, num_args = 1.., value_name = "ID")]
        ids: Vec<u64>,
    },
    /// Update to the latest version from GitHub Releases
    Update,
    /// Migrate configuration files and prompts after an update
    Migrate {
        /// Show what would change without applying
        #[arg(long, default_value = "false")]
        dry_run: bool,
        /// Migrate project-local .octorus/ config
        #[arg(long, default_value = "false")]
        local: bool,
        /// Overwrite all files regardless of customization
        #[arg(long, default_value = "false")]
        force: bool,
    },
}

/// Print ASCII art logo with nebula gradient (#eaafc8 → #654ea3)
fn print_logo() {
    use crossterm::style::{Color, Print, ResetColor, SetForegroundColor};
    use std::io::IsTerminal;

    const LOGO_LINES: [&str; 6] = [
        r"  ██████╗   ██████╗ ████████╗  ██████╗  ██████╗  ██╗   ██╗ ███████╗",
        r" ██╔═══██╗ ██╔════╝ ╚══██╔══╝ ██╔═══██╗ ██╔══██╗ ██║   ██║ ██╔════╝",
        r" ██║   ██║ ██║         ██║    ██║   ██║ ██████╔╝ ██║   ██║ ███████╗",
        r" ██║   ██║ ██║         ██║    ██║   ██║ ██╔══██╗ ██║   ██║ ╚════██║",
        r" ╚██████╔╝ ╚██████╗    ██║    ╚██████╔╝ ██║  ██║ ╚██████╔╝ ███████║",
        r"  ╚═════╝   ╚═════╝    ╚═╝     ╚═════╝  ╚═╝  ╚═╝  ╚═════╝  ╚══════╝",
    ];

    let mut stdout = io::stdout();
    let use_color = stdout.is_terminal();

    if use_color {
        const START: (u8, u8, u8) = (234, 175, 200); // #eaafc8
        const END: (u8, u8, u8) = (101, 78, 163); // #654ea3
        let steps = (LOGO_LINES.len() - 1) as f32;

        for (i, line) in LOGO_LINES.iter().enumerate() {
            let t = i as f32 / steps;
            let r = (START.0 as f32 + (END.0 as f32 - START.0 as f32) * t) as u8;
            let g = (START.1 as f32 + (END.1 as f32 - START.1 as f32) * t) as u8;
            let b = (START.2 as f32 + (END.2 as f32 - START.2 as f32) * t) as u8;
            let _ = execute!(
                stdout,
                SetForegroundColor(Color::Rgb { r, g, b }),
                Print(line),
                ResetColor,
                Print("\n")
            );
        }
    } else {
        for line in &LOGO_LINES {
            println!("{line}");
        }
    }
    println!();
}

fn is_root_help(raw_args: &[OsString]) -> bool {
    raw_args.len() == 1
        && raw_args[0]
            .to_str()
            .is_some_and(|arg| arg == "-h" || arg == "--help")
}

/// Restore terminal to normal state
fn restore_terminal() {
    octorus::ui::cleanup_keyboard_enhancement();
    let _ = disable_raw_mode();
    let _ = execute!(io::stdout(), LeaveAlternateScreen);
}

/// Set up panic hook to restore terminal on panic
fn setup_panic_hook() {
    let original_hook = panic::take_hook();
    panic::set_hook(Box::new(move |panic_info| {
        restore_terminal();
        original_hook(panic_info);
    }));
}

#[tokio::main]
async fn main() -> Result<()> {
    // Set up panic hook before anything else
    setup_panic_hook();

    // OR_DEBUG=1 enables file logging (file, not stderr, to avoid corrupting the TUI)
    if std::env::var("OR_DEBUG").ok().as_deref() == Some("1") {
        let log_dir = cache::cache_dir();
        if std::fs::create_dir_all(&log_dir).is_ok() {
            if let Ok(log_file) = std::fs::File::options()
                .create(true)
                .append(true)
                .open(log_dir.join("debug.log"))
            {
                use tracing_subscriber::EnvFilter;
                tracing_subscriber::fmt()
                    .with_writer(std::sync::Mutex::new(log_file))
                    .with_env_filter(EnvFilter::new("octorus=debug,or=debug"))
                    .init();
                tracing::info!("Debug logging enabled");
            }
        }
    }

    let raw_args: Vec<OsString> = std::env::args_os().skip(1).collect();
    if is_root_help(&raw_args) {
        use clap::CommandFactory;
        print_logo();
        Args::command().print_help()?;
        println!();
        return Ok(());
    }

    let args = Args::parse();

    // Handle subcommands
    if let Some(command) = args.command {
        return match command {
            Commands::Init { force, local } => init::run_init(force, local),
            Commands::Clean => {
                cache::cleanup_rally_sessions();
                let rally_dir = cache::cache_dir().join("rally");
                println!("Rally sessions cleaned: {}", rally_dir.display());
                Ok(())
            }
            Commands::LocalComments {
                repo,
                working_dir,
                limit,
                json,
                all,
                resolved,
                purge,
            } => {
                if purge {
                    local_comments::purge_local_comments_command(repo, working_dir).await
                } else {
                    local_comments::show_local_comments_command(
                        repo,
                        working_dir,
                        limit,
                        json,
                        all,
                        resolved,
                    )
                    .await
                }
            }
            Commands::UpdateLocalComment {
                repo,
                working_dir,
                resolve,
                reopen,
                ids,
            } => {
                local_comments::update_local_comments_command(
                    repo,
                    working_dir,
                    resolve,
                    reopen,
                    ids,
                )
                .await
            }
            Commands::Update => {
                update::run_update()?;
                Ok(())
            }
            Commands::Migrate {
                dry_run,
                local,
                force,
            } => migrate::run_migrate(dry_run, local, force),
        };
    }

    let is_no_args = args.pr.is_none() && !args.local && args.issue.is_none() && !args.git_ops;

    let (repo, repo_available) = match args.repo.clone() {
        Some(r) => (r, true),
        None => {
            if args.local || is_no_args {
                match github::detect_repo().await {
                    Ok(r) => (r, true),
                    Err(_) => ("local".to_string(), false),
                }
            } else {
                match github::detect_repo().await {
                    Ok(r) => (r, true),
                    Err(e) => {
                        eprintln!("Error: {}", e);
                        std::process::exit(1);
                    }
                }
            }
        }
    };

    if is_no_args {
        let config = if let Some(ref dir) = args.working_dir {
            config::Config::load_for_dir(Path::new(dir))?
        } else {
            config::Config::load()?
        };
        return run_with_cockpit(&repo, config, &args, repo_available).await;
    }

    // Pre-initialize syntax highlighting in background to avoid delay on first diff view
    std::thread::spawn(|| {
        let _ = syntax::syntax_set();
        let _ = syntax::theme_set();
    });

    let config = if let Some(ref dir) = args.working_dir {
        config::Config::load_for_dir(Path::new(dir))?
    } else {
        config::Config::load()?
    };

    // Headless mode: --ai-rally with --pr <number> or --local bypasses TUI entirely
    if args.ai_rally && matches!(args.pr, Some(pr) if pr > 0) {
        let pr = args.pr.unwrap();
        let working_dir = resolve_working_dir(&args);
        match headless::run_headless_rally(
            &repo,
            pr,
            &config,
            working_dir.as_deref(),
            args.accept_local_overrides,
            args.output.as_deref(),
        )
        .await
        {
            Ok(approved) => std::process::exit(if approved { 0 } else { 1 }),
            Err(e) => {
                headless::write_error_json(&e.to_string(), args.output.as_deref());
                eprintln!("Error: {}", e);
                std::process::exit(1);
            }
        }
    }
    if args.local && args.ai_rally {
        let working_dir = resolve_working_dir(&args);
        match headless::run_headless_rally_local(
            &repo,
            &config,
            working_dir.as_deref(),
            args.accept_local_overrides,
            args.output.as_deref(),
        )
        .await
        {
            Ok(approved) => std::process::exit(if approved { 0 } else { 1 }),
            Err(e) => {
                headless::write_error_json(&e.to_string(), args.output.as_deref());
                eprintln!("Error: {}", e);
                std::process::exit(1);
            }
        }
    }

    if args.local {
        run_with_local_diff(&repo, &config, &args).await
    } else if let Some(pr) = args.pr.filter(|&n| n > 0) {
        run_with_pr(&repo, pr, &config, &args).await
    } else {
        run_with_pr_list(&repo, config, &args, args.issue).await
    }
}

async fn run_with_local_diff(repo: &str, config: &config::Config, args: &Args) -> Result<()> {
    let (retry_tx, mut retry_rx) = mpsc::channel::<RefreshRequest>(1);
    let (mut app, tx) = app::App::new_loading(repo, 0, config.clone());
    let working_dir = args.working_dir.clone();
    let refresh_pending = Arc::new(AtomicBool::new(false));

    app.set_retry_sender(retry_tx.clone());
    start_update_check(&mut app);
    setup_local_watch(retry_tx, working_dir.clone(), refresh_pending.clone());
    app.set_local_mode(true);
    app.set_local_auto_focus(args.auto_focus);
    setup_working_dir(&mut app, args);

    if args.ai_rally {
        app.set_start_ai_rally_on_load(true);
    }
    if args.git_ops {
        app.open_git_ops();
    }

    let cancel_token = CancellationToken::new();
    let token_clone = cancel_token.clone();
    let repo = repo.to_string();

    loader::fetch_local_diff(repo.clone(), working_dir.clone(), tx.clone()).await;

    tokio::spawn(async move {
        tokio::select! {
            _ = token_clone.cancelled() => {}
            _ = async {
                while let Some(request) = retry_rx.recv().await {
                    match request {
                        RefreshRequest::LocalRefresh => {
                            refresh_pending.store(false, Ordering::Release);

                            loop {
                                let tx_retry = tx.clone();
                                loader::fetch_local_diff(repo.clone(), working_dir.clone(), tx_retry).await;

                                if !refresh_pending.swap(false, Ordering::AcqRel) {
                                    break;
                                }
                            }
                        }
                        RefreshRequest::PrRefresh { .. } => {
                            // In local mode, pr_number == 0 is a dummy value that would
                            // produce invalid API calls, so treat PrRefresh as LocalRefresh.
                            let tx_retry = tx.clone();
                            loader::fetch_local_diff(repo.clone(), working_dir.clone(), tx_retry).await;
                        }
                    }
                }
            } => {}
        }
    });

    let result = app.run().await;
    cancel_token.cancel();

    if let Err(ref e) = result {
        restore_terminal();
        eprintln!("Error: {:#}", e);
    }

    let exit_code = if result.is_ok() { 0 } else { 1 };
    std::process::exit(exit_code);
}

fn setup_local_watch(
    refresh_tx: mpsc::Sender<RefreshRequest>,
    working_dir: Option<String>,
    refresh_pending: Arc<AtomicBool>,
) {
    let watch_dir = working_dir.unwrap_or_else(|| {
        std::env::current_dir()
            .map(|path| path.to_string_lossy().to_string())
            .unwrap_or_else(|_| ".".to_string())
    });

    std::thread::spawn({
        let refresh_tx = refresh_tx.clone();
        move || {
            let callback = move |result: notify::Result<notify::Event>| {
                let Ok(event) = result else {
                    return;
                };

                let should_refresh = should_refresh_local_change(&event.paths, &event.kind);

                if should_refresh && !refresh_pending.swap(true, Ordering::AcqRel) {
                    let _ = refresh_tx.try_send(RefreshRequest::LocalRefresh);
                }
            };

            let Ok(mut watcher) = RecommendedWatcher::new(callback, Config::default()) else {
                return;
            };

            let _ = watcher.watch(Path::new(&watch_dir), RecursiveMode::Recursive);

            loop {
                std::thread::sleep(Duration::from_secs(60));
            }
        }
    });
}

fn should_refresh_local_change(paths: &[PathBuf], kind: &EventKind) -> bool {
    !matches!(kind, EventKind::Access(_))
        && paths
            .iter()
            .any(|path| !is_git_file(path) && !is_octorus_config_file(path))
}

fn is_git_file(path: &Path) -> bool {
    path.components()
        .any(|component| component.as_os_str() == ".git")
}

fn is_octorus_config_file(path: &Path) -> bool {
    path.components()
        .any(|component| component.as_os_str() == ".octorus")
}

/// Run the app with a specific PR number (existing flow)
async fn run_with_pr(repo: &str, pr: u32, config: &config::Config, args: &Args) -> Result<()> {
    let (retry_tx, mut retry_rx) = mpsc::channel::<RefreshRequest>(1);
    let refresh_pending = Arc::new(AtomicBool::new(false));

    let (mut app, tx) = app::App::new_loading(repo, pr, config.clone());

    app.set_retry_sender(retry_tx);
    start_update_check(&mut app);
    setup_working_dir(&mut app, args);

    if args.ai_rally {
        app.set_start_ai_rally_on_load(true);
    }
    if args.git_ops {
        app.open_git_ops();
    }

    let cancel_token = CancellationToken::new();
    let token_clone = cancel_token.clone();

    let repo_clone = repo.to_string();
    let pr_number = pr;
    let working_dir = args.working_dir.clone();

    tokio::spawn(async move {
        tokio::select! {
            _ = token_clone.cancelled() => {}
            _ = async {
                loader::fetch_pr_data(repo_clone.clone(), pr_number, loader::FetchMode::Fresh, tx.clone()).await;

                while let Some(request) = retry_rx.recv().await {
                    match request {
                        RefreshRequest::PrRefresh { pr_number } => {
                            let tx_retry = tx.clone();
                            loader::fetch_pr_data(repo_clone.clone(), pr_number, loader::FetchMode::Fresh, tx_retry)
                                .await;
                        }
                        RefreshRequest::LocalRefresh => {
                            refresh_pending.store(false, Ordering::Release);
                            loop {
                                let tx_retry = tx.clone();
                                loader::fetch_local_diff(repo_clone.clone(), working_dir.clone(), tx_retry).await;
                                if !refresh_pending.swap(false, Ordering::AcqRel) {
                                    break;
                                }
                            }
                        }
                    }
                }
            } => {}
        }
    });

    let result = app.run().await;
    cancel_token.cancel();

    if let Err(ref e) = result {
        restore_terminal();
        eprintln!("Error: {:#}", e);
    }

    // Immediate exit to avoid hanging on spawn_blocking tasks (e.g. tree-sitter
    // parsing). Background tasks are already cancelled; the OS reclaims resources.
    let exit_code = if result.is_ok() { 0 } else { 1 };
    std::process::exit(exit_code);
}

/// Run the app with PR list (new flow)
async fn run_with_pr_list(
    repo: &str,
    config: config::Config,
    args: &Args,
    issue_arg: Option<u32>,
) -> Result<()> {
    // Retry channel also handles PR list → Local mode transitions.
    let (retry_tx, mut retry_rx) = mpsc::channel::<RefreshRequest>(1);
    let refresh_pending = Arc::new(AtomicBool::new(false));

    let mut app = app::App::new_pr_list(repo, config);
    app.set_retry_sender(retry_tx);
    start_update_check(&mut app);
    setup_working_dir(&mut app, args);

    if args.ai_rally {
        app.set_pending_ai_rally(true);
    }
    if args.git_ops {
        app.open_git_ops();
    }

    match issue_arg {
        Some(n) if n > 0 => {
            app.open_issue_list();
            app.select_issue(n);
        }
        Some(_) => {
            app.open_issue_list();
        }
        None => {}
    }

    let (pr_list_tx, rx) = mpsc::channel(2);
    app.set_pr_list_receiver(rx);

    let repo_clone = repo.to_string();
    let state_filter = app.prs.pr_list_state_filter;

    tokio::spawn(async move {
        let result = github::fetch_pr_list(&repo_clone, state_filter, 30).await;
        let _ = pr_list_tx.send(result.map_err(|e| e.to_string())).await;
    });

    // Data channel for local-mode transitions from the PR list screen.
    let (data_tx, data_rx) = mpsc::channel(2);
    app.set_data_receiver(0, data_rx);

    let cancel_token = CancellationToken::new();
    let token_clone = cancel_token.clone();
    let repo_for_retry = repo.to_string();
    let working_dir = args.working_dir.clone();

    tokio::spawn(async move {
        tokio::select! {
            _ = token_clone.cancelled() => {}
            _ = async {
                while let Some(request) = retry_rx.recv().await {
                    match request {
                        RefreshRequest::PrRefresh { pr_number } => {
                            let tx_retry = data_tx.clone();
                            loader::fetch_pr_data(repo_for_retry.clone(), pr_number, loader::FetchMode::Fresh, tx_retry)
                                .await;
                        }
                        RefreshRequest::LocalRefresh => {
                            refresh_pending.store(false, Ordering::Release);
                            loop {
                                let tx_retry = data_tx.clone();
                                loader::fetch_local_diff(repo_for_retry.clone(), working_dir.clone(), tx_retry).await;
                                if !refresh_pending.swap(false, Ordering::AcqRel) {
                                    break;
                                }
                            }
                        }
                    }
                }
            } => {}
        }
    });

    let result = app.run().await;
    cancel_token.cancel();

    if let Err(ref e) = result {
        restore_terminal();
        eprintln!("Error: {:#}", e);
    }

    // Same immediate-exit rationale as run_with_pr.
    let exit_code = if result.is_ok() { 0 } else { 1 };
    std::process::exit(exit_code);
}

/// Run the app with Cockpit dashboard (no-args startup)
async fn run_with_cockpit(
    repo: &str,
    config: config::Config,
    args: &Args,
    repo_available: bool,
) -> Result<()> {
    let (retry_tx, mut retry_rx) = mpsc::channel::<app::RefreshRequest>(1);
    let refresh_pending = Arc::new(AtomicBool::new(false));

    let mut app = app::App::new_cockpit(repo, config, repo_available);
    app.set_retry_sender(retry_tx);
    start_update_check(&mut app);
    setup_working_dir(&mut app, args);

    app.open_cockpit();

    let (data_tx, data_rx) = mpsc::channel(2);
    app.set_data_receiver(0, data_rx);

    let cancel_token = CancellationToken::new();
    let token_clone = cancel_token.clone();

    let repo_for_retry = repo.to_string();
    let working_dir = args.working_dir.clone();

    tokio::spawn(async move {
        tokio::select! {
            _ = token_clone.cancelled() => {}
            _ = async {
                while let Some(request) = retry_rx.recv().await {
                    match request {
                        app::RefreshRequest::PrRefresh { pr_number } => {
                            let tx_retry = data_tx.clone();
                            loader::fetch_pr_data(repo_for_retry.clone(), pr_number, loader::FetchMode::Fresh, tx_retry)
                                .await;
                        }
                        app::RefreshRequest::LocalRefresh => {
                            refresh_pending.store(false, Ordering::Release);
                            loop {
                                let tx_retry = data_tx.clone();
                                loader::fetch_local_diff(repo_for_retry.clone(), working_dir.clone(), tx_retry).await;
                                if !refresh_pending.swap(false, Ordering::AcqRel) {
                                    break;
                                }
                            }
                        }
                    }
                }
            } => {}
        }
    });

    let result = app.run().await;
    cancel_token.cancel();

    if let Err(ref e) = result {
        restore_terminal();
        eprintln!("Error: {:#}", e);
    }

    let exit_code = if result.is_ok() { 0 } else { 1 };
    std::process::exit(exit_code);
}

/// Spawn a background version check and set the receiver on the App.
fn start_update_check(app: &mut app::App) {
    let (tx, rx) = mpsc::channel(1);
    app.set_update_check_receiver(rx);

    tokio::task::spawn_blocking(move || {
        let result = update::check_for_update();
        let _ = tx.blocking_send(result);
    });
}

/// Resolve working directory for headless mode
fn resolve_working_dir(args: &Args) -> Option<String> {
    if let Some(dir) = args.working_dir.clone() {
        Some(dir)
    } else {
        std::env::current_dir()
            .ok()
            .map(|p| p.to_string_lossy().to_string())
    }
}

/// Set up working directory for AI agents
fn setup_working_dir(app: &mut app::App, args: &Args) {
    if let Some(dir) = args.working_dir.clone() {
        app.set_working_dir(Some(dir));
    } else {
        // current_dir() can fail in edge cases (e.g., if the current directory
        // has been deleted, or on some restricted environments). When --ai-rally is
        // used without --working-dir, we need a valid directory for the AI agents.
        match std::env::current_dir() {
            Ok(cwd) => {
                app.set_working_dir(Some(cwd.to_string_lossy().to_string()));
            }
            Err(e) => {
                if args.ai_rally {
                    eprintln!(
                        "Warning: Failed to get current directory: {}. AI Rally may not work correctly without --working-dir.",
                        e
                    );
                }
                // Continue without setting working_dir; it's optional for non-AI-Rally usage
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use notify::event::{AccessKind, AccessMode, CreateKind};
    use std::path::PathBuf;

    #[test]
    fn test_should_refresh_local_change_ignores_access_events() {
        let paths = vec![PathBuf::from("src/main.rs")];
        let kind = EventKind::Access(AccessKind::Close(AccessMode::Write));

        assert!(!should_refresh_local_change(&paths, &kind));
    }

    #[test]
    fn test_should_refresh_local_change_ignores_git_paths() {
        let paths = vec![PathBuf::from(".git/HEAD"), PathBuf::from(".git/index.lock")];
        let kind = EventKind::Create(CreateKind::File);

        assert!(!should_refresh_local_change(&paths, &kind));
    }

    #[test]
    fn test_should_refresh_local_change_refreshes_subdir_change() {
        let paths = vec![
            PathBuf::from(".git/HEAD"),
            PathBuf::from("src/subdir/changed.rs"),
        ];
        let kind = EventKind::Create(CreateKind::File);

        assert!(should_refresh_local_change(&paths, &kind));
    }

    #[test]
    fn test_is_git_file_identifies_git_path() {
        assert!(is_git_file(std::path::Path::new(".git/refs/heads/main")));
        assert!(!is_git_file(std::path::Path::new("src/main.rs")));
    }

    #[test]
    fn test_should_refresh_local_change_ignores_octorus_paths() {
        let paths = vec![
            PathBuf::from(".octorus/config.toml"),
            PathBuf::from(".octorus/prompts/reviewer.md"),
        ];
        let kind = EventKind::Create(CreateKind::File);

        assert!(!should_refresh_local_change(&paths, &kind));
    }

    #[test]
    fn test_is_octorus_config_file() {
        assert!(is_octorus_config_file(std::path::Path::new(
            ".octorus/config.toml"
        )));
        assert!(is_octorus_config_file(std::path::Path::new(
            ".octorus/prompts/reviewer.md"
        )));
        assert!(!is_octorus_config_file(std::path::Path::new("src/main.rs")));
    }
}