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
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
//! XSD validation for fix result XML format.
//!
//! This module provides validation of XML output against the XSD schema
//! to ensure AI agent output conforms to the expected format for fix results.
//!
//! Uses `quick_xml` for robust XML parsing with proper whitespace handling.

use crate::files::llm_output_extraction::xml_helpers::{
    create_reader, duplicate_element_error, format_content_preview, malformed_xml_error,
    missing_required_error, read_text_until_end, read_text_until_end_fuzzy, skip_to_end,
    tolerant_parsing::{normalize_enum_value, normalize_tag_name, FIX_STATUS_SYNONYMS},
};
use crate::files::llm_output_extraction::xsd_validation::{XsdErrorType, XsdValidationError};
use quick_xml::events::Event;

/// Example of a valid fix result XML for error messages.
const EXAMPLE_FIX_RESULT_XML: &str = r"<ralph-fix-result>
<ralph-status>all_issues_addressed</ralph-status>
<ralph-summary>Fixed all 3 issues found during review</ralph-summary>
</ralph-fix-result>";

/// Valid status values for fix results.
const VALID_STATUSES: [&str; 3] = ["all_issues_addressed", "issues_remain", "no_issues_found"];

/// Known child element tags for fix result validation.
/// Used for fuzzy tag name matching (typo tolerance).
const KNOWN_FIX_RESULT_TAGS: &[&str] = &["ralph-status", "ralph-summary"];

/// Validate fix result XML content against the XSD schema.
///
/// This function validates that the XML content conforms to the expected
/// fix result format defined in `fix_result.xsd`:
///
/// ```xml
/// <ralph-fix-result>
///   <ralph-status>all_issues_addressed|issues_remain|no_issues_found</ralph-status>
///   <ralph-summary>Optional summary of fixes applied</ralph-summary>
/// </ralph-fix-result>
/// ```
///
/// # Arguments
///
/// * `xml_content` - The XML content to validate
///
/// # Returns
///
/// * `Ok(FixResultElements)` if the XML is valid and contains all required elements
/// * `Err(XsdValidationError)` if the XML is invalid or doesn't conform to the schema
///
/// # Errors
///
/// Returns error if the operation fails.
pub fn validate_fix_result_xml(xml_content: &str) -> Result<FixResultElements, XsdValidationError> {
    use crate::files::llm_output_extraction::xml_helpers::check_for_illegal_xml_characters;

    let content = xml_content.trim();

    // Check for illegal XML characters BEFORE parsing
    check_for_illegal_xml_characters(content)?;

    let parsed_state =
        parse_fix_result_state(content, &mut create_reader(content), &mut Vec::new())?;
    let FixResultParseState { status, summary } = parsed_state;

    // Validate required element: status
    let status = status.ok_or_else(|| {
        missing_required_error(
            "ralph-status",
            "ralph-fix-result",
            Some(EXAMPLE_FIX_RESULT_XML),
        )
    })?;

    // Validate status content
    if status.is_empty() {
        return Err(XsdValidationError {
            error_type: XsdErrorType::InvalidContent,
            element_path: "ralph-status".to_string(),
            expected: "non-empty status value".to_string(),
            found: "empty status".to_string(),
            suggestion: format!(
                "The <ralph-status> must contain one of: {}",
                VALID_STATUSES.join(", ")
            ),
            example: Some(EXAMPLE_FIX_RESULT_XML.into()),
        });
    }

    // Tolerant: normalize status via synonym table (case-insensitive, synonym mapping).
    // Returns the canonical form if the value is recognized, or None if truly ambiguous.
    let Some(status) = normalize_enum_value(&status, &VALID_STATUSES, FIX_STATUS_SYNONYMS) else {
        return Err(XsdValidationError {
            error_type: XsdErrorType::InvalidContent,
            element_path: "ralph-status".to_string(),
            expected: format!("one of: {}", VALID_STATUSES.join(", ")),
            found: status.clone(),
            suggestion: format!(
                "Change <ralph-status>{}</ralph-status> to use a valid value: {}",
                status,
                VALID_STATUSES.join(", ")
            ),
            example: Some(EXAMPLE_FIX_RESULT_XML.into()),
        });
    };

    Ok(FixResultElements {
        status,
        summary: summary.filter(|s| !s.is_empty()),
    })
}

