semver-analyzer-llm 0.0.4

LLM-based behavioral analysis for the semver-analyzer
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
//! LLM command invocation and response parsing.
//!
//! Handles running external LLM commands and extracting structured JSON
//! from their free-text output. Uses multiple strategies for JSON extraction
//! to handle different LLM output formats.

use anyhow::{Context, Result};
use regex::Regex;
use semver_analyzer_core::{BreakingVerdict, ExpectedChild, FunctionSpec, RemovalDisposition};
use serde::Deserialize;
use std::process::Command;
use std::sync::LazyLock;

/// Run an LLM command with the given prompt and return the output.
///
/// The command string is split on whitespace and the prompt is appended
/// as the final argument. The command is expected to return a response
/// on stdout.
///
/// Examples:
/// - `"goose run --no-session -q -t"` → `goose run --no-session -q -t "<prompt>"`
/// - `"opencode run"` → `opencode run "<prompt>"`
pub fn run_llm_command(command: &str, prompt: &str, timeout_secs: u64) -> Result<String> {
    let parts: Vec<&str> = command.split_whitespace().collect();
    if parts.is_empty() {
        anyhow::bail!("Empty LLM command");
    }

    let program = parts[0];
    let args = &parts[1..];

    let mut child = Command::new(program)
        .args(args)
        .arg(prompt)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .with_context(|| format!("Failed to execute LLM command: {}", command))?;

    // Wait with timeout
    let timeout = std::time::Duration::from_secs(timeout_secs);
    let start = std::time::Instant::now();

    loop {
        match child.try_wait() {
            Ok(Some(status)) => {
                // Process finished
                let output = child.wait_with_output()?;
                if !status.success() {
                    let stderr = String::from_utf8_lossy(&output.stderr);
                    anyhow::bail!(
                        "LLM command failed (exit code {:?}): {}",
                        status.code(),
                        stderr
                    );
                }
                let stdout = String::from_utf8_lossy(&output.stdout).to_string();
                if stdout.trim().is_empty() {
                    anyhow::bail!("LLM command returned empty output");
                }
                // Goose truncates large stdout and writes the full output
                // to a temp file, e.g.:
                //   ... (57 more lines → /tmp/goose-xxx.txt)
                // Detect this and read the temp file to get the full response.
                return Ok(resolve_goose_overflow(&stdout));
            }
            Ok(None) => {
                // Still running
                if start.elapsed() > timeout {
                    let _ = child.kill();
                    anyhow::bail!("LLM command timed out after {} seconds", timeout_secs);
                }
                std::thread::sleep(std::time::Duration::from_millis(100));
            }
            Err(e) => {
                anyhow::bail!("Error waiting for LLM command: {}", e);
            }
        }
    }
}

/// Goose CLI truncates large outputs to stdout and saves the full text to a
/// temp file. The truncation marker looks like:
///   `... (57 more lines → /var/folders/.../goose-XxYz.txt)`
/// This function detects the marker, reads the overflow file, and reconstructs
/// the full response by replacing the truncation line with the file contents.
fn resolve_goose_overflow(stdout: &str) -> String {
    // Pattern: "... (N more lines → <path>)"
    // The marker is always on a line by itself, near the end of stdout.
    static OVERFLOW_RE: LazyLock<Regex> =
        LazyLock::new(|| Regex::new(r"(?m)^\.\.\. \(\d+ more lines → (.+)\)\s*$").unwrap());

    let Some(caps) = OVERFLOW_RE.captures(stdout) else {
        return stdout.to_string();
    };

    let overflow_path = caps.get(1).unwrap().as_str();
    let overflow_content = match std::fs::read_to_string(overflow_path) {
        Ok(content) => content,
        Err(e) => {
            tracing::warn!(
                path = overflow_path,
                %e,
                "failed to read goose overflow file, using truncated stdout"
            );
            return stdout.to_string();
        }
    };

    // The overflow file contains the FULL response (without code fences).
    // Use it directly since it's complete.
    tracing::debug!(
        path = overflow_path,
        overflow_lines = overflow_content.lines().count(),
        "resolved goose overflow file"
    );
    overflow_content
}

