rslph 0.1.1

CLI tool for LLM-powered autonomous task execution
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
//! Planning command handler.
//!
//! Executes Claude in headless mode to transform user ideas into structured progress files.

use std::io::{self, BufRead};
use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

use crate::build::tokens::{format_tokens, TokenUsage};
use crate::config::Config;
use crate::error::RslphError;
use crate::planning::{
    assess_vagueness, detect_stack, REQUIREMENTS_CLARIFIER_PERSONA, TESTING_STRATEGIST_PERSONA,
};
use crate::progress::ProgressFile;
use crate::prompts::{get_plan_prompt_for_mode, PromptMode};
use crate::subprocess::{ClaudeRunner, OutputLine, StreamEvent, StreamResponse};
use crate::tui::plan_tui::run_plan_tui;

/// Run the planning command.
///
/// Executes Claude CLI in headless mode with the planning system prompt,
/// parses the output into a ProgressFile, and writes it to disk.
///
/// # Arguments
///
/// * `input` - User's idea/plan description
/// * `adaptive` - Whether to use adaptive mode with clarifying questions
/// * `tui` - Whether to use TUI mode with streaming output
/// * `mode` - The prompt mode to use (Basic, Gsd, GsdTdd)
/// * `config` - Application configuration
/// * `working_dir` - Directory to use as working directory and output location
/// * `cancel_token` - Token for graceful cancellation
/// * `timeout` - Maximum duration to wait for Claude
///
/// # Returns
///
/// Tuple of (path to generated progress file, token usage).
#[allow(clippy::too_many_arguments)]
pub async fn run_plan_command(
    input: &str,
    adaptive: bool,
    tui: bool,
    mode: PromptMode,
    config: &Config,
    working_dir: &Path,
    cancel_token: CancellationToken,
    timeout: Duration,
) -> color_eyre::Result<(PathBuf, TokenUsage)> {
    // If TUI mode, run the TUI planning flow
    if tui {
        return run_tui_planning(input, mode, config, working_dir, cancel_token, timeout).await;
    }

    // If adaptive mode, run the adaptive planning flow
    if adaptive {
        return run_adaptive_planning(input, mode, config, working_dir, cancel_token, timeout)
            .await;
    }

    // Basic mode: direct planning without clarification
    run_basic_planning(input, mode, config, working_dir, cancel_token, timeout).await
}

