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
//! Flexible XML extraction helpers.
//!
//! Extracts `<ralph-commit>...</ralph-commit>` blocks from common embedding patterns.

use crate::files::llm_output_extraction::cleaning::unescape_json_strings_aggressive;

/// Extract an XML commit message from AI output using multiple strategies.
#[must_use]
pub fn extract_xml_commit(content: &str) -> Option<String> {
    // Strategy 1: Direct XML at start (most efficient)
    if let Some(xml) = try_extract_direct_xml(content) {
        return Some(xml);
    }

    // Strategy 2: XML in markdown code fence
    if let Some(xml) = try_extract_from_markdown_fence(content) {
        return Some(xml);
    }

    // Strategy 3: XML in JSON string (escaped)
    if let Some(xml) = try_extract_from_json_string(content) {
        return Some(xml);
    }

    // Strategy 4: Search for tags anywhere (most permissive)
    try_extract_embedded_xml(content)
}

/// Strategy 1: Extract XML that starts with `<ralph-commit>` tag.
///
/// This is the most efficient strategy for well-formed output where
/// the AI agent output starts directly with the XML.
fn try_extract_direct_xml(content: &str) -> Option<String> {
    let trimmed = content.trim();

    // Check if content starts with the opening tag
    if !trimmed.starts_with("<ralph-commit>") {
        return None;
    }

    // Find the closing tag
    let start = trimmed.find("<ralph-commit>")?;
    let end = trimmed.find("</ralph-commit>")?;

    if start >= end {
        return None;
    }

    // Extract including both tags
    let xml_end = end + "</ralph-commit>".len();
    Some(trimmed[start..xml_end].to_string())
}

/// Strategy 2: Extract XML from markdown code fences.
///
/// Handles:
/// - ```xml ... ```
/// - ``` ... ```
/// - May have leading/trailing whitespace
fn try_extract_from_markdown_fence(content: &str) -> Option<String> {
    // Pattern 1: ```xml fence
    if let Some(start) = content.find("```xml") {
        let after_fence = &content[start + 6..]; // Skip ```xml

        // Find the end of the code fence
        if let Some(end) = after_fence.find("```") {
            let fence_content = after_fence[..end].trim();
            // Look for ralph-commit tags within the fence
            if let Some(xml) = extract_ralph_commit_from_content(fence_content) {
                return Some(xml);
            }
        }
    }

    // Pattern 2: Generic ``` fence (no language specified)
    if let Some(start) = content.find("```") {
        let after_fence = &content[start + 3..]; // Skip ```

        // Find the end of the code fence
        if let Some(end) = after_fence.find("```") {
            let fence_content = after_fence[..end].trim();
            // Look for ralph-commit tags within the fence
            if let Some(xml) = extract_ralph_commit_from_content(fence_content) {
                return Some(xml);
            }
        }
    }

    None
}

/// Strategy 3: Extract XML from JSON strings (escaped).
///
/// Handles:
/// - NDJSON stream with result field containing escaped XML
/// - Direct JSON with escaped XML in string values
fn try_extract_from_json_string(content: &str) -> Option<String> {
    // Pattern 1: NDJSON stream with result field
    // {"type":"result","result":"<ralph-commit>...<\/ralph-commit>"}
    let ndjson_result = content
        .lines()
        .filter_map(|line| {
            let trimmed = line.trim();
            if !trimmed.starts_with('{') {
                return None;
            }

            let json = serde_json::from_str::<serde_json::Value>(trimmed).ok()?;
            json.get("result")
                .and_then(|v| v.as_str())
                .map(String::from)
        })
        .find_map(|result| {
            extract_ralph_commit_from_content(&result).or_else(|| {
                let unescaped = unescape_json_strings_aggressive(&result);
                extract_ralph_commit_from_content(&unescaped)
            })
        });

    if ndjson_result.is_some() {
        return ndjson_result;
    }

    // Also try content/message fields for NDJSON
    let ndjson_fields_result = content
        .lines()
        .filter_map(|line| {
            let trimmed = line.trim();
            if !trimmed.starts_with('{') {
                return None;
            }

            serde_json::from_str::<serde_json::Value>(trimmed).ok()
        })
        .flat_map(|json| {
            ["content", "message", "output", "text"]
                .into_iter()
                .filter_map(move |field_name| {
                    json.get(field_name)
                        .and_then(|v| v.as_str())
                        .map(|field_value| {
                            (
                                String::from(field_value),
                                unescape_json_strings_aggressive(field_value),
                            )
                        })
                })
        })
        .find_map(|(field_value, unescaped)| {
            extract_ralph_commit_from_content(&field_value)
                .or_else(|| extract_ralph_commit_from_content(&unescaped))
        });

    if ndjson_fields_result.is_some() {
        return ndjson_fields_result;
    }

    // Pattern 2: Direct JSON object (not NDJSON)
    let trimmed = content.trim();
    if trimmed.starts_with('{') && trimmed.contains(r#""result""#) {
        if let Ok(json) = serde_json::from_str::<serde_json::Value>(trimmed) {
            if let Some(result) = json.get("result").and_then(|v| v.as_str()) {
                return extract_ralph_commit_from_content(result).or_else(|| {
                    let unescaped = unescape_json_strings_aggressive(result);
                    extract_ralph_commit_from_content(&unescaped)
                });
            }
        }
    }

    None
}