/// Parse a `FunctionSpec` from LLM output.
///
/// Tries multiple strategies:
/// 1. Fenced JSON block (```json ... ```)
/// 2. Raw JSON object ({ ... })
/// 3. JSON embedded in prose text
pub fn parse_function_spec(response: &str) -> Result<FunctionSpec> {
    let json_str = extract_json(response).context("Could not extract JSON from LLM response")?;

    serde_json::from_str(&json_str).with_context(|| {
        format!(
            "Failed to parse FunctionSpec from JSON. Extracted:\n{}",
            truncate(&json_str, 500)
        )
    })
}

/// Parse a `BreakingVerdict` from LLM output.
pub fn parse_breaking_verdict(response: &str) -> Result<BreakingVerdict> {
    let json_str =
        extract_json(response).context("Could not extract JSON from LLM response for verdict")?;

    serde_json::from_str(&json_str).with_context(|| {
        format!(
            "Failed to parse BreakingVerdict from JSON. Extracted:\n{}",
            truncate(&json_str, 500)
        )
    })
}

/// Parse a boolean propagation result from LLM output.
///
/// Looks for clear yes/no signals. Defaults to `true` (conservative:
/// assume propagation) if the response is ambiguous.
pub fn parse_propagation_result(response: &str) -> Result<bool> {
    let lower = response.to_lowercase();

    // Try to parse as JSON first
    if let Some(json_str) = extract_json(response) {
        if let Ok(val) = serde_json::from_str::<serde_json::Value>(&json_str) {
            if let Some(propagates) = val.get("propagates").and_then(|v| v.as_bool()) {
                return Ok(propagates);
            }
            if let Some(propagates) = val.get("is_breaking").and_then(|v| v.as_bool()) {
                return Ok(propagates);
            }
        }
    }

    // Heuristic text matching
    if lower.contains("does not propagate")
        || lower.contains("does not affect")
        || lower.contains("absorbs the change")
        || lower.contains("masks the change")
        || lower.contains("no propagation")
    {
        return Ok(false);
    }

    if lower.contains("propagates")
        || lower.contains("is affected")
        || lower.contains("breaks the caller")
        || lower.contains("yes, the change propagates")
    {
        return Ok(true);
    }

    // Conservative default: assume propagation
    Ok(true)
}

// ── File-level behavioral change parsing ────────────────────────────────

/// A single behavioral change from the file-level LLM response.
#[derive(Debug, Clone, Deserialize)]
pub struct FileBehavioralChange {
    pub symbol: String,
    #[serde(default = "default_kind")]
    pub kind: String,
    /// Sub-category of the behavioral change (language-specific, injected
    /// via `LlmCategoryDefinition` in the prompt).
    #[serde(default)]
    pub category: Option<String>,
    pub description: String,
    /// Whether this change only affects internal rendering and does NOT
    /// require consumer code changes.
    #[serde(default)]
    pub is_internal_only: Option<bool>,
}

fn default_kind() -> String {
    "class".to_string()
}

/// A single API type-level change from the file-level LLM response.
#[derive(Debug, Clone, Deserialize)]
pub struct FileApiChange {
    pub symbol: String,
    #[serde(default = "default_change")]
    pub change: String,
    pub description: String,
    /// Why a removed prop was removed and where its functionality went.
    #[serde(default)]
    pub removal_disposition: Option<RemovalDisposition>,
}

fn default_change() -> String {
    "signature_changed".to_string()
}

/// Parsed response from the file-level analysis prompt.
#[derive(Debug, Clone, Deserialize)]
pub struct FileBehavioralResponse {
    #[serde(default)]
    pub breaking_behavioral_changes: Vec<FileBehavioralChange>,
    #[serde(default)]
    pub breaking_api_changes: Vec<FileApiChange>,
}

