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
//! Tier 1: Structural comparison of `FunctionSpec` objects.
//!
//! Compares two specs mechanically without LLM. This catches clear-cut
//! breaking changes where:
//! - Preconditions were tightened (new precondition added, existing one changed)
//! - Postconditions were weakened (postcondition removed, return value changed)
//! - Error behavior changed (type changed, new error added for existing input)
//! - Side effects removed or changed
//!
//! ## Matching Strategy
//!
//! Entries are matched by their "identity" field:
//! - `Precondition` entries match on `parameter`
//! - `Postcondition` entries match on `condition`
//! - `ErrorBehavior` entries match on `trigger`
//! - `SideEffect` entries match on `target` + `action` pair
//!
//! String comparisons use normalized lowercase trimmed form.

use semver_analyzer_core::{BreakingVerdict, FunctionSpec};

/// Perform a structural (no-LLM) comparison of two function specs.
///
/// Returns a `BreakingVerdict` with confidence 0.80 for clear structural
/// differences, or 0.0/not-breaking if the specs are structurally equivalent.
pub fn structural_compare(old: &FunctionSpec, new: &FunctionSpec) -> BreakingVerdict {
    let mut reasons = Vec::new();

    // ── Preconditions ───────────────────────────────────────────────
    // Breaking if: new precondition added, or existing one tightened
    // Not breaking if: precondition removed (more permissive)
    compare_preconditions(old, new, &mut reasons);

    // ── Postconditions ──────────────────────────────────────────────
    // Breaking if: postcondition removed, or return value changed
    // Not breaking if: new postcondition added (more guarantees)
    compare_postconditions(old, new, &mut reasons);

    // ── Error behavior ──────────────────────────────────────────────
    // Breaking if: error type changed, or new error for existing input
    // Not breaking if: error removed (more permissive)
    compare_error_behavior(old, new, &mut reasons);

    // ── Side effects ────────────────────────────────────────────────
    // Breaking if: side effect removed or changed
    // Potentially breaking if: new side effect added
    compare_side_effects(old, new, &mut reasons);

    let is_breaking = !reasons.is_empty();
    let confidence = if is_breaking { 0.80 } else { 0.0 };

    BreakingVerdict {
        is_breaking,
        reasons,
        confidence,
    }
}

// ── Precondition Comparison ─────────────────────────────────────────────

fn compare_preconditions(old: &FunctionSpec, new: &FunctionSpec, reasons: &mut Vec<String>) {
    // Check for new preconditions (tightened)
    for new_pre in &new.preconditions {
        let matched = old
            .preconditions
            .iter()
            .find(|old_pre| normalize(&old_pre.parameter) == normalize(&new_pre.parameter));

        match matched {
            None => {
                // New precondition added — function accepts less input
                reasons.push(format!(
                    "New precondition added: parameter '{}' now requires '{}'",
                    new_pre.parameter, new_pre.condition
                ));
            }
            Some(old_pre) => {
                // Existing precondition — check if tightened
                if normalize(&old_pre.condition) != normalize(&new_pre.condition) {
                    reasons.push(format!(
                        "Precondition changed for '{}': '{}' → '{}'",
                        new_pre.parameter, old_pre.condition, new_pre.condition
                    ));
                }
                if normalize(&old_pre.on_violation) != normalize(&new_pre.on_violation) {
                    reasons.push(format!(
                        "Violation behavior changed for '{}': '{}' → '{}'",
                        new_pre.parameter, old_pre.on_violation, new_pre.on_violation
                    ));
                }
            }
        }
    }
}

// ── Postcondition Comparison ────────────────────────────────────────────

