sgr-agent 0.5.1

SGR LLM client + agent framework — structured output, function calling, agent loop, 3 agent variants
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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
//! Flexible JSON parser — extracts structured data from messy LLM output.
//!
//! Inspired by BAML's "jsonish" SAP (Schema-Aligned Parsing) approach.
//! Collects multiple parse candidates (AnyOf), tries to deserialize each
//! into the target type `T`, returns the first success.
//!
//! Parse cascade:
//! 1. Direct JSON (`serde_json::from_str`)
//! 2. Markdown code blocks (````json ... ````)
//! 3. Greedy JSON extraction (first `{...}` or `[...]` in text)
//! 4. Fixing parser (close brackets, strip trailing commas, unquoted keys)
//! 5. Fail with all candidates listed
//!
//! Works with any model — no structured output API required.

use schemars::JsonSchema;
use serde::de::DeserializeOwned;

use crate::coerce::coerce_value;
use crate::schema::response_schema_for;

/// A parse candidate with provenance info for debugging.
#[derive(Debug, Clone)]
pub struct Candidate {
    /// The JSON string to try deserializing.
    pub json: String,
    /// How this candidate was extracted.
    pub source: CandidateSource,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CandidateSource {
    /// Direct parse — input was valid JSON.
    Direct,
    /// Extracted from a ```json code block.
    MarkdownBlock,
    /// Grepped `{...}` or `[...]` from text.
    Grepped,
    /// Fixed broken JSON (closed brackets, stripped trailing commas, etc).
    Fixed,
}

/// Result of a flexible parse attempt.
#[derive(Debug)]
pub struct ParseResult<T> {
    /// Successfully parsed value.
    pub value: T,
    /// Which candidate succeeded.
    pub source: CandidateSource,
    /// Total candidates tried.
    pub candidates_tried: usize,
}

/// Parse error with all attempted candidates.
#[derive(Debug)]
pub struct ParseError {
    /// All candidates that were tried.
    pub candidates: Vec<(Candidate, String)>,
    /// Original raw text.
    pub raw: String,
}

impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Failed to parse into target type. {} candidates tried",
            self.candidates.len()
        )?;
        for (i, (candidate, err)) in self.candidates.iter().enumerate() {
            write!(
                f,
                "\n  [{i}] {:?}: {}",
                candidate.source,
                truncate(err, 100)
            )?;
        }
        Ok(())
    }
}

impl std::error::Error for ParseError {}

/// Parse raw LLM output into type `T` using the AnyOf cascade.
///
/// Tries multiple extraction strategies, returns the first successful parse.
pub fn parse_flexible<T: DeserializeOwned>(raw: &str) -> Result<ParseResult<T>, ParseError> {
    let candidates = collect_candidates(raw);
    let mut errors = Vec::new();

    for candidate in &candidates {
        match serde_json::from_str::<T>(&candidate.json) {
            Ok(value) => {
                return Ok(ParseResult {
                    value,
                    source: candidate.source,
                    candidates_tried: errors.len() + 1,
                });
            }
            Err(e) => {
                errors.push((candidate.clone(), e.to_string()));
            }
        }
    }

    Err(ParseError {
        candidates: errors,
        raw: raw.to_string(),
    })
}

/// Parse with schema-aware coercion: "42" → 42, "true" → true, "redd" → "Red".
///
/// First tries `parse_flexible` (strict serde). If all candidates fail,
/// retries each candidate with coercion applied before deserialization.
pub fn parse_flexible_coerced<T: JsonSchema + DeserializeOwned>(
    raw: &str,
) -> Result<ParseResult<T>, ParseError> {
    // Try strict first — no coercion overhead if JSON is clean
    if let Ok(result) = parse_flexible::<T>(raw) {
        return Ok(result);
    }

    // Retry with coercion
    let candidates = collect_candidates(raw);
    let schema = response_schema_for::<T>();
    let mut errors = Vec::new();

    for candidate in &candidates {
        // Parse to Value, coerce, then deserialize
        if let Ok(mut value) = serde_json::from_str::<serde_json::Value>(&candidate.json) {
            coerce_value(&mut value, &schema);
            match serde_json::from_value::<T>(value) {
                Ok(parsed) => {
                    return Ok(ParseResult {
                        value: parsed,
                        source: candidate.source,
                        candidates_tried: errors.len() + 1,
                    });
                }
                Err(e) => {
                    errors.push((candidate.clone(), format!("coerced: {}", e)));
                }
            }
        } else {
            errors.push((candidate.clone(), "invalid JSON even for Value".into()));
        }
    }

    Err(ParseError {
        candidates: errors,
        raw: raw.to_string(),
    })
}

