Skip to content

Latest commit

 

History

History
169 lines (125 loc) · 5.75 KB

File metadata and controls

169 lines (125 loc) · 5.75 KB

MCP Function Sorting - Improved

Problem

The list_changed_functions tool was showing functions in a suboptimal order. All additions and deletions have a magnitude of 1.0, so they were appearing first, making it hard to find the most interesting changes (modified functions with actual code differences).

Example of Old Behavior

Changed Functions (showing 50 of 693):

1. tx_isp_proc_w00_show - deleted (magnitude: 1.00, similarity: 0.00)
2. tx_isp_proc_w00_open - deleted (magnitude: 1.00, similarity: 0.00)
3. tx_isp_proc_w01_show - deleted (magnitude: 1.00, similarity: 0.00)
...

This made it difficult to find the most important changes - functions that were actually modified with varying degrees of change.

Solution

Improved the sorting algorithm in crates/mcp-server/src/comparison/context.rs to prioritize by change type first, then by magnitude within each type:

New Sorting Priority

  1. Modified functions (sorted by magnitude, highest first)

    • These are the most interesting - actual code changes
    • Higher magnitude = more significant changes
  2. Added functions (alphabetically)

    • New functionality added to the codebase
  3. Deleted functions (alphabetically)

    • Functionality removed from the codebase
  4. Renamed/moved functions (sorted by magnitude)

    • Structural changes with minimal code impact

Implementation

/// Get functions sorted by change magnitude (most changed first)
/// 
/// Sorting priority:
/// 1. Modified functions (sorted by magnitude, highest first)
/// 2. Added functions (alphabetically)
/// 3. Deleted functions (alphabetically)
/// 4. Renamed/moved functions (sorted by magnitude)
pub fn get_sorted_changes(&self) -> Vec<FunctionChange> {
    let mut changes = self.function_changes.clone();
    changes.sort_by(|a, b| {
        // First, prioritize by change type
        let type_priority_a = Self::change_type_priority(&a.change_type);
        let type_priority_b = Self::change_type_priority(&b.change_type);
        
        match type_priority_a.cmp(&type_priority_b) {
            std::cmp::Ordering::Equal => {
                // Within same type, sort by magnitude (descending) then name
                match b.change_magnitude.partial_cmp(&a.change_magnitude) {
                    Some(std::cmp::Ordering::Equal) | None => {
                        a.function_name.cmp(&b.function_name)
                    }
                    Some(ordering) => ordering,
                }
            }
            ordering => ordering,
        }
    });
    changes
}

/// Get priority for change type (lower number = higher priority)
fn change_type_priority(change_type: &str) -> u8 {
    match change_type {
        "modified" => 0,  // Highest priority - actual code changes
        "added" => 1,     // New functionality
        "deleted" => 2,   // Removed functionality
        "renamed" => 3,   // Structural changes
        "moved" => 4,     // File reorganization
        _ => 5,           // Unknown types last
    }
}

Example of New Behavior

Changed Functions (showing 5 of 5):

1. old_name - modified (magnitude: 0.26, similarity: 0.74)
   Source: test.c (lines 22-22)
   Target: test.c (lines 21-21)
   Summary: Function 'old_name' modified (similarity: 0.74)

2. heavily_modified - modified (magnitude: 0.08, similarity: 0.92)
   Source: test.c (lines 15-15)
   Target: test.c (lines 10-10)
   Summary: Function 'heavily_modified' modified (similarity: 0.92)

3. slightly_modified - modified (magnitude: 0.02, similarity: 0.98)
   Source: test.c (lines 9-9)
   Target: test.c (lines 4-4)
   Summary: Function 'slightly_modified' modified (similarity: 0.98)

4. added_function - added (magnitude: 1.00, similarity: 0.00)
   Target: test.c (lines 31-31)
   Summary: Function added

5. deleted_function - deleted (magnitude: 1.00, similarity: 0.00)
   Source: test.c (lines 4-4)
   Summary: Function deleted

Benefits

Modified functions appear first - The most interesting changes are immediately visible

Magnitude-based ranking within modified - Functions with more significant changes appear higher

Logical grouping - Similar change types are grouped together

Better UX for large codebases - When you have 693 changes, you see the most important ones first

Testing

The fix has been tested and verified:

./test_sorting_fix.sh

Results:

  • ✅ Modified functions appear first (positions 1-3)
  • ✅ Within modified, higher magnitude comes first (0.26 > 0.08 > 0.02)
  • ✅ Added functions come next (position 4)
  • ✅ Deleted functions come last (position 5)

How to Apply

The fix has been applied and the server restarted:

  1. ✅ Code updated in crates/mcp-server/src/comparison/context.rs
  2. ✅ Binary rebuilt at target/release/smart-diff-mcp
  3. ✅ MCP server restarted with new binary

The improved sorting is now active!

Impact

  • No breaking changes - The API remains the same
  • Better user experience - Most relevant changes appear first
  • Scalable - Works well with large numbers of changes (e.g., 693 functions)
  • Intuitive - Matches developer expectations about what's "most changed"

Use Cases

This improvement is especially valuable when:

  1. Reviewing large refactorings - Modified functions with actual logic changes appear first
  2. Understanding impact - See the most significant changes immediately
  3. Code review - Focus on the functions that matter most
  4. Debugging - Quickly identify functions with substantial changes

Additional Notes

  • The magnitude calculation for modified functions is: 1.0 - similarity_score
  • A similarity of 0.74 means 26% change (magnitude 0.26)
  • A similarity of 0.98 means 2% change (magnitude 0.02)
  • Additions and deletions always have magnitude 1.0 (complete change)