/// Run basic (non-adaptive) planning mode.
async fn run_basic_planning(
    input: &str,
    mode: PromptMode,
    config: &Config,
    working_dir: &Path,
    cancel_token: CancellationToken,
    timeout: Duration,
) -> color_eyre::Result<(PathBuf, TokenUsage)> {
    // Step 1: Detect project stack for testing strategy
    let stack = detect_stack(working_dir);

    // Step 2: Get the planning prompt for the specified mode
    let system_prompt = get_plan_prompt_for_mode(mode);

    // Step 3: Build user input with stack context
    let full_input = format!(
        "## Detected Stack\n{}\n\n## User Request\n{}",
        stack.to_summary(),
        input
    );

    // Step 4: Build Claude CLI args for headless mode
    let args = vec![
        "-p".to_string(),         // Print mode (headless)
        "--verbose".to_string(),  // Required for stream-json with -p
        "--output-format".to_string(), // Output format
        "stream-json".to_string(), // JSONL for structured parsing
        "--system-prompt".to_string(), // Custom system prompt
        system_prompt,
        full_input, // User input as positional arg
    ];

    eprintln!(
        "[TRACE] Spawning: {} {:?}",
        config.claude_path,
        args.iter().take(4).collect::<Vec<_>>()
    );

    // Step 5: Spawn Claude
    let mut runner = ClaudeRunner::spawn(&config.claude_path, &args, working_dir)
        .await
        .map_err(|e| RslphError::Subprocess(format!("Failed to spawn claude: {}", e)))?;

    eprintln!("[TRACE] Spawned subprocess with PID: {:?}", runner.id());

    // Step 6: Run with timeout and collect output, tracing each line
    let output = run_with_tracing(&mut runner, timeout, cancel_token.clone()).await?;

    // Step 7: Parse JSONL output using StreamResponse
    let mut stream_response = StreamResponse::new();
    for line in &output {
        if let OutputLine::Stdout(s) = line {
            stream_response.process_line(s);
        }
    }
    let response_text = stream_response.text;

    eprintln!(
        "[TRACE] Claude output length: {} chars",
        response_text.len()
    );
    if let Some(model) = &stream_response.model {
        eprintln!("[TRACE] Model: {}", model);
    }
    eprintln!(
        "[TRACE] Tokens: {} in / {} out / {} cache_write / {} cache_read",
        stream_response.input_tokens,
        stream_response.output_tokens,
        stream_response.cache_creation_input_tokens,
        stream_response.cache_read_input_tokens
    );

    // Display token summary for user
    println!(
        "Tokens used: In: {} | Out: {} | CacheW: {} | CacheR: {}",
        format_tokens(stream_response.input_tokens),
        format_tokens(stream_response.output_tokens),
        format_tokens(stream_response.cache_creation_input_tokens),
        format_tokens(stream_response.cache_read_input_tokens),
    );

    // Step 8: Parse response into ProgressFile
    let mut progress_file = ProgressFile::parse(&response_text)?;

    // Step 8.5: Generate project name if empty
    if progress_file.name.is_empty() {
        eprintln!("[TRACE] Progress file has no name, generating one...");
        let generated_name = generate_project_name(
            &config.claude_path,
            input,
            working_dir,
            cancel_token.clone(),
            timeout,
        )
        .await?;
        eprintln!("[TRACE] Generated project name: {}", generated_name);
        progress_file.name = generated_name;
    }

    // Step 9: Write to file
    let output_path = working_dir.join("progress.md");
    progress_file.write(&output_path)?;

    eprintln!("[TRACE] Wrote progress file to: {}", output_path.display());

    // Step 10: Create TokenUsage from stream_response
    let tokens = TokenUsage {
        input_tokens: stream_response.input_tokens,
        output_tokens: stream_response.output_tokens,
        cache_creation_input_tokens: stream_response.cache_creation_input_tokens,
        cache_read_input_tokens: stream_response.cache_read_input_tokens,
    };

    Ok((output_path, tokens))
}