/// Collect all parse candidates from raw text (AnyOf pattern).
pub fn collect_candidates(raw: &str) -> Vec<Candidate> {
    let mut candidates = Vec::new();

    // 0. Unescape double-wrapped JSON string: "{ \"key\": ... }" → { "key": ... }
    let effective = try_unescape_json_string(raw).unwrap_or_else(|| raw.to_string());
    let raw = effective.as_str();

    // 1. Direct JSON parse
    if looks_like_json(raw) {
        candidates.push(Candidate {
            json: raw.to_string(),
            source: CandidateSource::Direct,
        });
    }

    // 2. Markdown code blocks
    for block in extract_markdown_blocks(raw) {
        candidates.push(Candidate {
            json: block,
            source: CandidateSource::MarkdownBlock,
        });
    }

    // 3. Greedy JSON extraction
    for json in extract_json_objects(raw) {
        // Skip if we already have this exact string as a candidate
        if !candidates.iter().any(|c| c.json == json) {
            candidates.push(Candidate {
                json,
                source: CandidateSource::Grepped,
            });
        }
    }

    // 4. Try fixing each candidate that failed
    let fixable: Vec<String> = candidates.iter().map(|c| c.json.clone()).collect();
    for json in &fixable {
        if let Some(fixed) = try_fix_json(json)
            && !candidates.iter().any(|c| c.json == fixed)
        {
            candidates.push(Candidate {
                json: fixed,
                source: CandidateSource::Fixed,
            });
        }
    }

    // Also try fixing the raw input directly if no candidates yet
    if (candidates.is_empty()
        || !candidates
            .iter()
            .any(|c| c.source == CandidateSource::Direct))
        && let Some(fixed) = try_fix_json(raw)
        && !candidates.iter().any(|c| c.json == fixed)
    {
        candidates.push(Candidate {
            json: fixed,
            source: CandidateSource::Fixed,
        });
    }

    // 5. Truncation recovery — try progressively aggressive cuts for streaming
    // (only if no Fixed candidate parsed as valid Value with all required fields)
    for json_source in [raw]
        .iter()
        .chain(fixable.iter().map(|s| s as &str).collect::<Vec<_>>().iter())
    {
        for recovered in truncation_recovery_candidates(json_source) {
            if !candidates.iter().any(|c| c.json == recovered) {
                candidates.push(Candidate {
                    json: recovered,
                    source: CandidateSource::Fixed,
                });
            }
        }
    }

    candidates
}

// ============================================================================
// Extraction strategies
// ============================================================================

/// Extract JSON from markdown code blocks: ```json\n...\n``` or ```\n...\n```
fn extract_markdown_blocks(text: &str) -> Vec<String> {
    let mut blocks = Vec::new();
    let mut rest = text;

    while let Some(start) = rest.find("```") {
        let after_ticks = &rest[start + 3..];

        // Skip optional language tag (e.g., "json", "JSON", "jsonc")
        let content_start = if let Some(newline) = after_ticks.find('\n') {
            newline + 1
        } else {
            break;
        };
        let content = &after_ticks[content_start..];

        // Find closing ```
        if let Some(end) = content.find("```") {
            let block = content[..end].trim();
            if !block.is_empty() && looks_like_json(block) {
                blocks.push(block.to_string());
            }
            rest = &content[end + 3..];
        } else {
            // Unclosed code block — try to parse what we have
            let block = content.trim();
            if !block.is_empty() && looks_like_json(block) {
                blocks.push(block.to_string());
            }
            break;
        }
    }

    blocks
}

/// Find JSON objects `{...}` and arrays `[...]` in text using bracket matching.
fn extract_json_objects(text: &str) -> Vec<String> {
    let mut results = Vec::new();

    for open in ['{', '['] {
        let close = if open == '{' { '}' } else { ']' };
        let mut search_from = 0;

        while let Some(start) = text[search_from..].find(open) {
            let abs_start = search_from + start;
            if let Some(end) = find_matching_bracket(text, abs_start, open, close) {
                let json = &text[abs_start..=end];
                if !results.contains(&json.to_string()) {
                    results.push(json.to_string());
                }
                search_from = end + 1;
            } else {
                // No matching bracket — try with auto-close
                search_from = abs_start + 1;
            }
        }
    }

    results
}

