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
//! Single iteration execution logic.
//!
//! Handles spawning Claude, parsing response, and updating progress file.

use std::path::Path;
use std::time::Duration;

use tokio::sync::mpsc;

use crate::error::RslphError;
use crate::progress::ProgressFile;
use crate::prompts::get_build_prompt_for_mode;
use crate::subprocess::{
    format_tool_summary, ClaudeRunner, OutputLine, StreamEvent, StreamResponse,
};
use crate::tui::SubprocessEvent;

use super::state::{BuildContext, DoneReason, IterationResult};
use super::tokens::IterationTokens;

/// Format commit message for an iteration.
fn format_iteration_commit(project_name: &str, iteration: u32, tasks_completed: u32) -> String {
    format!(
        "[{}][iter {}] Completed {} task(s)",
        project_name, iteration, tasks_completed
    )
}

/// Parse a stream-json line and send appropriate events to TUI.
///
/// Returns the parsed event for response accumulation, or None if parsing failed.
fn parse_and_stream_line(
    line: &str,
    tui_tx: &mpsc::UnboundedSender<SubprocessEvent>,
) -> Option<StreamEvent> {
    let event = match StreamEvent::parse(line) {
        Ok(e) => e,
        Err(_) => return None,
    };

    // Send raw event for conversation view extraction
    let _ = tui_tx.send(SubprocessEvent::StreamEvent(event.clone()));

    // Send assistant text as ClaudeOutput
    if event.is_assistant() {
        if let Some(text) = event.extract_text() {
            if !text.is_empty() {
                let _ = tui_tx.send(SubprocessEvent::Output(text));
            }
        }

        // Send tool uses as ToolUse events with formatted summary
        for (tool_name, input) in event.extract_tool_uses() {
            let summary = format_tool_summary(&tool_name, &input);
            let _ = tui_tx.send(SubprocessEvent::ToolUse {
                tool_name,
                content: summary,
            });
        }

        // Send context usage if available
        if let Some(usage) = event.usage() {
            // Send token usage event for TUI display
            let _ = tui_tx.send(SubprocessEvent::TokenUsage {
                input_tokens: usage.input_tokens,
                output_tokens: usage.output_tokens,
                cache_creation_input_tokens: usage.cache_creation_input_tokens.unwrap_or(0),
                cache_read_input_tokens: usage.cache_read_input_tokens.unwrap_or(0),
            });

            // Estimate context usage as output_tokens / 200k (rough estimate)
            // A more accurate approach would track input+output vs max context
            let ratio = (usage.input_tokens + usage.output_tokens) as f64 / 200_000.0;
            let _ = tui_tx.send(SubprocessEvent::Usage(ratio.min(1.0)));
        }
    }

    Some(event)
}