fn compare_postconditions(old: &FunctionSpec, new: &FunctionSpec, reasons: &mut Vec<String>) {
    // Check for removed postconditions (weakened)
    for old_post in &old.postconditions {
        let matched = new
            .postconditions
            .iter()
            .find(|new_post| normalize(&new_post.condition) == normalize(&old_post.condition));

        match matched {
            None => {
                // Postcondition removed — function guarantees less
                reasons.push(format!(
                    "Postcondition removed: '{}' no longer guarantees '{}'",
                    old_post.condition, old_post.returns
                ));
            }
            Some(new_post) => {
                // Check if return value changed
                if normalize(&old_post.returns) != normalize(&new_post.returns) {
                    reasons.push(format!(
                        "Postcondition return changed for '{}': '{}' → '{}'",
                        old_post.condition, old_post.returns, new_post.returns
                    ));
                }
            }
        }
    }
}

// ── Error Behavior Comparison ───────────────────────────────────────────

fn compare_error_behavior(old: &FunctionSpec, new: &FunctionSpec, reasons: &mut Vec<String>) {
    // Check for changed error types (breaking)
    for old_err in &old.error_behavior {
        let matched = new
            .error_behavior
            .iter()
            .find(|new_err| normalize(&new_err.trigger) == normalize(&old_err.trigger));

        match matched {
            Some(new_err) if normalize(&old_err.error_type) != normalize(&new_err.error_type) => {
                reasons.push(format!(
                    "Error type changed for trigger '{}': {}{}",
                    old_err.trigger, old_err.error_type, new_err.error_type
                ));
            }
            _ => {
                // Either error types match, or error case removed (more permissive)
            }
        }
    }

    // Check for new error cases (breaking: function throws where it didn't)
    for new_err in &new.error_behavior {
        let existed = old
            .error_behavior
            .iter()
            .any(|old_err| normalize(&old_err.trigger) == normalize(&new_err.trigger));

        if !existed {
            reasons.push(format!(
                "New error case: '{}' now throws {} (was not an error before)",
                new_err.trigger, new_err.error_type
            ));
        }
    }
}

// ── Side Effect Comparison ──────────────────────────────────────────────

fn compare_side_effects(old: &FunctionSpec, new: &FunctionSpec, reasons: &mut Vec<String>) {
    // Check for removed side effects (breaking)
    for old_se in &old.side_effects {
        let matched = new.side_effects.iter().find(|new_se| {
            normalize(&new_se.target) == normalize(&old_se.target)
                && normalize(&new_se.action) == normalize(&old_se.action)
        });

        if matched.is_none() {
            reasons.push(format!(
                "Side effect removed: '{}' no longer '{}'",
                old_se.target, old_se.action
            ));
        }
    }

    // Check for changed side effects (same target, different action)
    for old_se in &old.side_effects {
        for new_se in &new.side_effects {
            if normalize(&old_se.target) == normalize(&new_se.target)
                && normalize(&old_se.action) != normalize(&new_se.action)
            {
                reasons.push(format!(
                    "Side effect changed for '{}': '{}' → '{}'",
                    old_se.target, old_se.action, new_se.action
                ));
            }
        }
    }
}

// ── Normalization ───────────────────────────────────────────────────────

/// Normalize a string for comparison: lowercase, trimmed, collapsed whitespace.
fn normalize(s: &str) -> String {
    s.trim()
        .to_lowercase()
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
}

#[cfg(test)]
mod tests {
    use super::*;
    use semver_analyzer_core::{ErrorBehavior, Postcondition, Precondition, SideEffect};

    fn empty_spec() -> FunctionSpec {
        FunctionSpec {
            preconditions: vec![],
            postconditions: vec![],
            error_behavior: vec![],
            side_effects: vec![],
            notes: vec![],
        }
    }

    // ── No changes ──────────────────────────────────────────────────

    #[test]
    fn identical_specs_not_breaking() {
        let spec = empty_spec();
        let result = structural_compare(&spec, &spec);
        assert!(!result.is_breaking);
        assert!(result.reasons.is_empty());
    }

    #[test]
    fn both_empty_not_breaking() {
        let result = structural_compare(&empty_spec(), &empty_spec());
        assert!(!result.is_breaking);
    }