/// Find the matching closing bracket, respecting nesting and strings.
fn find_matching_bracket(text: &str, start: usize, open: char, close: char) -> Option<usize> {
    let bytes = text.as_bytes();
    let mut depth = 0i32;
    let mut in_string = false;
    let mut escape_next = false;
    let mut i = start;

    while i < bytes.len() {
        let ch = bytes[i] as char;

        if escape_next {
            escape_next = false;
            i += 1;
            continue;
        }

        if ch == '\\' && in_string {
            escape_next = true;
            i += 1;
            continue;
        }

        if ch == '"' {
            in_string = !in_string;
            i += 1;
            continue;
        }

        if !in_string {
            if ch == open {
                depth += 1;
            } else if ch == close {
                depth -= 1;
                if depth == 0 {
                    return Some(i);
                }
            }
        }

        i += 1;
    }

    None
}

// ============================================================================
// JSON fixing
// ============================================================================

/// Try to fix common JSON errors. Returns None if unfixable.
fn try_fix_json(raw: &str) -> Option<String> {
    let trimmed = raw.trim();

    // Already valid? No fix needed.
    if serde_json::from_str::<serde_json::Value>(trimmed).is_ok() {
        return None;
    }

    let mut fixed = trimmed.to_string();
    let mut changed = false;

    // Fix 1: Strip trailing commas before } or ]
    let re_trailing = strip_trailing_commas(&fixed);
    if re_trailing != fixed {
        fixed = re_trailing;
        changed = true;
    }

    // Fix 2: Close unclosed brackets/braces
    let closed = close_brackets(&fixed);
    if closed != fixed {
        fixed = closed;
        changed = true;
    }

    // Fix 3: Single quotes → double quotes (outside of double-quoted strings)
    let quoted = fix_single_quotes(&fixed);
    if quoted != fixed {
        fixed = quoted;
        changed = true;
    }

    // Fix 4: Strip JS-style comments (// and /* */)
    let uncommented = strip_comments(&fixed);
    if uncommented != fixed {
        fixed = uncommented;
        changed = true;
    }

    // Verify the fix actually produces valid JSON
    if changed && serde_json::from_str::<serde_json::Value>(&fixed).is_ok() {
        Some(fixed)
    } else {
        None
    }
}

/// Strip trailing commas: `{a: 1,}` → `{a: 1}`
fn strip_trailing_commas(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let chars: Vec<char> = s.chars().collect();
    let mut i = 0;

    while i < chars.len() {
        if chars[i] == '"' {
            // Skip strings
            result.push(chars[i]);
            i += 1;
            while i < chars.len() {
                result.push(chars[i]);
                if chars[i] == '\\' && i + 1 < chars.len() {
                    i += 1;
                    result.push(chars[i]);
                } else if chars[i] == '"' {
                    break;
                }
                i += 1;
            }
            i += 1;
            continue;
        }

        if chars[i] == ',' {
            // Look ahead for ] or } (skipping whitespace)
            let mut j = i + 1;
            while j < chars.len() && chars[j].is_whitespace() {
                j += 1;
            }
            if j < chars.len() && (chars[j] == '}' || chars[j] == ']') {
                // Skip the trailing comma
                i += 1;
                continue;
            }
        }

        result.push(chars[i]);
        i += 1;
    }

    result
}

/// Close unclosed brackets: `{"a": [1, 2` → `{"a": [1, 2]}`
///
/// Also handles streaming truncation: if truncated mid-value inside an array/object,
/// drops the incomplete element and closes brackets (like BAML's partial parse).
fn close_brackets(s: &str) -> String {
    let mut stack = Vec::new();
    let mut in_string = false;
    let mut escape_next = false;

    for ch in s.chars() {
        if escape_next {
            escape_next = false;
            continue;
        }
        if ch == '\\' && in_string {
            escape_next = true;
            continue;
        }
        if ch == '"' {
            in_string = !in_string;
            continue;
        }
        if !in_string {
            match ch {
                '{' => stack.push('}'),
                '[' => stack.push(']'),
                '}' | ']' => {
                    stack.pop();
                }
                _ => {}
            }
        }
    }

    // If not truncated (balanced), nothing to do
    if stack.is_empty() && !in_string {
        return s.to_string();
    }

    // Close unclosed string
    let mut result = s.to_string();
    if in_string {
        result.push('"');
    }

    // Close brackets in reverse order
    while let Some(close) = stack.pop() {
        result.push(close);
    }

    result
}