/// Parse file-level changes (behavioral + API) from LLM output.
pub fn parse_file_behavioral_response(
    response: &str,
) -> Result<(Vec<FileBehavioralChange>, Vec<FileApiChange>)> {
    let json_str = extract_json(response)
        .context("Could not extract JSON from LLM response for file analysis")?;

    let parsed: FileBehavioralResponse = serde_json::from_str(&json_str).with_context(|| {
        format!(
            "Failed to parse FileBehavioralResponse from JSON. Extracted:\n{}",
            truncate(&json_str, 500)
        )
    })?;

    Ok((
        parsed.breaking_behavioral_changes,
        parsed.breaking_api_changes,
    ))
}

// ── JSON Extraction ─────────────────────────────────────────────────────

/// Regex for fenced JSON blocks.
static FENCED_JSON_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"```(?:json)?\s*\n([\s\S]*?)\n```").unwrap());

/// Regex for standalone JSON objects.
static JSON_OBJECT_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\{[\s\S]*\}").unwrap());

/// Extract JSON from LLM output using multiple strategies.
///
/// Strategy order (first match wins):
/// 1. Fenced JSON block: ```json\n{...}\n```
/// 2. Last fenced block (if multiple)
/// 3. Largest `{...}` substring
fn extract_json(text: &str) -> Option<String> {
    // Strategy 1: Fenced JSON block
    let fenced_matches: Vec<_> = FENCED_JSON_RE
        .captures_iter(text)
        .filter_map(|cap| cap.get(1).map(|m| m.as_str().trim().to_string()))
        .collect();

    if let Some(last) = fenced_matches.last() {
        return Some(last.clone());
    }

    // Strategy 2: Find the largest JSON object in the text
    let mut best: Option<String> = None;
    let mut best_len = 0;

    for mat in JSON_OBJECT_RE.find_iter(text) {
        let candidate = mat.as_str();
        // Validate it's actually parseable JSON
        if serde_json::from_str::<serde_json::Value>(candidate).is_ok()
            && candidate.len() > best_len
        {
            best = Some(candidate.to_string());
            best_len = candidate.len();
        }
    }

    if best.is_some() {
        return best;
    }

    // Strategy 3: Try to find a JSON object by brace matching
    if let Some(start) = text.find('{') {
        let mut depth = 0;
        let mut in_string = false;
        let mut escape = false;

        for (i, ch) in text[start..].char_indices() {
            if escape {
                escape = false;
                continue;
            }
            match ch {
                '\\' if in_string => escape = true,
                '"' => in_string = !in_string,
                '{' if !in_string => depth += 1,
                '}' if !in_string => {
                    depth -= 1;
                    if depth == 0 {
                        let json_str = &text[start..start + i + 1];
                        if serde_json::from_str::<serde_json::Value>(json_str).is_ok() {
                            return Some(json_str.to_string());
                        }
                    }
                }
                _ => {}
            }
        }
    }

    None
}

/// Truncate a string for error messages.
fn truncate(s: &str, max_len: usize) -> &str {
    if s.len() <= max_len {
        s
    } else {
        &s[..max_len]
    }
}

// ── Rename inference response parsing ─────────────────────────────────

/// A single constant rename pattern from the LLM response.
#[derive(Debug, Clone, Deserialize)]
pub struct LlmConstantRenamePattern {
    #[serde(alias = "match")]
    pub match_regex: String,
    pub replace: String,
}

/// A single interface rename mapping from the LLM response.
#[derive(Debug, Clone, Deserialize)]
pub struct LlmInterfaceRenameMapping {
    pub old_name: String,
    pub new_name: String,
    #[serde(default = "default_confidence")]
    pub confidence: String,
    #[serde(default)]
    pub reason: String,
}

fn default_confidence() -> String {
    "medium".to_string()
}

