ralph-workflow 0.7.18

PROMPT-driven multi-agent orchestrator for git repos
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
//! Rebase conflict resolution prompts.
//!
//! This module provides prompts for AI agents to resolve merge conflicts
//! that occur during rebase operations.
//!
//! # Design Note
//!
//! Per project requirements, AI agents should NOT know that we are in the
//! middle of a rebase. The prompt frames conflicts as "merge conflicts between
//! two versions" without mentioning rebase or rebasing.

#![deny(unsafe_code)]

use crate::prompts::template_context::TemplateContext;
use crate::prompts::template_engine::Template;
use crate::workspace::Workspace;
use std::collections::HashMap;
use std::path::Path;

/// Structure representing a single file conflict.
#[derive(Debug, Clone)]
pub struct FileConflict {
    /// The conflict marker content from the file
    pub conflict_content: String,
    /// The current file content with conflict markers
    pub current_content: String,
}

/// Build a conflict resolution prompt for the AI agent.
///
/// This function generates a prompt that instructs the AI agent to resolve
/// merge conflicts. The prompt does NOT mention "rebase" - it frames the
/// task as resolving merge conflicts between two versions.
///
/// # Arguments
///
/// * `conflicts` - Map of file paths to their conflict information
/// * `prompt_md_content` - Optional content from PROMPT.md for task context
/// * `plan_content` - Optional content from PLAN.md for additional context
///
/// # Returns
///
/// Returns a formatted prompt string for the AI agent.
#[cfg(test)]
#[expect(clippy::print_stderr, reason = "test-only error logging")]
pub fn build_conflict_resolution_prompt(
    conflicts: &HashMap<String, FileConflict>,
    prompt_md_content: Option<&str>,
    plan_content: Option<&str>,
) -> String {
    let template_content = include_str!("templates/conflict_resolution.txt");
    let template = Template::new(template_content);

    let context = format_context_section(prompt_md_content, plan_content);
    let conflicts_section = format_conflicts_section(conflicts);

    let variables = HashMap::from([
        ("CONTEXT", context),
        ("CONFLICTS", conflicts_section.clone()),
    ]);

    template.render(&variables).unwrap_or_else(|e| {
        eprintln!("Warning: Failed to render conflict resolution template: {e}");
        let fallback_template_content = include_str!("templates/conflict_resolution_fallback.txt");
        let fallback_template = Template::new(fallback_template_content);
        fallback_template.render(&variables).unwrap_or_else(|e| {
            eprintln!("Critical: Failed to render fallback template: {e}");
            format!(
                "# MERGE CONFLICT RESOLUTION\n\nResolve these conflicts:\n\n{}",
                &conflicts_section
            )
        })
    })
}

/// Build a conflict resolution prompt using template registry.
///
/// This version uses the template registry which supports user template overrides.
/// It's the recommended way to generate prompts going forward.
///
/// # Arguments
///
/// * `context` - Template context containing the template registry
/// * `conflicts` - Map of file paths to their conflict information
/// * `prompt_md_content` - Optional content from PROMPT.md for task context
/// * `plan_content` - Optional content from PLAN.md for additional context
#[must_use]
#[expect(
    clippy::print_stderr,
    reason = "error logging for template rendering failures"
)]
pub fn build_conflict_resolution_prompt_with_context<S: std::hash::BuildHasher>(
    context: &TemplateContext,
    conflicts: &HashMap<String, FileConflict, S>,
    prompt_md_content: Option<&str>,
    plan_content: Option<&str>,
) -> String {
    let template_content = context
        .registry()
        .get_template("conflict_resolution")
        .unwrap_or_else(|_| include_str!("templates/conflict_resolution.txt").to_string());
    let template = Template::new(&template_content);

    let ctx_section = format_context_section(prompt_md_content, plan_content);
    let conflicts_section = format_conflicts_section(conflicts);

    let variables = HashMap::from([
        ("CONTEXT", ctx_section),
        ("CONFLICTS", conflicts_section.clone()),
    ]);

    template.render(&variables).unwrap_or_else(|e| {
        eprintln!("Warning: Failed to render conflict resolution template: {e}");
        // Use fallback template
        let fallback_template_content = context
            .registry()
            .get_template("conflict_resolution_fallback")
            .unwrap_or_else(|_| {
                include_str!("templates/conflict_resolution_fallback.txt").to_string()
            });
        let fallback_template = Template::new(&fallback_template_content);
        fallback_template.render(&variables).unwrap_or_else(|e| {
            eprintln!("Critical: Failed to render fallback template: {e}");
            // Last resort: minimal emergency prompt - conflicts_section is captured from closure
            format!(
                "# MERGE CONFLICT RESOLUTION\n\nResolve these conflicts:\n\n{}",
                &conflicts_section
            )
        })
    })
}