#[derive(Debug, Default)]
struct FixResultParseState {
    status: Option<String>,
    summary: Option<String>,
}

fn parse_fix_result_state(
    content: &str,
    reader: &mut quick_xml::Reader<&[u8]>,
    buf: &mut Vec<u8>,
) -> Result<FixResultParseState, XsdValidationError> {
    find_fix_root(content, reader, buf)
        .and_then(|_| parse_fix_children(reader, buf, FixResultParseState::default()))
}

fn find_fix_root(
    content: &str,
    reader: &mut quick_xml::Reader<&[u8]>,
    buf: &mut Vec<u8>,
) -> Result<(), XsdValidationError> {
    *buf = Vec::new();
    match reader.read_event_into(buf) {
        Ok(Event::Start(e)) if e.name().as_ref() == b"ralph-fix-result" => Ok(()),
        Ok(Event::Start(e)) => {
            let name_bytes = e.name();
            let tag_name = String::from_utf8_lossy(name_bytes.as_ref());
            Err(XsdValidationError {
                error_type: XsdErrorType::MissingRequiredElement,
                element_path: "ralph-fix-result".to_string(),
                expected: "<ralph-fix-result> as root element".to_string(),
                found: format!("<{tag_name}> (wrong root element)"),
                suggestion: "Use <ralph-fix-result> as the root element.".to_string(),
                example: Some(EXAMPLE_FIX_RESULT_XML.into()),
            })
        }
        Ok(Event::Eof) => Err(XsdValidationError {
            error_type: XsdErrorType::MissingRequiredElement,
            element_path: "ralph-fix-result".to_string(),
            expected: "<ralph-fix-result> as root element".to_string(),
            found: format_content_preview(content),
            suggestion: "Wrap your result in <ralph-fix-result>...</ralph-fix-result> tags."
                .to_string(),
            example: Some(EXAMPLE_FIX_RESULT_XML.into()),
        }),
        Ok(_) => find_fix_root(content, reader, buf),
        Err(e) => Err(malformed_xml_error(&e)),
    }
}

fn parse_fix_children(
    reader: &mut quick_xml::Reader<&[u8]>,
    buf: &mut Vec<u8>,
    state: FixResultParseState,
) -> Result<FixResultParseState, XsdValidationError> {
    *buf = Vec::new();
    match reader.read_event_into(buf) {
        Ok(Event::Start(e)) => {
            let next_state = parse_fix_start_child(reader, &state, e.name().as_ref())?;
            parse_fix_children(reader, buf, next_state)
        }
        Ok(Event::Text(_)) => parse_fix_children(reader, buf, state),
        Ok(Event::Empty(e)) => {
            let next_state = parse_fix_empty_child(&state, e.name().as_ref())?;
            parse_fix_children(reader, buf, next_state)
        }
        Ok(Event::End(e)) if e.name().as_ref() == b"ralph-fix-result" => Ok(state),
        Ok(Event::Eof) => Err(XsdValidationError {
            error_type: XsdErrorType::MalformedXml,
            element_path: "ralph-fix-result".to_string(),
            expected: "closing </ralph-fix-result> tag".to_string(),
            found: "end of content without closing tag".to_string(),
            suggestion: "Add </ralph-fix-result> at the end.".to_string(),
            example: Some(EXAMPLE_FIX_RESULT_XML.into()),
        }),
        Ok(_) => parse_fix_children(reader, buf, state),
        Err(e) => Err(malformed_xml_error(&e)),
    }
}