/// Run TUI planning mode with streaming output display.
///
/// TUI mode:
/// 1. Detects project stack
/// 2. Spawns Claude with stream-json output
/// 3. Streams events to TUI for real-time display
/// 4. Parses output and writes progress file
async fn run_tui_planning(
    input: &str,
    mode: PromptMode,
    config: &Config,
    working_dir: &Path,
    cancel_token: CancellationToken,
    timeout: Duration,
) -> color_eyre::Result<(PathBuf, TokenUsage)> {
    use tokio::time::timeout as tokio_timeout;

    // Step 1: Detect project stack for testing strategy
    let stack = detect_stack(working_dir);

    // Step 2: Get the planning prompt for the specified mode
    let system_prompt = get_plan_prompt_for_mode(mode);

    // Step 3: Build user input with stack context
    let full_input = format!(
        "## Detected Stack\n{}\n\n## User Request\n{}",
        stack.to_summary(),
        input
    );

    // Step 4: Build Claude CLI args for streaming mode
    let args = vec![
        "-p".to_string(),
        "--verbose".to_string(),
        "--output-format".to_string(),
        "stream-json".to_string(),
        "--system-prompt".to_string(),
        system_prompt,
        full_input,
    ];

    // Step 5: Spawn Claude
    let mut runner = ClaudeRunner::spawn(&config.claude_path, &args, working_dir)
        .await
        .map_err(|e| RslphError::Subprocess(format!("Failed to spawn claude: {}", e)))?;

    // Step 6: Create channel for stream events
    let (event_tx, event_rx) = mpsc::unbounded_channel();

    // Step 7: Spawn TUI task
    let tui_cancel = cancel_token.clone();
    let tui_handle = tokio::spawn(async move { run_plan_tui(event_rx, tui_cancel).await });

    // Step 8: Stream events to TUI with timeout
    let mut stream_response = StreamResponse::new();
    let stream_cancel = cancel_token.clone();

    let stream_result = tokio_timeout(timeout, async {
        loop {
            tokio::select! {
                biased;

                _ = stream_cancel.cancelled() => {
                    runner.terminate_gracefully(Duration::from_secs(5)).await
                        .map_err(|e| RslphError::Subprocess(e.to_string()))?;
                    return Err::<(), RslphError>(RslphError::Cancelled);
                }

                line = runner.next_output() => {
                    match line {
                        Some(OutputLine::Stdout(s)) => {
                            // Parse and forward to TUI
                            if let Ok(event) = StreamEvent::parse(&s) {
                                stream_response.process_event(&event);
                                let _ = event_tx.send(event);
                            }
                        }
                        Some(OutputLine::Stderr(_)) => {
                            // Ignore stderr in TUI mode
                        }
                        None => {
                            // Stream complete
                            break;
                        }
                    }
                }
            }
        }
        Ok(())
    })
    .await;

    // Step 9: Drop sender to signal completion to TUI
    drop(event_tx);

    // Step 10: Wait for TUI to finish
    let tui_state = tui_handle
        .await
        .map_err(|e| RslphError::Subprocess(format!("TUI task failed: {}", e)))?
        .map_err(|e| RslphError::Subprocess(format!("TUI error: {}", e)))?;

    // Check for timeout or cancellation
    match stream_result {
        Err(_) => return Err(RslphError::Timeout(timeout.as_secs()).into()),
        Ok(Err(e)) => return Err(e.into()),
        Ok(Ok(())) => {}
    }

    // Check if user quit
    if tui_state.should_quit {
        return Err(RslphError::Cancelled.into());
    }

    // Step 11: Parse response into ProgressFile
    let mut progress_file = ProgressFile::parse(&stream_response.text)?;

    // Step 12: Generate project name if empty (non-TUI for simplicity)
    if progress_file.name.is_empty() {
        let generated_name = generate_project_name(
            &config.claude_path,
            input,
            working_dir,
            cancel_token.clone(),
            timeout,
        )
        .await?;
        progress_file.name = generated_name;
    }

    // Step 13: Write to file
    let output_path = working_dir.join("progress.md");
    progress_file.write(&output_path)?;

    // Step 14: Create TokenUsage from stream_response
    let tokens = TokenUsage {
        input_tokens: stream_response.input_tokens,
        output_tokens: stream_response.output_tokens,
        cache_creation_input_tokens: stream_response.cache_creation_input_tokens,
        cache_read_input_tokens: stream_response.cache_read_input_tokens,
    };

    Ok((output_path, tokens))
}

/// Run subprocess with tracing to stderr for each line of output.
async fn run_with_tracing(
    runner: &mut ClaudeRunner,
    max_duration: Duration,
    cancel_token: CancellationToken,
) -> Result<Vec<OutputLine>, RslphError> {
    use tokio::time::timeout;

    let collect_with_trace = async {
        let mut output = Vec::new();
        loop {
            tokio::select! {
                biased;

                _ = cancel_token.cancelled() => {
                    eprintln!("[TRACE] Cancellation requested");
                    runner.terminate_gracefully(Duration::from_secs(5)).await
                        .map_err(|e| RslphError::Subprocess(e.to_string()))?;
                    return Err(RslphError::Cancelled);
                }

                line = runner.next_output() => {
                    match line {
                        Some(OutputLine::Stdout(s)) => {
                            eprintln!("[STDOUT] {}", s);
                            output.push(OutputLine::Stdout(s));
                        }
                        Some(OutputLine::Stderr(s)) => {
                            eprintln!("[STDERR] {}", s);
                            output.push(OutputLine::Stderr(s));
                        }
                        None => {
                            eprintln!("[TRACE] Subprocess streams closed");
                            break;
                        }
                    }
                }
            }
        }
        Ok(output)
    };

    match timeout(max_duration, collect_with_trace).await {
        Ok(result) => result,
        Err(_elapsed) => {
            eprintln!("[TRACE] Timeout after {:?}", max_duration);
            runner
                .terminate_gracefully(Duration::from_secs(5))
                .await
                .map_err(|e| RslphError::Subprocess(e.to_string()))?;
            Err(RslphError::Timeout(max_duration.as_secs()))
        }
    }
}