/// Format the context section with PROMPT.md and PLAN.md content.
///
/// This helper builds the context section that gets injected into the
/// {{CONTEXT}} template variable.
fn format_context_section(prompt_md_content: Option<&str>, plan_content: Option<&str>) -> String {
    let prompt_part = prompt_md_content.map(|prompt_md| {
        format!(
            "## Task Context\n\nThe user was working on the following task:\n\n```\n{}\n```\n\n",
            prompt_md
        )
    });

    let plan_part = plan_content.map(|plan| {
        format!(
            "## Implementation Plan\n\nThe following plan was being implemented:\n\n```\n{}\n```\n\n",
            plan
        )
    });

    [prompt_part, plan_part]
        .into_iter()
        .flatten()
        .collect::<String>()
}

/// Format the conflicts section for all conflicted files.
///
/// This helper builds the conflicts section that gets injected into the
/// {{CONFLICTS}} template variable.
fn format_conflicts_section<S: std::hash::BuildHasher>(
    conflicts: &HashMap<String, FileConflict, S>,
) -> String {
    let sections: Vec<String> = conflicts
        .iter()
        .map(|(path, conflict)| {
            let header = format!("### {path}\n\n");
            let current = format!(
                "Current state (with conflict markers):\n\n```{}\n{}\n```\n\n",
                get_language_marker(path),
                conflict.current_content
            );
            let conflict_part = if conflict.conflict_content.is_empty() {
                String::new()
            } else {
                format!(
                    "Conflict sections:\n\n```{}\n{}\n```\n\n",
                    get_language_marker(path),
                    conflict.conflict_content
                )
            };
            [header, current, conflict_part].join("")
        })
        .collect();

    sections.join("")
}

/// Get a language marker for syntax highlighting based on file extension.
fn get_language_marker(path: &str) -> String {
    let ext = Path::new(path)
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("");

    match ext {
        "rs" => "rust",
        "py" => "python",
        "js" | "jsx" => "javascript",
        "ts" | "tsx" => "typescript",
        "go" => "go",
        "java" => "java",
        "c" => "c",
        "cpp" | "cc" | "cxx" => "cpp",
        "h" | "hpp" => "cpp",
        "cs" => "csharp",
        "rb" => "ruby",
        "php" => "php",
        "swift" => "swift",
        "kt" | "kts" => "kotlin",
        "scala" => "scala",
        "sh" | "bash" | "zsh" => "bash",
        "yml" | "yaml" => "yaml",
        "json" => "json",
        "toml" => "toml",
        "xml" => "xml",
        "html" | "htm" => "html",
        "css" => "css",
        "scss" | "sass" => "scss",
        "sql" => "sql",
        "md" | "markdown" => "markdown",
        _ => "",
    }
    .to_string()
}

/// Branch information for enhanced context.
#[derive(Debug, Clone)]
pub struct BranchInfo {
    /// Current branch name
    pub current_branch: String,
    /// Upstream/target branch name
    pub upstream_branch: String,
    /// Recent commits on current branch
    pub current_commits: Vec<String>,
    /// Recent commits on upstream branch
    pub upstream_commits: Vec<String>,
    /// Number of diverging commits
    pub diverging_count: usize,
}