fn parse_fix_start_child(
    reader: &mut quick_xml::Reader<&[u8]>,
    state: &FixResultParseState,
    tag: &[u8],
) -> Result<FixResultParseState, XsdValidationError> {
    match tag {
        b"ralph-status" => with_status(
            state,
            read_text_until_end(reader, b"ralph-status")?,
            "ralph-status",
        ),
        b"ralph-summary" => with_summary(
            state,
            read_text_until_end(reader, b"ralph-summary")?,
            "ralph-summary",
        ),
        other => parse_fix_start_child_fuzzy(reader, state, other),
    }
}

fn parse_fix_start_child_fuzzy(
    reader: &mut quick_xml::Reader<&[u8]>,
    state: &FixResultParseState,
    original_tag: &[u8],
) -> Result<FixResultParseState, XsdValidationError> {
    match normalize_fix_child_tag(original_tag) {
        Some("ralph-status") => with_status(
            state,
            read_text_until_end_fuzzy(reader, b"ralph-status", original_tag)?,
            "ralph-status",
        ),
        Some("ralph-summary") => with_summary(
            state,
            read_text_until_end_fuzzy(reader, b"ralph-summary", original_tag)?,
            "ralph-summary",
        ),
        Some(_) => {
            let _ = skip_to_end(reader, original_tag);
            Ok(FixResultParseState {
                status: state.status.clone(),
                summary: state.summary.clone(),
            })
        }
        None => {
            let _ = skip_to_end(reader, original_tag);
            Ok(FixResultParseState {
                status: state.status.clone(),
                summary: state.summary.clone(),
            })
        }
    }
}

fn parse_fix_empty_child(
    state: &FixResultParseState,
    tag: &[u8],
) -> Result<FixResultParseState, XsdValidationError> {
    match tag {
        b"ralph-status" => with_status(state, String::new(), "ralph-status"),
        b"ralph-summary" => with_summary(state, String::new(), "ralph-summary"),
        other => match normalize_fix_child_tag(other) {
            Some("ralph-status") => with_status(state, String::new(), "ralph-status"),
            Some("ralph-summary") => with_summary(state, String::new(), "ralph-summary"),
            Some(_) | None => Ok(FixResultParseState {
                status: state.status.clone(),
                summary: state.summary.clone(),
            }),
        },
    }
}

fn with_status(
    state: &FixResultParseState,
    status: String,
    element_name: &'static str,
) -> Result<FixResultParseState, XsdValidationError> {
    state
        .status
        .as_ref()
        .map(|_| duplicate_element_error(element_name, "ralph-fix-result"))
        .map_or_else(
            || {
                Ok(FixResultParseState {
                    status: Some(status),
                    summary: state.summary.clone(),
                })
            },
            Err,
        )
}

fn with_summary(
    state: &FixResultParseState,
    summary: String,
    element_name: &'static str,
) -> Result<FixResultParseState, XsdValidationError> {
    state
        .summary
        .as_ref()
        .map(|_| duplicate_element_error(element_name, "ralph-fix-result"))
        .map_or_else(
            || {
                Ok(FixResultParseState {
                    status: state.status.clone(),
                    summary: Some(summary),
                })
            },
            Err,
        )
}

fn normalize_fix_child_tag(tag: &[u8]) -> Option<&'static str> {
    let tag_name = String::from_utf8_lossy(tag);
    normalize_tag_name(&tag_name, KNOWN_FIX_RESULT_TAGS)
}

/// Parsed fix result elements from valid XML.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FixResultElements {
    /// The fix status (required).
    ///
    /// This field always contains a canonical, normalized status value. The validator
    /// applies tolerant parsing (see `xml_helpers::tolerant_parsing::normalize_enum_value`)
    /// before storing the status, so this field is guaranteed to be one of the canonical
    /// values: `"all_issues_addressed"`, `"issues_remain"`, or `"no_issues_found"`.
    ///
    /// Downstream consumers can safely use exact string comparison
    /// without needing to handle synonym values or case variations.
    pub status: String,
    /// Optional summary of fixes applied
    pub summary: Option<String>,
}