/// Truncation recovery: find cut points and generate multiple candidates.
///
/// For `{"a":[{"b":1},{"c":2,"d` generates:
/// - Cut at inner comma: `{"a":[{"b":1},{"c":2}]}` (partial element)
/// - Cut at outer comma: `{"a":[{"b":1}]}` (drop incomplete element)
///
/// Returns all valid JSON candidates, most aggressive cut last (so AnyOf tries
/// the most complete version first).
fn truncation_recovery_candidates(s: &str) -> Vec<String> {
    // Collect all cut points: commas and closing brackets (outside strings)
    // Use byte positions (not char indices) for correct slicing with Unicode
    let mut cut_points = Vec::new();
    let mut in_string = false;
    let mut escape_next = false;

    for (byte_pos, ch) in s.char_indices() {
        if escape_next {
            escape_next = false;
            continue;
        }
        if ch == '\\' && in_string {
            escape_next = true;
            continue;
        }
        if ch == '"' {
            in_string = !in_string;
            continue;
        }
        if in_string {
            continue;
        }
        match ch {
            ',' => cut_points.push(byte_pos),
            '}' | ']' => cut_points.push(byte_pos + 1),
            _ => {}
        }
    }

    // Try cuts from rightmost (most data kept) to leftmost (most data dropped)
    let mut results = Vec::new();
    for &cut in cut_points.iter().rev() {
        if cut == 0 || cut >= s.len() {
            continue;
        }
        if let Some(candidate) = try_close_at(s, cut)
            && !results.contains(&candidate)
        {
            results.push(candidate);
        }
    }

    results
}

/// Try cutting the string at `pos` and closing all open brackets.
fn try_close_at(s: &str, pos: usize) -> Option<String> {
    let mut truncated = s[..pos].trim_end().to_string();

    // Strip trailing comma
    if truncated.ends_with(',') {
        truncated.pop();
    }

    // Close open brackets
    let mut stack = Vec::new();
    let mut in_str = false;
    let mut esc = false;
    for ch in truncated.chars() {
        if esc {
            esc = false;
            continue;
        }
        if ch == '\\' && in_str {
            esc = true;
            continue;
        }
        if ch == '"' {
            in_str = !in_str;
            continue;
        }
        if !in_str {
            match ch {
                '{' => stack.push('}'),
                '[' => stack.push(']'),
                '}' | ']' => {
                    stack.pop();
                }
                _ => {}
            }
        }
    }
    if in_str {
        truncated.push('"');
    }
    while let Some(close) = stack.pop() {
        truncated.push(close);
    }

    if serde_json::from_str::<serde_json::Value>(&truncated).is_ok() {
        Some(truncated)
    } else {
        None
    }
}

/// Convert single-quoted strings to double-quoted (outside existing double quotes).
fn fix_single_quotes(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut in_double = false;
    let mut escape_next = false;

    for ch in s.chars() {
        if escape_next {
            result.push(ch);
            escape_next = false;
            continue;
        }
        if ch == '\\' {
            result.push(ch);
            if in_double {
                escape_next = true;
            }
            continue;
        }
        if ch == '"' {
            in_double = !in_double;
            result.push(ch);
            continue;
        }
        if ch == '\'' && !in_double {
            result.push('"');
        } else {
            result.push(ch);
        }
    }

    result
}