/// Run adaptive planning mode with clarifying questions.
///
/// Adaptive mode:
/// 1. Detects project stack
/// 2. Assesses vagueness of input
/// 3. If vague, asks clarifying questions via requirements clarifier persona
/// 4. Generates testing strategy via testing strategist persona
/// 5. Runs final planning with all gathered context
pub async fn run_adaptive_planning(
    input: &str,
    mode: PromptMode,
    config: &Config,
    working_dir: &Path,
    cancel_token: CancellationToken,
    timeout: Duration,
) -> color_eyre::Result<(PathBuf, TokenUsage)> {
    // Step 1: Detect project stack
    let stack = detect_stack(working_dir);
    println!("Detected stack:\n{}", stack.to_summary());

    // Step 2: Assess vagueness
    let vagueness = assess_vagueness(input);
    println!("\nVagueness score: {:.2} (threshold: 0.5)", vagueness.score);
    if !vagueness.reasons.is_empty() {
        println!("Reasons: {}", vagueness.reasons.join(", "));
    }

    // Step 3: Initialize clarifications
    let mut clarifications = String::new();

    // Step 4: If vague, run requirements clarifier
    if vagueness.is_vague() {
        println!("\nInput appears vague, gathering requirements...\n");

        let clarifier_input = format!(
            "## Project Stack\n{}\n\n## User Idea\n{}",
            stack.to_summary(),
            input
        );

        let questions = run_claude_headless(
            &config.claude_path,
            REQUIREMENTS_CLARIFIER_PERSONA,
            &clarifier_input,
            working_dir,
            cancel_token.clone(),
            timeout,
        )
        .await?;

        if !questions.contains("REQUIREMENTS_CLEAR") {
            // Print questions and get user input
            println!("Clarifying Questions:\n");
            println!("{}", questions);
            println!("\nPlease answer the questions above (type your answers, then Enter twice to submit):\n");

            // Read multi-line input from stdin
            clarifications = read_multiline_input()?;
            println!("\nGathered clarifications. Continuing...\n");
        } else {
            println!("Requirements are clear enough, skipping clarification.\n");
        }
    } else {
        println!("\nInput is specific enough, skipping clarification.\n");
    }

    // Step 5: Run testing strategist
    println!("Generating testing strategy...\n");

    let testing_input = format!(
        "## Project Stack\n{}\n\n## Requirements\n{}\n\n## Clarifications\n{}",
        stack.to_summary(),
        input,
        if clarifications.is_empty() {
            "None"
        } else {
            &clarifications
        }
    );

    let testing_strategy = run_claude_headless(
        &config.claude_path,
        TESTING_STRATEGIST_PERSONA,
        &testing_input,
        working_dir,
        cancel_token.clone(),
        timeout,
    )
    .await?;

    println!("Testing strategy generated.\n");

    // Step 6: Run final planning with all context
    println!("Generating final plan...\n");

    let plan_prompt = get_plan_prompt_for_mode(mode);
    let final_input = format!(
        "## Detected Stack\n{}\n\n## Requirements\n{}\n\n## Clarifications\n{}\n\n## Testing Strategy\n{}",
        stack.to_summary(),
        input,
        if clarifications.is_empty() { "None" } else { &clarifications },
        testing_strategy
    );

    // Build Claude CLI args for headless mode
    let args = vec![
        "-p".to_string(),
        "--verbose".to_string(), // Required for stream-json with -p
        "--output-format".to_string(),
        "stream-json".to_string(), // JSONL for structured parsing
        "--system-prompt".to_string(),
        plan_prompt,
        final_input,
    ];

    // Spawn Claude
    let mut runner = ClaudeRunner::spawn(&config.claude_path, &args, working_dir)
        .await
        .map_err(|e| RslphError::Subprocess(format!("Failed to spawn claude: {}", e)))?;

    // Run with timeout and collect output
    let output = runner
        .run_with_timeout(timeout, cancel_token.clone())
        .await?;

    // Parse JSONL output using StreamResponse
    let mut stream_response = StreamResponse::new();
    for line in &output {
        if let OutputLine::Stdout(s) = line {
            stream_response.process_line(s);
        }
    }
    let response_text = stream_response.text;

    // Display token summary for user
    println!(
        "Tokens used: In: {} | Out: {} | CacheW: {} | CacheR: {}",
        format_tokens(stream_response.input_tokens),
        format_tokens(stream_response.output_tokens),
        format_tokens(stream_response.cache_creation_input_tokens),
        format_tokens(stream_response.cache_read_input_tokens),
    );

    // Parse response into ProgressFile
    let mut progress_file = ProgressFile::parse(&response_text)?;

    // Generate project name if empty
    if progress_file.name.is_empty() {
        eprintln!("[TRACE] Progress file has no name, generating one...");
        let generated_name = generate_project_name(
            &config.claude_path,
            input,
            working_dir,
            cancel_token.clone(),
            timeout,
        )
        .await?;
        eprintln!("[TRACE] Generated project name: {}", generated_name);
        progress_file.name = generated_name;
    }

    // Write to file
    let output_path = working_dir.join("progress.md");
    progress_file.write(&output_path)?;

    // Create TokenUsage from stream_response
    let tokens = TokenUsage {
        input_tokens: stream_response.input_tokens,
        output_tokens: stream_response.output_tokens,
        cache_creation_input_tokens: stream_response.cache_creation_input_tokens,
        cache_read_input_tokens: stream_response.cache_read_input_tokens,
    };

    Ok((output_path, tokens))
}