/// Strategy 4: Search for XML tags anywhere in content.
///
/// This is the most permissive strategy - it looks for the tags
/// anywhere in the content, regardless of what comes before or after.
fn try_extract_embedded_xml(content: &str) -> Option<String> {
    extract_ralph_commit_from_content(content)
}

/// Extract `<ralph-commit>...</ralph-commit>` from arbitrary content.
///
/// This helper function searches for the commit tags within any text,
/// handling cases where the AI agent embedded XML in analysis text.
fn extract_ralph_commit_from_content(content: &str) -> Option<String> {
    let start = content.find("<ralph-commit>")?;
    let end = content.find("</ralph-commit>")?;

    if start >= end {
        return None;
    }

    // Extract including both tags
    let xml_end = end + "</ralph-commit>".len();
    let extracted = &content[start..xml_end];

    // Unescape JSON string escape sequences (e.g., \n -> newline)
    let unescaped = unescape_json_strings_aggressive(extracted);

    Some(unescaped)
}

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

    // =========================================================================
    // Strategy 1: Direct XML tests
    // =========================================================================

    #[test]
    fn test_extract_direct_xml_basic() {
        let content = r"<ralph-commit>
<ralph-subject>feat: add feature</ralph-subject>
</ralph-commit>";
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        assert_eq!(result.unwrap(), content);
    }

    #[test]
    fn test_extract_direct_xml_with_whitespace() {
        let content = r"
<ralph-commit>
<ralph-subject>fix: bug</ralph-subject>
</ralph-commit>
  ";
        let expected = r"<ralph-commit>
<ralph-subject>fix: bug</ralph-subject>
</ralph-commit>";
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        // Whitespace trimmed but content preserved
        assert!(result.unwrap().trim() == expected.trim());
    }

    #[test]
    fn test_extract_direct_xml_not_at_start_fails() {
        // Content that doesn't start with <ralph-commit>
        // should fail for direct extraction but may succeed with other strategies
        let content = r"Here is the commit:
<ralph-commit>
<ralph-subject>feat: add feature</ralph-subject>
</ralph-commit>";
        // Should still extract via embedded search (Strategy 4)
        let result = extract_xml_commit(content);
        assert!(result.is_some());
    }

    // =========================================================================
    // Strategy 2: Markdown fence tests
    // =========================================================================

    #[test]
    fn test_extract_from_xml_fence() {
        let content = r"Here's the commit message:

```xml
<ralph-commit>
<ralph-subject>feat: add feature</ralph-subject>
</ralph-commit>
```

That's it!";
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        let extracted = result.unwrap();
        assert!(extracted.contains("<ralph-commit>"));
        assert!(extracted.contains("</ralph-commit>"));
    }

    #[test]
    fn test_extract_from_generic_fence() {
        let content = r"```
<ralph-commit>
<ralph-subject>fix: resolve bug</ralph-subject>
</ralph-commit>
```";
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        assert!(result.unwrap().contains("<ralph-commit>"));
    }

    #[test]
    fn test_extract_fence_with_body() {
        let content = r"```xml
<ralph-commit>
<ralph-subject>docs: update readme</ralph-subject>
<ralph-body>Add usage examples.
Update installation instructions.</ralph-body>
</ralph-commit>
```";
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        let extracted = result.unwrap();
        assert!(extracted.contains("<ralph-body>"));
        assert!(extracted.contains("usage examples"));
    }

    // =========================================================================
    // Strategy 3: JSON string tests
    // =========================================================================

    #[test]
    fn test_extract_from_ndjson_result() {
        let content = r#"{"type":"result","result":"<ralph-commit>\n<ralph-subject>feat: add</ralph-subject>\n</ralph-commit>"}"#;
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        let extracted = result.unwrap();
        assert!(extracted.contains("<ralph-commit>"));
        assert!(extracted.contains("feat: add"));
    }

    #[test]
    fn test_extract_from_escaped_json() {
        // Double-escaped XML in JSON
        let content = r#"{"type":"result","result":"<ralph-commit>\\n<ralph-subject>fix: bug</ralph-subject>\\n</ralph-commit>"}"#;
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        let extracted = result.unwrap();
        assert!(extracted.contains("<ralph-commit>"));
        assert!(extracted.contains("fix: bug"));
    }

    #[test]
    fn test_extract_from_json_content_field() {
        let content = r#"{"type":"message","content":"<ralph-commit><ralph-subject>chore: update</ralph-subject></ralph-commit>"}"#;
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        assert!(result.unwrap().contains("chore: update"));
    }

    #[test]
    fn test_extract_from_ndjson_stream() {
        let content = r#"{"type":"stream_event","event":"start"}
{"type":"result","result":"<ralph-commit>\n<ralph-subject>test: add coverage</ralph-subject>\n</ralph-commit>"}
{"type":"stream_event","event":"end"}"#;
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        assert!(result.unwrap().contains("test: add coverage"));
    }

    // =========================================================================
    // Strategy 4: Embedded search tests
    // =========================================================================

    #[test]
    fn test_extract_embedded_in_analysis() {
        let content = r"Looking at the diff, I can see the following changes:

The main files modified are parser.rs and extractor.rs.

Based on this analysis, here's the commit message:

<ralph-commit>
<ralph-subject>refactor: simplify parsing logic</ralph-subject>
</ralph-commit>

Let me know if you need any changes!";
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        let extracted = result.unwrap();
        assert!(extracted.contains("<ralph-commit>"));
        assert!(extracted.contains("refactor: simplify"));
    }

    #[test]
    fn test_extract_embedded_with_preamble_and_postscript() {
        let content = r"# Analysis

After reviewing the diff...

## Commit Message

<ralph-commit>
<ralph-subject>feat: add new feature</ralph-subject>
<ralph-body>Implementation details here.</ralph-body>
</ralph-commit>

## Summary

This commit adds the requested feature.";
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        let extracted = result.unwrap();
        assert!(extracted.contains("<ralph-body>"));
        assert!(extracted.contains("Implementation details"));
    }

    #[test]
    fn test_extract_embedded_multiple_xml_blocks() {
        // Should extract the first (or only) valid block
        let content = r"Some text...

<ralph-commit>
<ralph-subject>first: message</ralph-subject>
</ralph-commit>

More analysis...

<ralph-commit>
<ralph-subject>second: message</ralph-subject>
</ralph-commit>";

        let result = extract_xml_commit(content);
        assert!(result.is_some());
        let extracted = result.unwrap();
        // Should extract the first occurrence
        assert!(extracted.contains("first: message"));
    }

    // =========================================================================
    // Edge cases and error conditions
    // =========================================================================

    #[test]
    fn test_extract_no_xml_returns_none() {
        let content = r"This is just plain text without any XML tags.";
        let result = extract_xml_commit(content);
        assert!(result.is_none());
    }

    #[test]
    fn test_extract_malformed_xml_no_closing_tag() {
        let content = r"<ralph-commit>
<ralph-subject>feat: add</ralph-subject>";
        let result = extract_xml_commit(content);
        // Should return None since closing tag is missing
        assert!(result.is_none());
    }

    #[test]
    fn test_extract_malformed_xml_closing_before_opening() {
        let content = r"</ralph-commit>
<ralph-subject>feat: add</ralph-subject>
<ralph-commit>";
        let result = extract_xml_commit(content);
        assert!(result.is_none());
    }

    #[test]
    fn test_extract_empty_content() {
        let result = extract_xml_commit("");
        assert!(result.is_none());
    }

    #[test]
    fn test_extract_only_opening_tag() {
        let content = r"<ralph-commit>
<ralph-subject>feat: add</ralph-subject>";
        let result = extract_xml_commit(content);
        assert!(result.is_none());
    }

    #[test]
    fn test_extract_nested_fence_and_embedded() {
        // XML in a fence, embedded in analysis text
        let content = r"Based on my analysis:

```xml
<ralph-commit>
<ralph-subject>fix: resolve edge case</ralph-subject>
</ralph-commit>
```

This should be extractable.";
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        assert!(result.unwrap().contains("fix: resolve"));
    }

    #[test]
    fn test_extract_detailed_body_format() {
        let content = r"<ralph-commit>
<ralph-subject>feat: add authentication</ralph-subject>
<ralph-body-summary>Implement OAuth2 login flow</ralph-body-summary>
<ralph-body-details>Add support for Google and GitHub providers.
Include session management for tokens.</ralph-body-details>
<ralph-body-footer>Breaks compatibility with v1.0 API</ralph-body-footer>
</ralph-commit>";
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        let extracted = result.unwrap();
        assert!(extracted.contains("<ralph-body-summary>"));
        assert!(extracted.contains("<ralph-body-details>"));
        assert!(extracted.contains("<ralph-body-footer>"));
    }

    #[test]
    fn test_extract_with_scope() {
        let content = r"<ralph-commit>
<ralph-subject>fix(parser): handle edge case in parsing</ralph-subject>
</ralph-commit>";
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        assert!(result.unwrap().contains("fix(parser):"));
    }

    #[test]
    fn test_extract_with_breaking_change() {
        let content = r"<ralph-commit>
<ralph-subject>feat!: remove deprecated API</ralph-subject>
<ralph-body>BREAKING CHANGE: The old API is no longer supported.</ralph-body>
</ralph-commit>";
        let result = extract_xml_commit(content);
        assert!(result.is_some());
        let extracted = result.unwrap();
        assert!(extracted.contains("feat!:"));
        assert!(extracted.contains("BREAKING CHANGE"));
    }
}