impl FixResultElements {
    /// Returns true if all issues have been addressed or no issues were found.
    #[must_use]
    pub fn is_complete(&self) -> bool {
        self.status == "all_issues_addressed" || self.status == "no_issues_found"
    }

    /// Returns true if issues remain.
    #[must_use]
    pub fn has_remaining_issues(&self) -> bool {
        self.status == "issues_remain"
    }

    /// Returns true if no issues were found.
    #[must_use]
    pub fn is_no_issues(&self) -> bool {
        self.status == "no_issues_found"
    }
}

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

    #[test]
    fn test_validate_valid_all_issues_addressed() {
        let xml = r"<ralph-fix-result>
<ralph-status>all_issues_addressed</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(result.is_ok());
        let elements = result.unwrap();
        assert_eq!(elements.status, "all_issues_addressed");
        assert!(elements.is_complete());
        assert!(!elements.has_remaining_issues());
    }

    #[test]
    fn test_validate_valid_issues_remain() {
        let xml = r"<ralph-fix-result>
<ralph-status>issues_remain</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(result.is_ok());
        let elements = result.unwrap();
        assert_eq!(elements.status, "issues_remain");
        assert!(elements.has_remaining_issues());
        assert!(!elements.is_complete());
    }

    #[test]
    fn test_validate_valid_no_issues_found() {
        let xml = r"<ralph-fix-result>
<ralph-status>no_issues_found</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(result.is_ok());
        let elements = result.unwrap();
        assert_eq!(elements.status, "no_issues_found");
        assert!(elements.is_no_issues());
    }

    #[test]
    fn test_validate_valid_with_summary() {
        let xml = r"<ralph-fix-result>
<ralph-status>all_issues_addressed</ralph-status>
<ralph-summary>All reported issues have been fixed</ralph-summary>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(result.is_ok());
        let elements = result.unwrap();
        assert_eq!(elements.status, "all_issues_addressed");
        assert_eq!(
            elements.summary,
            Some("All reported issues have been fixed".to_string())
        );
    }

    #[test]
    fn test_validate_missing_root_element() {
        let xml = r"Some random text without proper XML tags";

        let result = validate_fix_result_xml(xml);
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert_eq!(error.element_path, "ralph-fix-result");
    }

    #[test]
    fn test_validate_missing_status() {
        let xml = r"<ralph-fix-result>
<ralph-summary>No status</ralph-summary>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(error.element_path.contains("ralph-status"));
    }

    #[test]
    fn test_validate_invalid_status() {
        let xml = r"<ralph-fix-result>
<ralph-status>invalid_status_value</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(error.expected.contains("all_issues_addressed"));
    }

    #[test]
    fn test_tolerant_fix_status_synonym_fixed() {
        // "fixed" should map to "all_issues_addressed"
        let xml = r"<ralph-fix-result>
<ralph-status>fixed</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Synonym 'fixed' should be accepted as 'all_issues_addressed': {result:?}"
        );
        let elements = result.unwrap();
        assert_eq!(
            elements.status, "all_issues_addressed",
            "Synonym 'fixed' should be normalized to 'all_issues_addressed'"
        );
    }

    #[test]
    fn test_tolerant_fix_status_case_insensitive() {
        // ALL_ISSUES_ADDRESSED should be accepted (case-insensitive)
        let xml = r"<ralph-fix-result>
<ralph-status>ALL_ISSUES_ADDRESSED</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Case-insensitive 'ALL_ISSUES_ADDRESSED' should be accepted: {result:?}"
        );
        let elements = result.unwrap();
        assert_eq!(
            elements.status, "all_issues_addressed",
            "Case-insensitive 'ALL_ISSUES_ADDRESSED' should be normalized to lowercase"
        );
    }

    #[test]
    fn test_tolerant_fix_skips_unknown_elements() {
        // Extra elements alongside valid ones should be skipped
        let xml = r"<ralph-fix-result>
<ralph-status>all_issues_addressed</ralph-status>
<ralph-summary>All fixed</ralph-summary>
<ralph-extra-info>some extra info</ralph-extra-info>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Unknown elements should be skipped, not rejected: {result:?}"
        );
        let elements = result.unwrap();
        assert_eq!(elements.status, "all_issues_addressed");
    }

    #[test]
    fn test_truly_unknown_fix_status_rejected() {
        // Ambiguous/unknown status values should still be rejected
        let xml = r"<ralph-fix-result>
<ralph-status>banana</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_err(),
            "Truly unknown status 'banana' should still be rejected"
        );
        let error = result.unwrap_err();
        assert!(
            error.element_path.contains("ralph-status"),
            "Error should reference ralph-status"
        );
    }

    #[test]
    fn test_validate_empty_status() {
        let xml = r"<ralph-fix-result>
<ralph-status>   </ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(result.is_err());
    }

    #[test]
    fn test_tolerant_fix_status_synonym_all_fixed_maps_to_all_issues_addressed() {
        let xml = r"<ralph-fix-result>
<ralph-status>all_fixed</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Synonym 'all_fixed' should be accepted as 'all_issues_addressed': {result:?}"
        );
        let elements = result.unwrap();
        assert_eq!(elements.status, "all_issues_addressed");
    }

    #[test]
    fn test_tolerant_fix_status_synonym_remaining_maps_to_issues_remain() {
        let xml = r"<ralph-fix-result>
<ralph-status>remaining</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Synonym 'remaining' should be accepted as 'issues_remain': {result:?}"
        );
        let elements = result.unwrap();
        assert_eq!(elements.status, "issues_remain");
    }

    #[test]
    fn test_tolerant_fix_status_synonym_no_issues_maps_to_no_issues_found() {
        let xml = r"<ralph-fix-result>
<ralph-status>no_issues</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Synonym 'no_issues' should be accepted as 'no_issues_found': {result:?}"
        );
        let elements = result.unwrap();
        assert_eq!(elements.status, "no_issues_found");
    }

    #[test]
    fn test_tolerant_fix_status_case_insensitive_issues_remain() {
        let xml = r"<ralph-fix-result>
<ralph-status>ISSUES_REMAIN</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Case-insensitive 'ISSUES_REMAIN' should be accepted: {result:?}"
        );
        let elements = result.unwrap();
        assert_eq!(elements.status, "issues_remain");
    }

    #[test]
    fn test_tolerant_fix_skips_self_closing_unknown_element() {
        // Self-closing unknown elements should be skipped (tests Event::Empty handler)
        let xml = r"<ralph-fix-result>
<ralph-status>all_issues_addressed</ralph-status>
<ralph-meta/>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Self-closing unknown element should be skipped, not rejected: {result:?}"
        );
        let elements = result.unwrap();
        assert_eq!(elements.status, "all_issues_addressed");
    }

    #[test]
    fn test_tolerant_fix_element_reordering_summary_before_status() {
        // summary before status should still parse correctly
        let xml = r"<ralph-fix-result>
<ralph-summary>All issues have been addressed</ralph-summary>
<ralph-status>all_issues_addressed</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Element reordering (summary before status) should be tolerated: {result:?}"
        );
        let elements = result.unwrap();
        assert_eq!(elements.status, "all_issues_addressed");
        assert_eq!(
            elements.summary,
            Some("All issues have been addressed".to_string())
        );
    }

    #[test]
    fn test_tolerant_fix_stray_text_between_elements() {
        // Stray text between elements should be tolerated
        let xml = "<ralph-fix-result>\nSome stray text\n<ralph-status>all_issues_addressed</ralph-status>\nMore stray text\n</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Stray text between elements should be tolerated: {result:?}"
        );
        let elements = result.unwrap();
        assert_eq!(elements.status, "all_issues_addressed");
    }

    #[test]
    fn test_tolerant_fix_truly_ambiguous_status_rejected() {
        // Ambiguous/unknown status values should still be rejected
        let xml = r"<ralph-fix-result>
<ralph-status>partially_fixed</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_err(),
            "Ambiguous status 'partially_fixed' should still be rejected"
        );
        let error = result.unwrap_err();
        assert!(
            error.element_path.contains("ralph-status"),
            "Error should reference ralph-status"
        );
    }

    #[test]
    fn test_tolerant_fix_empty_self_closing_status_rejected() {
        // Empty self-closing status should be rejected (no value)
        let xml = r"<ralph-fix-result>
<ralph-status/>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_err(),
            "Empty self-closing status element should be rejected"
        );
        let error = result.unwrap_err();
        assert!(
            error.element_path.contains("ralph-status"),
            "Error should reference ralph-status"
        );
    }

    #[test]
    fn test_validate_duplicate_status() {
        let xml = r"<ralph-fix-result>
<ralph-status>all_issues_addressed</ralph-status>
<ralph-status>issues_remain</ralph-status>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_whitespace_handling() {
        // This is the key test - quick_xml should handle whitespace between elements
        let xml = "  <ralph-fix-result>  \n  <ralph-status>all_issues_addressed</ralph-status>  \n  </ralph-fix-result>  ";

        let result = validate_fix_result_xml(xml);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_with_xml_declaration() {
        let xml = r#"<?xml version="1.0"?>
<ralph-fix-result>
<ralph-status>all_issues_addressed</ralph-status>
</ralph-fix-result>"#;

        let result = validate_fix_result_xml(xml);
        assert!(result.is_ok());
    }

    // =========================================================================
    // Fuzzy tag matching tests (Step 5 of implementation plan)
    // =========================================================================

    /// Test: misspelled ralph-summry tag resolves to ralph-summary.
    #[test]
    fn test_tolerant_fix_misspelled_summary_tag_accepted() {
        let xml = r"<ralph-fix-result>
<ralph-status>all_issues_addressed</ralph-status>
<ralph-summry>Fixed all issues</ralph-summry>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Misspelled <ralph-summry> should be accepted: {result:?}"
        );
        let elements = result.unwrap();
        assert_eq!(
            elements.summary,
            Some("Fixed all issues".to_string()),
            "Summary content should be correctly extracted from misspelled tag"
        );
    }

    /// Test: misspelled ralph-statuss tag resolves to ralph-status.
    #[test]
    fn test_tolerant_fix_misspelled_status_tag_accepted() {
        let xml = r"<ralph-fix-result>
<ralph-statuss>all_issues_addressed</ralph-statuss>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Misspelled <ralph-statuss> should be accepted: {result:?}"
        );
        let elements = result.unwrap();
        assert_eq!(
            elements.status, "all_issues_addressed",
            "Status should be correctly extracted from misspelled tag"
        );
    }

    /// Test: completely unknown tag (large edit distance) is skipped.
    #[test]
    fn test_tolerant_fix_completely_unknown_tag_skipped() {
        let xml = r"<ralph-fix-result>
<ralph-status>all_issues_addressed</ralph-status>
<ralph-banana>this should be ignored</ralph-banana>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Unknown tag with large edit distance should be skipped: {result:?}"
        );
        let elements = result.unwrap();
        assert_eq!(elements.status, "all_issues_addressed");
    }

    /// Test: self-closing misspelled tag is also handled.
    #[test]
    fn test_tolerant_fix_self_closing_misspelled_tag() {
        let xml = r"<ralph-fix-result>
<ralph-status>all_issues_addressed</ralph-status>
<ralph-summry/>
</ralph-fix-result>";

        let result = validate_fix_result_xml(xml);
        assert!(
            result.is_ok(),
            "Self-closing misspelled tag should be handled: {result:?}"
        );
        let elements = result.unwrap();
        // Self-closing misspelled tag should be treated as empty and skipped
        assert!(elements.summary.is_none());
    }

    #[test]
    fn test_normalize_fix_child_tag_unknown_returns_none() {
        assert_eq!(normalize_fix_child_tag(b"ralph-banana"), None);
    }
}