/// Run Claude CLI in headless mode with a system prompt and return the response.
async fn run_claude_headless(
    claude_path: &str,
    system_prompt: &str,
    user_input: &str,
    working_dir: &Path,
    cancel_token: CancellationToken,
    timeout: Duration,
) -> color_eyre::Result<String> {
    let args = vec![
        "-p".to_string(),
        "--verbose".to_string(), // Required for stream-json with -p
        "--output-format".to_string(),
        "stream-json".to_string(), // JSONL for structured parsing
        "--system-prompt".to_string(),
        system_prompt.to_string(),
        user_input.to_string(),
    ];

    let mut runner = ClaudeRunner::spawn(claude_path, &args, working_dir)
        .await
        .map_err(|e| RslphError::Subprocess(format!("Failed to spawn claude: {}", e)))?;

    let output = runner.run_with_timeout(timeout, cancel_token).await?;

    // Parse JSONL output using StreamResponse
    let mut stream_response = StreamResponse::new();
    for line in &output {
        if let OutputLine::Stdout(s) = line {
            stream_response.process_line(s);
        }
    }

    Ok(stream_response.text)
}

/// Generate a short kebab-case project name from the user's input.
///
/// Asks Claude to summarize the project in 2 words max, formatted as kebab-case.
async fn generate_project_name(
    claude_path: &str,
    user_input: &str,
    working_dir: &Path,
    cancel_token: CancellationToken,
    timeout: Duration,
) -> color_eyre::Result<String> {
    const NAME_GENERATOR_PROMPT: &str = r#"Generate a short project name for the following idea.

Rules:
- Exactly 2 words maximum
- Use kebab-case (lowercase with hyphens, e.g., "quadratic-solver", "todo-app", "file-sync")
- Be descriptive but concise
- Output ONLY the project name, nothing else

Example outputs:
- task-manager
- code-formatter
- weather-api
- chat-bot"#;

    let response = run_claude_headless(
        claude_path,
        NAME_GENERATOR_PROMPT,
        user_input,
        working_dir,
        cancel_token,
        timeout,
    )
    .await?;

    // Clean up the response - take first line, trim, convert to kebab-case
    let name = response
        .lines()
        .next()
        .unwrap_or("unnamed-project")
        .trim()
        .to_lowercase()
        .replace(' ', "-")
        .chars()
        .filter(|c| c.is_alphanumeric() || *c == '-')
        .collect::<String>();

    // Ensure it's not empty
    if name.is_empty() {
        Ok("unnamed-project".to_string())
    } else {
        Ok(name)
    }
}