/// Parse the LLM response for constant rename pattern inference.
/// Returns a list of regex match/replace patterns.
pub fn parse_constant_rename_response(response: &str) -> Result<Vec<LlmConstantRenamePattern>> {
    let json_str = extract_json(response)
        .with_context(|| "No JSON found in constant rename inference response")?;
    let patterns: Vec<LlmConstantRenamePattern> =
        serde_json::from_str(&json_str).with_context(|| {
            format!(
                "Failed to parse constant rename patterns: {}",
                truncate(&json_str, 200)
            )
        })?;
    Ok(patterns)
}

/// Parse the LLM response for interface rename mapping inference.
/// Returns a list of old_name → new_name mappings.
pub fn parse_interface_rename_response(response: &str) -> Result<Vec<LlmInterfaceRenameMapping>> {
    let json_str = extract_json(response)
        .with_context(|| "No JSON found in interface rename inference response")?;
    let mappings: Vec<LlmInterfaceRenameMapping> =
        serde_json::from_str(&json_str).with_context(|| {
            format!(
                "Failed to parse interface rename mappings: {}",
                truncate(&json_str, 200)
            )
        })?;
    Ok(mappings)
}

// ── Hierarchy inference response parsing ─────────────────────────────

/// The top-level LLM hierarchy response.
#[derive(Debug, Clone, Deserialize)]
pub struct LlmHierarchyResponse {
    pub components: std::collections::HashMap<String, LlmComponentHierarchy>,
}

/// A single component's hierarchy entry from the LLM.
#[derive(Debug, Clone, Deserialize)]
pub struct LlmComponentHierarchy {
    #[serde(default)]
    pub expected_children: Vec<ExpectedChild>,
}

// ── CSS Suffix Rename Response Parsing ───────────────────────────────────

/// A single suffix rename from the LLM response.
#[derive(Debug, Clone, Deserialize)]
pub struct LlmSuffixRename {
    pub from: String,
    pub to: String,
}

/// The top-level LLM suffix rename response.
#[derive(Debug, Clone, Deserialize)]
struct LlmSuffixRenameResponse {
    pub renames: Vec<LlmSuffixRename>,
}

/// Parse the LLM response for CSS suffix rename inference.
/// Returns a list of (old_suffix, new_suffix) pairs.
pub fn parse_suffix_rename_response(response: &str) -> Result<Vec<LlmSuffixRename>> {
    let json_str = extract_json(response)
        .with_context(|| "No JSON found in suffix rename inference response")?;
    let parsed: LlmSuffixRenameResponse = serde_json::from_str(&json_str).with_context(|| {
        format!(
            "Failed to parse suffix rename response: {}",
            truncate(&json_str, 300)
        )
    })?;
    Ok(parsed.renames)
}