    // ── Precondition changes ────────────────────────────────────────

    #[test]
    fn new_precondition_is_breaking() {
        let old = empty_spec();
        let mut new = empty_spec();
        new.preconditions.push(Precondition {
            parameter: "email".into(),
            condition: "must be non-empty".into(),
            on_violation: "throws TypeError".into(),
        });

        let result = structural_compare(&old, &new);
        assert!(result.is_breaking);
        assert!(result.reasons[0].contains("New precondition added"));
    }

    #[test]
    fn tightened_precondition_is_breaking() {
        let mut old = empty_spec();
        old.preconditions.push(Precondition {
            parameter: "email".into(),
            condition: "must be a string".into(),
            on_violation: "throws TypeError".into(),
        });

        let mut new = empty_spec();
        new.preconditions.push(Precondition {
            parameter: "email".into(),
            condition: "must be a non-empty string with @ symbol".into(),
            on_violation: "throws TypeError".into(),
        });

        let result = structural_compare(&old, &new);
        assert!(result.is_breaking);
        assert!(result.reasons[0].contains("Precondition changed"));
    }

    #[test]
    fn removed_precondition_not_breaking() {
        let mut old = empty_spec();
        old.preconditions.push(Precondition {
            parameter: "email".into(),
            condition: "must be non-empty".into(),
            on_violation: "throws TypeError".into(),
        });

        let new = empty_spec();

        let result = structural_compare(&old, &new);
        assert!(!result.is_breaking); // More permissive = not breaking
    }

    // ── Postcondition changes ───────────────────────────────────────

    #[test]
    fn removed_postcondition_is_breaking() {
        let mut old = empty_spec();
        old.postconditions.push(Postcondition {
            condition: "valid input".into(),
            returns: "user object".into(),
        });

        let new = empty_spec();

        let result = structural_compare(&old, &new);
        assert!(result.is_breaking);
        assert!(result.reasons[0].contains("Postcondition removed"));
    }

    #[test]
    fn changed_postcondition_return_is_breaking() {
        let mut old = empty_spec();
        old.postconditions.push(Postcondition {
            condition: "always".into(),
            returns: "lowercased email".into(),
        });

        let mut new = empty_spec();
        new.postconditions.push(Postcondition {
            condition: "always".into(),
            returns: "lowercased email with + alias stripped".into(),
        });

        let result = structural_compare(&old, &new);
        assert!(result.is_breaking);
        assert!(result.reasons[0].contains("Postcondition return changed"));
    }

    #[test]
    fn added_postcondition_not_breaking() {
        let old = empty_spec();

        let mut new = empty_spec();
        new.postconditions.push(Postcondition {
            condition: "valid input".into(),
            returns: "user object".into(),
        });

        let result = structural_compare(&old, &new);
        assert!(!result.is_breaking); // More guarantees = not breaking
    }

    // ── Error behavior changes ──────────────────────────────────────

    #[test]
    fn changed_error_type_is_breaking() {
        let mut old = empty_spec();
        old.error_behavior.push(ErrorBehavior {
            trigger: "invalid email".into(),
            error_type: "TypeError".into(),
            message_pattern: None,
        });

        let mut new = empty_spec();
        new.error_behavior.push(ErrorBehavior {
            trigger: "invalid email".into(),
            error_type: "ValidationError".into(),
            message_pattern: None,
        });

        let result = structural_compare(&old, &new);
        assert!(result.is_breaking);
        assert!(result.reasons[0].contains("Error type changed"));
    }

    #[test]
    fn new_error_case_is_breaking() {
        let old = empty_spec();

        let mut new = empty_spec();
        new.error_behavior.push(ErrorBehavior {
            trigger: "empty input".into(),
            error_type: "Error".into(),
            message_pattern: None,
        });

        let result = structural_compare(&old, &new);
        assert!(result.is_breaking);
        assert!(result.reasons[0].contains("New error case"));
    }