/// Build an enhanced conflict resolution prompt with branch information.
///
/// This version includes additional context about the branches involved
/// in the conflict for more informed resolution.
///
/// # Arguments
///
/// * `context` - Template context containing the template registry
/// * `conflicts` - Map of file paths to their conflict information
/// * `branch_info` - Optional branch information for enhanced context
/// * `prompt_md_content` - Optional content from PROMPT.md for task context
/// * `plan_content` - Optional content from PLAN.md for additional context
#[must_use]
#[expect(
    clippy::print_stderr,
    reason = "error logging for template rendering failures"
)]
pub fn build_enhanced_conflict_resolution_prompt<S: std::hash::BuildHasher>(
    context: &TemplateContext,
    conflicts: &HashMap<String, FileConflict, S>,
    branch_info: Option<&BranchInfo>,
    prompt_md_content: Option<&str>,
    plan_content: Option<&str>,
) -> String {
    let template_content = context
        .registry()
        .get_template("conflict_resolution")
        .unwrap_or_else(|_| include_str!("templates/conflict_resolution.txt").to_string());
    let template = Template::new(&template_content);

    let ctx_section = match branch_info {
        Some(info) => {
            format_context_section(prompt_md_content, plan_content)
                + &format_branch_info_section(info)
        }
        None => format_context_section(prompt_md_content, plan_content),
    };

    let conflicts_section = format_conflicts_section(conflicts);

    let variables = HashMap::from([
        ("CONTEXT", ctx_section),
        ("CONFLICTS", conflicts_section.clone()),
    ]);

    template.render(&variables).unwrap_or_else(|e| {
        eprintln!("Warning: Failed to render conflict resolution template: {e}");
        // Use fallback template
        let fallback_template_content = context
            .registry()
            .get_template("conflict_resolution_fallback")
            .unwrap_or_else(|_| {
                include_str!("templates/conflict_resolution_fallback.txt").to_string()
            });
        let fallback_template = Template::new(&fallback_template_content);
        fallback_template.render(&variables).unwrap_or_else(|e| {
            eprintln!("Critical: Failed to render fallback template: {e}");
            // Last resort: minimal emergency prompt - conflicts_section is captured from closure
            format!(
                "# MERGE CONFLICT RESOLUTION\n\nResolve these conflicts:\n\n{}",
                &conflicts_section
            )
        })
    })
}

/// Format branch information for context section.
///
/// This helper builds a branch information section that gets injected
/// into the context for AI conflict resolution.
fn format_branch_info_section(info: &BranchInfo) -> String {
    let header = format!(
        "## Branch Information\n\n- **Current branch**: `{}`\n- **Target branch**: `{}`\n- **Diverging commits**: {}\n\n",
        info.current_branch, info.upstream_branch, info.diverging_count
    );

    let current_commits_section = if info.current_commits.is_empty() {
        String::new()
    } else {
        let commits: Vec<String> = info
            .current_commits
            .iter()
            .take(5)
            .enumerate()
            .map(|(i, msg)| format!("{}. {}", i + 1, msg))
            .collect();
        format!(
            "### Recent commits on current branch:\n\n{}\n\n",
            commits.join("\n")
        )
    };

    let upstream_commits_section = if info.upstream_commits.is_empty() {
        String::new()
    } else {
        let commits: Vec<String> = info
            .upstream_commits
            .iter()
            .take(5)
            .enumerate()
            .map(|(i, msg)| format!("{}. {}", i + 1, msg))
            .collect();
        format!(
            "### Recent commits on target branch:\n\n{}\n\n",
            commits.join("\n")
        )
    };

    [header, current_commits_section, upstream_commits_section]
        .into_iter()
        .filter(|s| !s.is_empty())
        .collect()
}