/// Parse the LLM response for hierarchy inference.
/// Returns a map of component name → expected children.
pub fn parse_hierarchy_response(
    response: &str,
) -> Result<std::collections::HashMap<String, Vec<ExpectedChild>>> {
    let json_str =
        extract_json(response).with_context(|| "No JSON found in hierarchy inference response")?;
    let parsed: LlmHierarchyResponse = serde_json::from_str(&json_str).with_context(|| {
        format!(
            "Failed to parse hierarchy response: {}",
            truncate(&json_str, 300)
        )
    })?;
    Ok(parsed
        .components
        .into_iter()
        .map(|(name, h)| (name, h.expected_children))
        .collect())
}

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

    // ── JSON extraction tests ───────────────────────────────────────

    #[test]
    fn extract_fenced_json() {
        let input = r#"Here is the spec:

```json
{
  "preconditions": [],
  "postconditions": [{"condition": "always", "returns": "42"}],
  "error_behavior": [],
  "side_effects": [],
  "notes": []
}
```

That's the spec."#;

        let json = extract_json(input).unwrap();
        let spec: FunctionSpec = serde_json::from_str(&json).unwrap();
        assert_eq!(spec.postconditions.len(), 1);
        assert_eq!(spec.postconditions[0].returns, "42");
    }

    #[test]
    fn extract_raw_json() {
        let input = r#"The function spec is: {"preconditions": [], "postconditions": [], "error_behavior": [], "side_effects": [], "notes": ["simple function"]}"#;

        let json = extract_json(input).unwrap();
        let spec: FunctionSpec = serde_json::from_str(&json).unwrap();
        assert_eq!(spec.notes.len(), 1);
    }

    #[test]
    fn extract_json_with_prose() {
        let input = r#"After analyzing the function, I found:

{
  "preconditions": [
    {"parameter": "email", "condition": "must be non-empty", "on_violation": "throws TypeError"}
  ],
  "postconditions": [],
  "error_behavior": [],
  "side_effects": [],
  "notes": []
}

The function validates email addresses."#;

        let json = extract_json(input).unwrap();
        let spec: FunctionSpec = serde_json::from_str(&json).unwrap();
        assert_eq!(spec.preconditions.len(), 1);
        assert_eq!(spec.preconditions[0].parameter, "email");
    }

    #[test]
    fn extract_json_prefers_fenced() {
        let input = r#"Small json: {"notes": ["wrong"]}

```json
{"preconditions": [], "postconditions": [], "error_behavior": [], "side_effects": [], "notes": ["correct"]}
```"#;

        let json = extract_json(input).unwrap();
        let spec: FunctionSpec = serde_json::from_str(&json).unwrap();
        assert_eq!(spec.notes, vec!["correct"]);
    }

    #[test]
    fn extract_json_returns_none_for_no_json() {
        assert!(extract_json("No JSON here at all").is_none());
        assert!(extract_json("").is_none());
    }

    // ── FunctionSpec parsing tests ──────────────────────────────────

    #[test]
    fn parse_spec_from_fenced_block() {
        let response = r#"```json
{
  "preconditions": [],
  "postconditions": [{"condition": "valid input", "returns": "processed string"}],
  "error_behavior": [{"trigger": "empty input", "error_type": "Error"}],
  "side_effects": [],
  "notes": []
}
```"#;

        let spec = parse_function_spec(response).unwrap();
        assert_eq!(spec.postconditions.len(), 1);
        assert_eq!(spec.error_behavior.len(), 1);
    }

    // ── Propagation result parsing ──────────────────────────────────

    #[test]
    fn parse_propagation_json() {
        let response = r#"{"propagates": false}"#;
        assert!(!parse_propagation_result(response).unwrap());

        let response = r#"{"propagates": true}"#;
        assert!(parse_propagation_result(response).unwrap());
    }

    #[test]
    fn parse_propagation_text() {
        assert!(!parse_propagation_result("The caller does not propagate the change").unwrap());
        assert!(!parse_propagation_result("It absorbs the change").unwrap());
        assert!(parse_propagation_result("The change propagates to the caller").unwrap());
    }

    #[test]
    fn parse_propagation_default_conservative() {
        // Ambiguous response defaults to true (conservative)
        assert!(parse_propagation_result("I'm not sure about this one").unwrap());
    }

    // ── BreakingVerdict parsing ─────────────────────────────────────

    // ── File behavioral response parsing ────────────────────────────

    #[test]
    fn parse_file_behavioral_empty() {
        let response = r#"```json
{"breaking_behavioral_changes": [], "breaking_api_changes": []}
```"#;
        let (beh, api) = parse_file_behavioral_response(response).unwrap();
        assert!(beh.is_empty());
        assert!(api.is_empty());
    }

    #[test]
    fn parse_file_behavioral_with_changes() {
        let response = r#"```json
{
  "breaking_behavioral_changes": [
    {
      "symbol": "Modal",
      "kind": "class",
      "description": "Component now renders a <section> instead of <div>"
    },
    {
      "symbol": "closeModal",
      "kind": "function",
      "description": "No longer emits 'beforeClose' event"
    }
  ],
  "breaking_api_changes": [
    {
      "symbol": "ModalProps.size",
      "change": "type_changed",
      "description": "Type narrowed from string to union"
    }
  ]
}
```"#;
        let (beh, api) = parse_file_behavioral_response(response).unwrap();
        assert_eq!(beh.len(), 2);
        assert_eq!(beh[0].symbol, "Modal");
        assert_eq!(beh[0].kind, "class");
        assert!(beh[0].description.contains("section"));
        assert_eq!(beh[1].symbol, "closeModal");
        assert_eq!(beh[1].kind, "function");
        assert_eq!(api.len(), 1);
        assert_eq!(api[0].symbol, "ModalProps.size");
        assert_eq!(api[0].change, "type_changed");
    }

    #[test]
    fn parse_file_behavioral_default_kind() {
        let response =
            r#"{"breaking_behavioral_changes": [{"symbol": "Foo", "description": "changed"}]}"#;
        let (beh, _api) = parse_file_behavioral_response(response).unwrap();
        assert_eq!(beh[0].kind, "class");
    }

    #[test]
    fn parse_file_no_api_field_ok() {
        // Old-format response without breaking_api_changes should still work
        let response = r#"{"breaking_behavioral_changes": []}"#;
        let (beh, api) = parse_file_behavioral_response(response).unwrap();
        assert!(beh.is_empty());
        assert!(api.is_empty());
    }

    // ── Tier 1 structured field parsing ─────────────────────────────

    #[test]
    fn parse_removal_disposition_moved_to_child() {
        let response = r#"```json
{
  "breaking_behavioral_changes": [],
  "breaking_api_changes": [
    {
      "symbol": "ModalProps.actions",
      "change": "removed",
      "description": "actions prop removed, pass as children of ModalFooter",
      "removal_disposition": {
        "type": "moved_to_related_type",
        "target_type": "ModalFooter",
        "mechanism": "children"
      }
    }
  ]
}
```"#;
        let (_beh, api) = parse_file_behavioral_response(response).unwrap();
        assert_eq!(api.len(), 1);
        assert_eq!(api[0].symbol, "ModalProps.actions");
        let disp = api[0]
            .removal_disposition
            .as_ref()
            .expect("Should have disposition");
        match disp {
            RemovalDisposition::MovedToRelatedType {
                target_type,
                mechanism,
            } => {
                assert_eq!(target_type, "ModalFooter");
                assert_eq!(mechanism, "children");
            }
            _ => panic!("Expected MovedToRelatedType, got {:?}", disp),
        }
    }

    #[test]
    fn parse_removal_disposition_moved_to_child_as_prop() {
        let response = r#"```json
{
  "breaking_behavioral_changes": [],
  "breaking_api_changes": [
    {
      "symbol": "ModalProps.title",
      "change": "removed",
      "description": "title prop moved to ModalHeader",
      "removal_disposition": {
        "type": "moved_to_related_type",
        "target_type": "ModalHeader",
        "mechanism": "prop"
      }
    }
  ]
}
```"#;
        let (_beh, api) = parse_file_behavioral_response(response).unwrap();
        let disp = api[0].removal_disposition.as_ref().unwrap();
        match disp {
            RemovalDisposition::MovedToRelatedType {
                target_type,
                mechanism,
            } => {
                assert_eq!(target_type, "ModalHeader");
                assert_eq!(mechanism, "prop");
            }
            _ => panic!("Expected MovedToRelatedType, got {:?}", disp),
        }
    }

    #[test]
    fn parse_removal_disposition_replaced_by_member() {
        let response = r#"```json
{
  "breaking_behavioral_changes": [],
  "breaking_api_changes": [
    {
      "symbol": "ButtonProps.isFlat",
      "change": "removed",
      "description": "isFlat replaced by isPlain",
      "removal_disposition": {
        "type": "replaced_by_member",
        "new_member": "isPlain"
      }
    }
  ]
}
```"#;
        let (_beh, api) = parse_file_behavioral_response(response).unwrap();
        let disp = api[0].removal_disposition.as_ref().unwrap();
        match disp {
            RemovalDisposition::ReplacedByMember { new_member } => {
                assert_eq!(new_member, "isPlain");
            }
            _ => panic!("Expected ReplacedByMember, got {:?}", disp),
        }
    }

    #[test]
    fn parse_removal_disposition_truly_removed() {
        let response = r#"```json
{
  "breaking_behavioral_changes": [],
  "breaking_api_changes": [
    {
      "symbol": "ModalProps.showClose",
      "change": "removed",
      "description": "showClose removed, close button now controlled by onClose presence",
      "removal_disposition": {"type": "truly_removed"}
    }
  ]
}
```"#;
        let (_beh, api) = parse_file_behavioral_response(response).unwrap();
        let disp = api[0].removal_disposition.as_ref().unwrap();
        assert!(matches!(disp, RemovalDisposition::TrulyRemoved));
    }

    #[test]
    fn parse_removal_disposition_made_automatic() {
        let response = r#"```json
{
  "breaking_behavioral_changes": [],
  "breaking_api_changes": [
    {
      "symbol": "SelectProps.isDynamic",
      "change": "removed",
      "description": "isDynamic now inferred automatically",
      "removal_disposition": {"type": "made_automatic"}
    }
  ]
}
```"#;
        let (_beh, api) = parse_file_behavioral_response(response).unwrap();
        let disp = api[0].removal_disposition.as_ref().unwrap();
        assert!(matches!(disp, RemovalDisposition::MadeAutomatic));
    }

    #[test]
    fn parse_removal_disposition_null_is_none() {
        let response = r#"```json
{
  "breaking_behavioral_changes": [],
  "breaking_api_changes": [
    {
      "symbol": "FooProps.bar",
      "change": "removed",
      "description": "bar removed",
      "removal_disposition": null
    }
  ]
}
```"#;
        let (_beh, api) = parse_file_behavioral_response(response).unwrap();
        assert!(api[0].removal_disposition.is_none());
    }

    #[test]
    fn parse_removal_disposition_missing_is_none() {
        // Backward compat: old responses without the field should still parse
        let response = r#"```json
{
  "breaking_behavioral_changes": [],
  "breaking_api_changes": [
    {
      "symbol": "FooProps.bar",
      "change": "removed",
      "description": "bar removed"
    }
  ]
}
```"#;
        let (_beh, api) = parse_file_behavioral_response(response).unwrap();
        assert!(api[0].removal_disposition.is_none());
    }

    #[test]
    fn parse_is_internal_only() {
        let response = r#"```json
{
  "breaking_behavioral_changes": [
    {
      "symbol": "ClipboardCopyButton",
      "kind": "class",
      "category": "render_output",
      "description": "CopyIcon now passed via icon prop internally",
      "is_internal_only": true
    },
    {
      "symbol": "Modal",
      "kind": "class",
      "category": "dom_structure",
      "description": "Modal no longer renders ModalBoxBody wrapper",
      "is_internal_only": false
    }
  ],
  "breaking_api_changes": []
}
```"#;
        let (beh, _api) = parse_file_behavioral_response(response).unwrap();
        assert_eq!(beh.len(), 2);
        assert_eq!(beh[0].is_internal_only, Some(true));
        assert_eq!(beh[1].is_internal_only, Some(false));
    }

    #[test]
    fn parse_is_internal_only_missing_is_none() {
        let response = r#"```json
{
  "breaking_behavioral_changes": [
    {"symbol": "Foo", "kind": "class", "description": "changed"}
  ],
  "breaking_api_changes": []
}
```"#;
        let (beh, _api) = parse_file_behavioral_response(response).unwrap();
        assert!(beh[0].is_internal_only.is_none());
    }

    #[test]
    fn parse_verdict_from_json() {
        let response = r#"```json
{
  "is_breaking": true,
  "reasons": ["postcondition weakened"],
  "confidence": 0.75
}
```"#;
        let verdict = parse_breaking_verdict(response).unwrap();
        assert!(verdict.is_breaking);
        assert_eq!(verdict.reasons.len(), 1);
        assert!((verdict.confidence - 0.75).abs() < f64::EPSILON);
    }

    // ── Hierarchy inference tests ───────────────────────────────────

    #[test]
    fn parse_hierarchy_response_dropdown_family() {
        let response = r#"```json
{
  "components": {
    "Dropdown": {
      "expected_children": [
        { "name": "DropdownList", "required": true },
        { "name": "DropdownGroup", "required": false }
      ]
    },
    "DropdownList": {
      "expected_children": [
        { "name": "DropdownItem", "required": true }
      ]
    },
    "DropdownGroup": {
      "expected_children": [
        { "name": "DropdownItem", "required": true }
      ]
    },
    "DropdownItem": {
      "expected_children": []
    }
  }
}
```"#;
        let result = parse_hierarchy_response(response).unwrap();
        assert_eq!(result.len(), 4);

        let dropdown = &result["Dropdown"];
        assert_eq!(dropdown.len(), 2);
        assert_eq!(dropdown[0].name, "DropdownList");
        assert!(dropdown[0].required);
        assert_eq!(dropdown[1].name, "DropdownGroup");
        assert!(!dropdown[1].required);

        let list = &result["DropdownList"];
        assert_eq!(list.len(), 1);
        assert_eq!(list[0].name, "DropdownItem");

        let item = &result["DropdownItem"];
        assert!(item.is_empty());
    }

    #[test]
    fn parse_hierarchy_response_modal_family() {
        let response = r#"```json
{
  "components": {
    "Modal": {
      "expected_children": [
        { "name": "ModalHeader", "required": false },
        { "name": "ModalBody", "required": true },
        { "name": "ModalFooter", "required": false }
      ]
    },
    "ModalHeader": { "expected_children": [] },
    "ModalBody": { "expected_children": [] },
    "ModalFooter": { "expected_children": [] }
  }
}
```"#;
        let result = parse_hierarchy_response(response).unwrap();
        assert_eq!(result.len(), 4);

        let modal = &result["Modal"];
        assert_eq!(modal.len(), 3);
        assert!(!modal[0].required); // ModalHeader optional
        assert!(modal[1].required); // ModalBody required
        assert!(!modal[2].required); // ModalFooter optional
    }

    #[test]
    fn parse_hierarchy_response_empty_components() {
        let response = r#"```json
{
  "components": {
    "Badge": {
      "expected_children": []
    }
  }
}
```"#;
        let result = parse_hierarchy_response(response).unwrap();
        assert_eq!(result.len(), 1);
        assert!(result["Badge"].is_empty());
    }

    // ── Suffix rename response parsing tests ───────────────────────

    #[test]
    fn parse_suffix_rename_response_valid() {
        let response = r#"```json
{
  "renames": [
    { "from": "PaddingTop", "to": "PaddingBlockStart" },
    { "from": "MarginLeft", "to": "MarginInlineStart" }
  ]
}
```"#;
        let result = parse_suffix_rename_response(response).unwrap();
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].from, "PaddingTop");
        assert_eq!(result[0].to, "PaddingBlockStart");
        assert_eq!(result[1].from, "MarginLeft");
        assert_eq!(result[1].to, "MarginInlineStart");
    }

    #[test]
    fn parse_suffix_rename_response_empty() {
        let response = r#"```json
{ "renames": [] }
```"#;
        let result = parse_suffix_rename_response(response).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn parse_suffix_rename_response_no_json() {
        let response = "I couldn't find any renames.";
        let result = parse_suffix_rename_response(response);
        assert!(result.is_err());
    }
}