    #[test]
    fn removed_error_case_not_breaking() {
        let mut old = empty_spec();
        old.error_behavior.push(ErrorBehavior {
            trigger: "invalid input".into(),
            error_type: "Error".into(),
            message_pattern: None,
        });

        let new = empty_spec();

        let result = structural_compare(&old, &new);
        assert!(!result.is_breaking); // More permissive = not breaking
    }

    // ── Side effect changes ─────────────────────────────────────────

    #[test]
    fn removed_side_effect_is_breaking() {
        let mut old = empty_spec();
        old.side_effects.push(SideEffect {
            target: "database".into(),
            action: "inserts user row".into(),
            condition: None,
        });

        let new = empty_spec();

        let result = structural_compare(&old, &new);
        assert!(result.is_breaking);
        assert!(result.reasons[0].contains("Side effect removed"));
    }

    #[test]
    fn changed_side_effect_is_breaking() {
        let mut old = empty_spec();
        old.side_effects.push(SideEffect {
            target: "database".into(),
            action: "inserts user row".into(),
            condition: None,
        });

        let mut new = empty_spec();
        new.side_effects.push(SideEffect {
            target: "database".into(),
            action: "upserts user row".into(),
            condition: None,
        });

        let result = structural_compare(&old, &new);
        assert!(result.is_breaking);
        assert!(result
            .reasons
            .iter()
            .any(|r| r.contains("Side effect changed")));
    }

    // ── Normalization ───────────────────────────────────────────────

    #[test]
    fn normalization_handles_whitespace() {
        let mut old = empty_spec();
        old.postconditions.push(Postcondition {
            condition: "  Always  ".into(),
            returns: "  user   object  ".into(),
        });

        let mut new = empty_spec();
        new.postconditions.push(Postcondition {
            condition: "always".into(),
            returns: "user object".into(),
        });

        let result = structural_compare(&old, &new);
        assert!(!result.is_breaking); // Should match after normalization
    }

    #[test]
    fn normalization_handles_case() {
        let mut old = empty_spec();
        old.postconditions.push(Postcondition {
            condition: "ALWAYS".into(),
            returns: "User Object".into(),
        });

        let mut new = empty_spec();
        new.postconditions.push(Postcondition {
            condition: "always".into(),
            returns: "user object".into(),
        });

        let result = structural_compare(&old, &new);
        assert!(!result.is_breaking);
    }

    // ── Multiple changes ────────────────────────────────────────────

    #[test]
    fn multiple_breaking_changes() {
        let mut old = empty_spec();
        old.postconditions.push(Postcondition {
            condition: "valid".into(),
            returns: "true".into(),
        });

        let mut new = empty_spec();
        new.preconditions.push(Precondition {
            parameter: "input".into(),
            condition: "must be non-null".into(),
            on_violation: "throws".into(),
        });
        new.postconditions.push(Postcondition {
            condition: "valid".into(),
            returns: "object".into(),
        });
        new.error_behavior.push(ErrorBehavior {
            trigger: "null input".into(),
            error_type: "TypeError".into(),
            message_pattern: None,
        });

        let result = structural_compare(&old, &new);
        assert!(result.is_breaking);
        assert!(result.reasons.len() >= 3);
    }

    // ── Confidence scoring ──────────────────────────────────────────

    #[test]
    fn breaking_has_high_confidence() {
        let old = empty_spec();
        let mut new = empty_spec();
        new.preconditions.push(Precondition {
            parameter: "x".into(),
            condition: "must be positive".into(),
            on_violation: "throws".into(),
        });

        let result = structural_compare(&old, &new);
        assert!(result.is_breaking);
        assert!((result.confidence - 0.80).abs() < f64::EPSILON);
    }

    #[test]
    fn not_breaking_has_zero_confidence() {
        let result = structural_compare(&empty_spec(), &empty_spec());
        assert!(!result.is_breaking);
        assert!((result.confidence - 0.0).abs() < f64::EPSILON);
    }
}