/// Strip JS-style comments (// line and /* block */).
fn strip_comments(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let chars: Vec<char> = s.chars().collect();
    let mut i = 0;
    let mut in_string = false;

    while i < chars.len() {
        if in_string {
            result.push(chars[i]);
            if chars[i] == '\\' && i + 1 < chars.len() {
                i += 1;
                result.push(chars[i]);
            } else if chars[i] == '"' {
                in_string = false;
            }
            i += 1;
            continue;
        }

        if chars[i] == '"' {
            in_string = true;
            result.push(chars[i]);
            i += 1;
            continue;
        }

        if i + 1 < chars.len() && chars[i] == '/' && chars[i + 1] == '/' {
            // Skip to end of line
            while i < chars.len() && chars[i] != '\n' {
                i += 1;
            }
            continue;
        }

        if i + 1 < chars.len() && chars[i] == '/' && chars[i + 1] == '*' {
            i += 2;
            while i + 1 < chars.len() && !(chars[i] == '*' && chars[i + 1] == '/') {
                i += 1;
            }
            i += 2; // skip */
            continue;
        }

        result.push(chars[i]);
        i += 1;
    }

    result
}

// ============================================================================
// Helpers
// ============================================================================

/// Try to unescape a double-wrapped JSON string.
///
/// Some models output JSON as a string literal: `"{ \"key\": \"value\" }"`
/// This detects and unescapes it back to `{ "key": "value" }`.
fn try_unescape_json_string(raw: &str) -> Option<String> {
    let trimmed = raw.trim();
    // Must start and end with quotes
    if !trimmed.starts_with('"') || !trimmed.ends_with('"') || trimmed.len() < 3 {
        return None;
    }
    // Inner content must look like escaped JSON (contains \")
    let inner = &trimmed[1..trimmed.len() - 1];
    if !inner.contains("\\\"") {
        return None;
    }
    // Try to parse as a JSON string, which gives us the unescaped content
    match serde_json::from_str::<String>(trimmed) {
        Ok(unescaped) if looks_like_json(&unescaped) => Some(unescaped),
        _ => None,
    }
}

fn looks_like_json(s: &str) -> bool {
    let trimmed = s.trim();
    (trimmed.starts_with('{') && trimmed.ends_with('}'))
        || (trimmed.starts_with('[') && trimmed.ends_with(']'))
        || trimmed == "null"
        || trimmed == "true"
        || trimmed == "false"
        || trimmed.starts_with('"')
}