/// Run a single iteration of the build loop.
///
/// This function:
/// 1. Re-reads the progress file (handles external edits)
/// 2. Checks for early exit conditions (RALPH_DONE, all tasks complete)
/// 3. Spawns a fresh Claude subprocess with the build prompt
/// 4. Parses the response and updates the progress file atomically
///
/// # Arguments
///
/// * `ctx` - Mutable build context with progress state
///
/// # Returns
///
/// * `Ok(IterationResult::Continue { tasks_completed })` - Iteration succeeded, continue
/// * `Ok(IterationResult::Done(reason))` - Build should stop
/// * `Err(RslphError)` - Iteration failed with error
pub async fn run_single_iteration(ctx: &mut BuildContext) -> Result<IterationResult, RslphError> {
    // Step 1: Re-read progress file (may have been updated externally)
    ctx.progress = ProgressFile::load(&ctx.progress_path)?;

    // Track tasks before iteration for diff
    let tasks_before = ctx.progress.completed_tasks();

    // Step 2: Check for early exit conditions
    if ctx.progress.is_done() {
        return Ok(IterationResult::Done(DoneReason::RalphDoneMarker));
    }

    if ctx.progress.completed_tasks() == ctx.progress.total_tasks()
        && ctx.progress.total_tasks() > 0
    {
        return Ok(IterationResult::Done(DoneReason::AllTasksComplete));
    }

    // Step 3: Build prompt with current progress context
    let system_prompt = get_build_prompt_for_mode(ctx.mode);

    // Clear completed this iteration from previous iteration
    ctx.progress.clear_iteration_completed();

    let user_input = format!(
        "## Current Progress\n\n{}\n\n## Instructions\n\nExecute the next incomplete task. Output the complete updated progress file.",
        ctx.progress.to_markdown()
    );

    // 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(),
        "stream-json".to_string(), // JSONL for structured parsing
        "--system-prompt".to_string(),
        system_prompt,
        user_input,
    ];

    // Step 5: Spawn fresh Claude subprocess
    // Handle both None parent and empty parent (when path is just filename)
    let working_dir = ctx
        .progress_path
        .parent()
        .filter(|p| !p.as_os_str().is_empty())
        .unwrap_or(Path::new("."));

    ctx.log(&format!(
        "[TRACE] Iteration {}: Spawning Claude subprocess",
        ctx.current_iteration
    ));

    let runner_result = ClaudeRunner::spawn(&ctx.config.claude_path, &args, working_dir).await;

    let mut runner = match runner_result {
        Ok(r) => r,
        Err(e) => {
            // Log attempt on spawn failure
            ctx.progress.add_attempt(
                ctx.current_iteration,
                "Spawn Claude subprocess",
                &format!("Error: {}", e),
                Some("Check claude_path configuration"),
            );
            ctx.progress
                .trim_attempts(ctx.config.recent_threads as usize);
            ctx.progress.write(&ctx.progress_path)?;
            let path_env = std::env::var("PATH").unwrap_or_else(|_| "(not set)".to_string());
            return Err(RslphError::Subprocess(format!(
                "Failed to spawn '{}': {}. Ensure claude is in PATH or set claude_path to absolute path in config. PATH: {}",
                ctx.config.claude_path, e, path_env
            )));
        }
    };

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

    // Step 6: Run subprocess and collect output
    // When TUI is active, stream output to TUI while collecting for response parsing
    let timeout = Duration::from_secs(ctx.config.iteration_timeout);
    let mut stream_response = StreamResponse::new();

    let run_result = if let Some(ref tui_tx) = ctx.tui_tx {
        // Streaming mode: use run_with_channel and parse+stream each line
        let (line_tx, mut line_rx) = mpsc::unbounded_channel::<OutputLine>();

        // Spawn the runner with channel
        let cancel_token = ctx.cancel_token.clone();
        let runner_handle =
            tokio::spawn(async move { runner.run_with_channel(line_tx, cancel_token).await });

        // Process lines as they arrive, with timeout
        let tui_tx_clone = tui_tx.clone();
        let process_result = tokio::time::timeout(timeout, async {
            while let Some(line) = line_rx.recv().await {
                if let OutputLine::Stdout(s) = &line {
                    // Stream to TUI
                    if let Some(event) = parse_and_stream_line(s, &tui_tx_clone) {
                        stream_response.process_event(&event);
                    }
                }
            }
            Ok::<(), RslphError>(())
        })
        .await;

        // Wait for runner to complete
        let runner_result = runner_handle
            .await
            .map_err(|e| RslphError::Subprocess(format!("Runner task failed: {}", e)))?;

        // Check for timeout or runner error
        match process_result {
            Ok(Ok(())) => runner_result,
            Ok(Err(e)) => Err(e),
            Err(_) => Err(RslphError::Timeout(timeout.as_secs())),
        }
    } else {
        // Non-streaming mode: collect all output then parse
        // Don't use ? here - we want to catch timeout errors and handle them below
        match runner
            .run_with_timeout(timeout, ctx.cancel_token.clone())
            .await
        {
            Ok(output) => {
                // Parse JSONL response
                for line in &output {
                    if let OutputLine::Stdout(s) = line {
                        stream_response.process_line(s);
                    }
                }
                Ok(())
            }
            Err(e) => Err(e),
        }
    };

    // Handle run errors
    if let Err(e) = run_result {
        // Check if this is a timeout error - return Timeout result for retry
        if matches!(e, RslphError::Timeout(_)) {
            ctx.progress.add_attempt(
                ctx.current_iteration,
                "Execute Claude subprocess",
                &format!("Timeout after {}s", ctx.config.iteration_timeout),
                Some("Retrying iteration"),
            );
            ctx.progress
                .trim_attempts(ctx.config.recent_threads as usize);
            ctx.progress.write(&ctx.progress_path)?;
            return Ok(IterationResult::Timeout);
        }

        ctx.progress.add_attempt(
            ctx.current_iteration,
            "Execute Claude subprocess",
            &format!("Error: {}", e),
            Some("Retry or check subprocess"),
        );
        ctx.progress
            .trim_attempts(ctx.config.recent_threads as usize);
        ctx.progress.write(&ctx.progress_path)?;
        return Err(e);
    }

    // Step 7: Extract response text
    let response_text = stream_response.text;

    ctx.log(&format!(
        "[TRACE] Claude output length: {} chars",
        response_text.len()
    ));
    if let Some(model) = &stream_response.model {
        ctx.log(&format!("[TRACE] Model: {}", model));
    }
    ctx.log(&format!(
        "[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
    ));

    // Accumulate tokens from this iteration
    let iteration_tokens = IterationTokens {
        iteration: ctx.current_iteration,
        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,
    };
    ctx.iteration_tokens.push(iteration_tokens);
    ctx.total_tokens.input_tokens += stream_response.input_tokens;
    ctx.total_tokens.output_tokens += stream_response.output_tokens;
    ctx.total_tokens.cache_creation_input_tokens += stream_response.cache_creation_input_tokens;
    ctx.total_tokens.cache_read_input_tokens += stream_response.cache_read_input_tokens;

    // Step 8: Parse response into ProgressFile
    let updated_progress = match ProgressFile::parse(&response_text) {
        Ok(p) => p,
        Err(e) => {
            // Log attempt on parse failure
            ctx.progress.add_attempt(
                ctx.current_iteration,
                "Parse Claude response",
                &format!("Error: {}", e),
                Some("Check response format"),
            );
            ctx.progress
                .trim_attempts(ctx.config.recent_threads as usize);
            ctx.progress.write(&ctx.progress_path)?;
            return Err(e);
        }
    };

    // Step 9: Write updated progress file atomically with trimmed attempts
    let mut updated_progress = updated_progress;
    updated_progress.trim_attempts(ctx.config.recent_threads as usize);
    updated_progress.write(&ctx.progress_path)?;

    ctx.log(&format!(
        "[TRACE] Updated progress file: {}",
        ctx.progress_path.display()
    ));

    // Step 10: Calculate tasks completed this iteration
    let tasks_after = updated_progress.completed_tasks();
    let tasks_completed = tasks_after.saturating_sub(tasks_before) as u32;

    // Step 11: VCS auto-commit if tasks were completed
    if tasks_completed > 0 {
        if let Some(ref vcs) = ctx.vcs {
            let commit_msg =
                format_iteration_commit(&ctx.project_name, ctx.current_iteration, tasks_completed);
            match vcs.commit_all(&commit_msg) {
                Ok(Some(hash)) => {
                    ctx.log(&format!("[VCS] Committed: {} ({})", hash, vcs.vcs_type()));
                }
                Ok(None) => {
                    ctx.log("[VCS] No file changes to commit");
                }
                Err(e) => {
                    // VCS errors are warnings, not failures
                    ctx.log(&format!("[VCS] Warning: {}", e));
                }
            }
        }
    }

    // Update context with new progress
    ctx.progress = updated_progress;

    // Check if done after update
    if ctx.progress.is_done() {
        return Ok(IterationResult::Done(DoneReason::RalphDoneMarker));
    }

    if ctx.progress.completed_tasks() == ctx.progress.total_tasks()
        && ctx.progress.total_tasks() > 0
    {
        return Ok(IterationResult::Done(DoneReason::AllTasksComplete));
    }

    Ok(IterationResult::Continue { tasks_completed })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::prompts::PromptMode;
    use tempfile::TempDir;
    use tokio_util::sync::CancellationToken;

    fn create_test_progress() -> ProgressFile {
        use crate::progress::{Task, TaskPhase};

        ProgressFile {
            name: "Test Plan".to_string(),
            status: "In Progress".to_string(),
            analysis: "Test analysis".to_string(),
            tasks: vec![TaskPhase {
                name: "Phase 1".to_string(),
                tasks: vec![
                    Task {
                        description: "Task 1".to_string(),
                        completed: false,
                    },
                    Task {
                        description: "Task 2".to_string(),
                        completed: false,
                    },
                ],
            }],
            testing_strategy: "Unit tests".to_string(),
            completed_this_iteration: vec![],
            recent_attempts: vec![],
            iteration_log: vec![],
        }
    }

    #[tokio::test]
    async fn test_iteration_detects_ralph_done() {
        let dir = TempDir::new().expect("temp dir");
        let progress_path = dir.path().join("progress.md");

        let mut progress = create_test_progress();
        progress.status = "RALPH_DONE - All complete".to_string();
        progress.write(&progress_path).expect("write");

        let config = crate::config::Config::default();
        let token = CancellationToken::new();

        let mut ctx = BuildContext::new(
            progress_path,
            progress,
            config,
            PromptMode::Basic,
            token,
            false,
            false,
        );
        ctx.current_iteration = 1;

        let result = run_single_iteration(&mut ctx).await;
        assert!(result.is_ok());
        assert!(matches!(
            result.unwrap(),
            IterationResult::Done(DoneReason::RalphDoneMarker)
        ));
    }

    #[tokio::test]
    async fn test_iteration_detects_all_tasks_complete() {
        let dir = TempDir::new().expect("temp dir");
        let progress_path = dir.path().join("progress.md");

        use crate::progress::{Task, TaskPhase};
        let progress = ProgressFile {
            name: "Test".to_string(),
            status: "In Progress".to_string(),
            tasks: vec![TaskPhase {
                name: "Phase 1".to_string(),
                tasks: vec![Task {
                    description: "Task 1".to_string(),
                    completed: true,
                }],
            }],
            ..Default::default()
        };
        progress.write(&progress_path).expect("write");

        let config = crate::config::Config::default();
        let token = CancellationToken::new();

        let mut ctx = BuildContext::new(
            progress_path,
            progress,
            config,
            PromptMode::Basic,
            token,
            false,
            false,
        );
        ctx.current_iteration = 1;

        let result = run_single_iteration(&mut ctx).await;
        assert!(result.is_ok());
        assert!(matches!(
            result.unwrap(),
            IterationResult::Done(DoneReason::AllTasksComplete)
        ));
    }
}