/// Collect branch information for conflict resolution.
///
/// Queries git to gather information about the branches involved in the conflict.
///
/// # Arguments
///
/// * `upstream_branch` - The name of the upstream/target branch
/// * `executor` - Process executor for external process execution
///
/// # Returns
///
/// Returns `Ok(BranchInfo)` with the gathered information, or an error if git operations fail.
///
/// # Errors
///
/// Returns error if the operation fails.
pub fn collect_branch_info(
    upstream_branch: &str,
    executor: &dyn crate::executor::ProcessExecutor,
) -> std::io::Result<BranchInfo> {
    // Get current branch name
    let current_branch =
        executor.execute("git", &["rev-parse", "--abbrev-ref", "HEAD"], &[], None)?;

    let current_branch = current_branch.stdout.trim().to_string();

    // Get recent commits from current branch
    let current_log = executor.execute("git", &["log", "--oneline", "-10", "HEAD"], &[], None)?;

    let current_commits: Vec<String> = current_log
        .stdout
        .lines()
        .map(std::string::ToString::to_string)
        .collect();

    // Get recent commits from upstream branch
    let upstream_log = executor.execute(
        "git",
        &["log", "--oneline", "-10", upstream_branch],
        &[],
        None,
    )?;

    let upstream_commits: Vec<String> = upstream_log
        .stdout
        .lines()
        .map(std::string::ToString::to_string)
        .collect();

    // Count diverging commits
    let diverging = executor.execute(
        "git",
        &[
            "rev-list",
            "--count",
            "--left-right",
            &format!("HEAD...{upstream_branch}"),
        ],
        &[],
        None,
    )?;

    let diverging_count = diverging
        .stdout
        .split_whitespace()
        .map(|s| s.parse::<usize>().unwrap_or(0))
        .sum::<usize>();

    Ok(BranchInfo {
        current_branch,
        upstream_branch: upstream_branch.to_string(),
        current_commits,
        upstream_commits,
        diverging_count,
    })
}

/// Collect conflict information from all conflicted files.
///
/// This function reads all conflicted files and builds a map of
/// file paths to their conflict information.
///
/// # Arguments
///
/// * `conflicted_paths` - List of paths to conflicted files
///
/// # Returns
///
/// Returns `Ok(HashMap)` mapping file paths to conflict information,
/// or an error if a file cannot be read.
///
/// # Errors
///
/// Returns error if the operation fails.
pub fn collect_conflict_info_with_workspace(
    workspace: &dyn Workspace,
    conflicted_paths: &[String],
) -> std::io::Result<HashMap<String, FileConflict>> {
    let conflicts: std::io::Result<Vec<(String, FileConflict)>> = conflicted_paths
        .iter()
        .map(|path| {
            let current_content = workspace.read(Path::new(path))?;
            let conflict_content = extract_conflict_sections_from_content(&current_content);
            Ok((
                path.clone(),
                FileConflict {
                    conflict_content,
                    current_content,
                },
            ))
        })
        .collect();

    let result: HashMap<String, FileConflict> = conflicts?.into_iter().collect();

    Ok(result)
}

fn extract_conflict_sections_from_content(content: &str) -> String {
    let lines: Vec<&str> = content.lines().collect();

    // Find all conflict markers and extract sections between them
    let conflict_sections: Vec<String> = lines
        .iter()
        .enumerate()
        .filter(|(_, line)| line.trim_start().starts_with("<<<<<<<"))
        .filter_map(|(start_idx, _)| {
            // Find the ======= line
            let equals_idx = lines
                .get(start_idx + 1..)?
                .iter()
                .position(|line| line.trim_start().starts_with("======="))
                .map(|i| start_idx + 1 + i);

            // Find the >>>>>>> line after =======
            let end_idx = equals_idx.and_then(|eq_idx| {
                lines
                    .get(eq_idx + 1..)?
                    .iter()
                    .position(|line| line.trim_start().starts_with(">>>>>>>"))
                    .map(|i| eq_idx + 1 + i)
            });

            // Extract the full conflict section
            let end = end_idx.unwrap_or(lines.len() - 1) + 1;
            Some(lines.get(start_idx..end)?.join("\n"))
        })
        .collect();

    if conflict_sections.is_empty() {
        String::new()
    } else {
        conflict_sections.join("\n\n")
    }
}

#[cfg(test)]
mod tests;

#[cfg(test)]
mod io_tests;