/// Read multi-line input from stdin.
///
/// Reading continues until two consecutive empty lines are entered.
fn read_multiline_input() -> color_eyre::Result<String> {
    let stdin = io::stdin();
    let mut lines = Vec::new();
    let mut empty_count = 0;

    for line in stdin.lock().lines() {
        let line = line?;
        if line.is_empty() {
            empty_count += 1;
            if empty_count >= 2 {
                break;
            }
            lines.push(line);
        } else {
            empty_count = 0;
            lines.push(line);
        }
    }

    // Remove trailing empty lines
    while lines.last().is_some_and(|l| l.is_empty()) {
        lines.pop();
    }

    Ok(lines.join("\n"))
}

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

    #[tokio::test]
    async fn test_run_plan_command_spawns_and_writes_file() {
        // This test verifies the full command flow using echo as a mock.
        // Echo outputs garbage but ProgressFile::parse is lenient so it succeeds.
        let dir = TempDir::new().expect("temp dir");

        // Create a config pointing to echo instead of claude
        let config = Config {
            claude_path: "/bin/echo".to_string(),
            ..Default::default()
        };

        let token = CancellationToken::new();

        let result = run_plan_command(
            "build something",
            false, // basic mode
            false, // no TUI
            PromptMode::Basic,
            &config,
            dir.path(),
            token,
            Duration::from_secs(5),
        )
        .await;

        // Echo outputs something, parse is lenient, so this actually succeeds
        assert!(result.is_ok(), "Command should complete: {:?}", result);

        // Verify progress.md was created
        let (output_path, tokens) = result.unwrap();
        assert!(output_path.exists(), "Progress file should exist");
        assert!(output_path.ends_with("progress.md"));
        // Verify tokens are returned (will be zero from echo mock)
        assert_eq!(tokens.input_tokens, 0);
        assert_eq!(tokens.output_tokens, 0);
    }

    #[tokio::test]
    async fn test_run_plan_command_timeout() {
        let dir = TempDir::new().expect("temp dir");

        // Use a script that ignores arguments and sleeps
        let script = "#!/bin/sh\nsleep 60\n";
        let script_path = dir.path().join("slow_script.sh");
        std::fs::write(&script_path, script).expect("write script");

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&script_path, std::fs::Permissions::from_mode(0o755))
                .expect("set permissions");
        }

        let config = Config {
            claude_path: script_path.to_string_lossy().to_string(),
            ..Default::default()
        };

        let token = CancellationToken::new();
        let result = run_plan_command(
            "anything",
            false, // basic mode
            false, // no TUI
            PromptMode::Basic,
            &config,
            dir.path(),
            token,
            Duration::from_millis(100),
        )
        .await;

        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("timeout"), "Should timeout: {}", err);
    }

    #[tokio::test]
    async fn test_run_plan_command_cancellation() {
        let dir = TempDir::new().expect("temp dir");

        // Use a script that ignores arguments and sleeps
        let script = "#!/bin/sh\nsleep 60\n";
        let script_path = dir.path().join("slow_script.sh");
        std::fs::write(&script_path, script).expect("write script");

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&script_path, std::fs::Permissions::from_mode(0o755))
                .expect("set permissions");
        }

        let config = Config {
            claude_path: script_path.to_string_lossy().to_string(),
            ..Default::default()
        };

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

        // Cancel after 50ms
        tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(50)).await;
            token_clone.cancel();
        });

        let result = run_plan_command(
            "anything",
            false, // basic mode
            false, // no TUI
            PromptMode::Basic,
            &config,
            dir.path(),
            token,
            Duration::from_secs(10),
        )
        .await;

        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("cancelled"), "Should be cancelled: {}", err);
    }

    #[tokio::test]
    async fn test_run_plan_command_nonexistent_command() {
        let dir = TempDir::new().expect("temp dir");

        let config = Config {
            claude_path: "/nonexistent/command".to_string(),
            ..Default::default()
        };

        let token = CancellationToken::new();
        let result = run_plan_command(
            "anything",
            false, // basic mode
            false, // no TUI
            PromptMode::Basic,
            &config,
            dir.path(),
            token,
            Duration::from_secs(5),
        )
        .await;

        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("spawn") || err.contains("Subprocess"),
            "Should fail to spawn: {}",
            err
        );
    }
}