fn truncate(s: &str, max: usize) -> &str {
    if s.len() <= max {
        s
    } else {
        &s[..s.floor_char_boundary(max)]
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    #[derive(Debug, Deserialize, PartialEq)]
    struct Answer {
        answer: String,
        confidence: f64,
    }

    // --- Direct JSON ---

    #[test]
    fn parses_clean_json() {
        let raw = r#"{"answer": "42", "confidence": 0.95}"#;
        let result = parse_flexible::<Answer>(raw).unwrap();
        assert_eq!(result.value.answer, "42");
        assert_eq!(result.source, CandidateSource::Direct);
    }

    // --- Markdown blocks ---

    #[test]
    fn parses_from_markdown_block() {
        let raw = r#"Here's my answer:

```json
{"answer": "hello", "confidence": 0.8}
```

Hope that helps!"#;
        let result = parse_flexible::<Answer>(raw).unwrap();
        assert_eq!(result.value.answer, "hello");
        assert_eq!(result.source, CandidateSource::MarkdownBlock);
    }

    #[test]
    fn parses_from_unlabeled_markdown_block() {
        let raw = r#"Sure:

```
{"answer": "test", "confidence": 0.5}
```"#;
        let result = parse_flexible::<Answer>(raw).unwrap();
        assert_eq!(result.value.answer, "test");
        assert_eq!(result.source, CandidateSource::MarkdownBlock);
    }

    // --- Grepped JSON ---

    #[test]
    fn extracts_json_from_surrounding_text() {
        let raw =
            r#"I think the answer is {"answer": "yes", "confidence": 0.9} based on my analysis."#;
        let result = parse_flexible::<Answer>(raw).unwrap();
        assert_eq!(result.value.answer, "yes");
        assert_eq!(result.source, CandidateSource::Grepped);
    }

    #[test]
    fn extracts_json_after_chain_of_thought() {
        let raw = r#"Let me think step by step...
First, I need to consider the question carefully.
The answer seems clear.

{"answer": "deep thought", "confidence": 0.99}"#;
        let result = parse_flexible::<Answer>(raw).unwrap();
        assert_eq!(result.value.answer, "deep thought");
    }

    // --- Fixed JSON ---

    #[test]
    fn fixes_trailing_comma() {
        let raw = r#"{"answer": "fixed", "confidence": 0.7,}"#;
        let result = parse_flexible::<Answer>(raw).unwrap();
        assert_eq!(result.value.answer, "fixed");
        assert_eq!(result.source, CandidateSource::Fixed);
    }

    #[test]
    fn fixes_unclosed_brackets() {
        let raw = r#"{"answer": "partial", "confidence": 0.6"#;
        let result = parse_flexible::<Answer>(raw).unwrap();
        assert_eq!(result.value.answer, "partial");
        assert_eq!(result.source, CandidateSource::Fixed);
    }

    #[test]
    fn fixes_single_quotes() {
        let raw = r#"{'answer': 'quoted', 'confidence': 0.5}"#;
        let result = parse_flexible::<Answer>(raw).unwrap();
        assert_eq!(result.value.answer, "quoted");
        assert_eq!(result.source, CandidateSource::Fixed);
    }

    #[test]
    fn fixes_js_comments() {
        let raw = r#"{
            // This is the answer
            "answer": "commented",
            "confidence": 0.4
        }"#;
        let result = parse_flexible::<Answer>(raw).unwrap();
        assert_eq!(result.value.answer, "commented");
        assert_eq!(result.source, CandidateSource::Fixed);
    }

    // --- Combined scenarios ---

    #[test]
    fn prefers_direct_over_markdown() {
        // If the whole input is valid JSON, use it directly
        let raw = r#"{"answer": "direct", "confidence": 1.0}"#;
        let result = parse_flexible::<Answer>(raw).unwrap();
        assert_eq!(result.source, CandidateSource::Direct);
    }

    #[test]
    fn handles_multiple_json_objects_picks_matching() {
        #[derive(Debug, Deserialize, PartialEq)]
        struct Config {
            model: String,
            temperature: f64,
        }

        let raw = r#"Here are two objects:
{"answer": "wrong type", "confidence": 0.5}
{"model": "gemini", "temperature": 0.3}"#;
        let result = parse_flexible::<Config>(raw).unwrap();
        assert_eq!(result.value.model, "gemini");
    }

    #[test]
    fn error_shows_all_candidates() {
        #[derive(Debug, Deserialize)]
        #[allow(dead_code)]
        struct Impossible {
            xyz_field_that_wont_match: i64,
        }

        let raw = "Just some plain text with no JSON";
        let err = parse_flexible::<Impossible>(raw).unwrap_err();
        assert!(err.to_string().contains("Failed to parse"));
    }

    // --- Edge cases ---

    #[test]
    fn handles_nested_json() {
        #[derive(Debug, Deserialize, PartialEq)]
        struct Nested {
            outer: Inner,
        }
        #[derive(Debug, Deserialize, PartialEq)]
        struct Inner {
            value: String,
        }

        let raw = r#"{"outer": {"value": "deep"}}"#;
        let result = parse_flexible::<Nested>(raw).unwrap();
        assert_eq!(result.value.outer.value, "deep");
    }

    #[test]
    fn handles_array_response() {
        let raw = r#"```json
[{"answer": "one", "confidence": 0.5}, {"answer": "two", "confidence": 0.8}]
```"#;
        let result = parse_flexible::<Vec<Answer>>(raw).unwrap();
        assert_eq!(result.value.len(), 2);
        assert_eq!(result.value[1].answer, "two");
    }

    #[test]
    fn handles_empty_input() {
        let err = parse_flexible::<Answer>("").unwrap_err();
        assert!(err.candidates.is_empty() || !err.candidates.is_empty());
    }

    #[test]
    fn handles_unclosed_markdown_block() {
        let raw = r#"```json
{"answer": "streaming", "confidence": 0.3}
"#;
        let result = parse_flexible::<Answer>(raw).unwrap();
        assert_eq!(result.value.answer, "streaming");
    }

    // --- Fixing strategies ---

    #[test]
    fn strip_trailing_commas_works() {
        assert_eq!(strip_trailing_commas(r#"{"a": 1,}"#), r#"{"a": 1}"#);
        assert_eq!(strip_trailing_commas(r#"[1, 2,]"#), r#"[1, 2]"#);
        // Don't strip inside strings
        assert_eq!(strip_trailing_commas(r#"{"a": "b,"}"#), r#"{"a": "b,"}"#);
    }

    #[test]
    fn close_brackets_works() {
        assert_eq!(close_brackets(r#"{"a": 1"#), r#"{"a": 1}"#);
        assert_eq!(close_brackets(r#"[1, [2"#), r#"[1, [2]]"#);
        assert_eq!(close_brackets(r#"{"a": "hello"#), r#"{"a": "hello"}"#);
    }

    #[test]
    fn truncation_recovery_drops_incomplete_element() {
        // Truncated mid-field in an array element — recovery should produce candidates
        let raw = r#"{"items":[{"id":1,"name":"ok"},{"id":2,"na"#;
        let candidates = truncation_recovery_candidates(raw);
        assert!(!candidates.is_empty(), "Should produce recovery candidates");
        // At least one candidate should have the first complete element
        let has_valid = candidates.iter().any(|c| {
            if let Ok(val) = serde_json::from_str::<serde_json::Value>(c) {
                val["items"]
                    .as_array()
                    .is_some_and(|a| !a.is_empty() && a[0]["id"] == 1)
            } else {
                false
            }
        });
        assert!(
            has_valid,
            "At least one candidate should have first complete element"
        );
    }

    #[test]
    fn truncation_recovery_streaming_action() {
        // Real-world case: truncated mid-action in NextStep
        #[derive(Debug, Deserialize)]
        struct Step {
            situation: String,
            actions: Vec<serde_json::Value>,
        }
        let raw = r#"{"situation":"working","actions":[{"tool":"read","path":"a.rs"},{"tool":"edit","path":"b.rs","old"#;
        let result = parse_flexible::<Step>(raw);
        assert!(result.is_ok(), "Should recover from truncated streaming");
        let step = result.unwrap().value;
        assert_eq!(step.situation, "working");
        // First complete action should survive, truncated second dropped
        assert!(!step.actions.is_empty());
    }

    #[test]
    fn unescape_double_wrapped_json() {
        #[derive(Debug, Deserialize)]
        struct Simple {
            msg: String,
        }

        let raw = r#""{\"msg\": \"hello world\"}""#;
        let result = parse_flexible::<Simple>(raw);
        assert!(result.is_ok(), "Should unescape double-wrapped JSON");
        assert_eq!(result.unwrap().value.msg, "hello world");
    }

    #[test]
    fn unescape_ignores_normal_strings() {
        // Normal quoted string that is NOT escaped JSON — should NOT be unescaped
        let result = try_unescape_json_string("\"just a normal string\"");
        assert!(result.is_none());
    }

    #[test]
    fn fix_single_quotes_works() {
        assert_eq!(fix_single_quotes("{'a': 'b'}"), r#"{"a": "b"}"#);
        // Don't touch singles inside double quotes
        assert_eq!(
            fix_single_quotes(r#"{"it's": "fine"}"#),
            r#"{"it's": "fine"}"#
        );
    }

    #[test]
    fn strip_comments_works() {
        assert_eq!(
            strip_comments("{\n// comment\n\"a\": 1\n}"),
            "{\n\n\"a\": 1\n}"
        );
        assert_eq!(strip_comments("{/* block */\"a\": 1}"), "{\"a\": 1}");
    }

    #[test]
    fn extract_markdown_blocks_multiple() {
        let raw = r#"First:
```json
{"a": 1}
```
Second:
```json
{"b": 2}
```"#;
        let blocks = extract_markdown_blocks(raw);
        assert_eq!(blocks.len(), 2);
    }

    #[test]
    fn extract_json_objects_finds_multiple() {
        let raw = r#"text {"a": 1} middle {"b": 2} end"#;
        let objects = extract_json_objects(raw);
        assert_eq!(objects.len(), 2);
    }

    #[test]
    fn extract_json_objects_nested_returns_outer() {
        let raw = r#"text {"outer": {"inner": 1}} more text"#;
        let objects = extract_json_objects(raw);
        // Outer matched first; inner is inside matched range so skipped
        assert_eq!(objects.len(), 1);
        assert!(objects[0].contains("outer"));
    }

    #[test]
    fn collect_candidates_deduplicates() {
        let raw = r#"{"answer": "test", "confidence": 0.5}"#;
        let candidates = collect_candidates(raw);
        // Direct + Grepped should be deduped
        let jsons: Vec<&str> = candidates.iter().map(|c| c.json.as_str()).collect();
        let unique: std::collections::HashSet<&&str> = jsons.iter().collect();
        assert_eq!(jsons.len(), unique.len());
    }
}