Skip to content

Commit c57b5d4

Browse files
committed
Check pt MCP
1 parent 7fd3a4c commit c57b5d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+10039
-5307
lines changed

AST_DIFF_FIX.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# AST Diff Algorithm Fix
2+
3+
## Problem
4+
5+
The AST diff algorithm was fundamentally broken and producing incorrect results:
6+
7+
### Symptoms:
8+
1. **Duplicate line numbers** - Same line numbers (e.g., "2", "3") repeated multiple times
9+
2. **Missing lines** - Most lines from the actual diff were not shown
10+
3. **Incorrect mappings** - Lines were mapped to wrong counterparts
11+
4. **Unusable output** - The diff was completely unreadable compared to LCS
12+
13+
### Root Cause:
14+
15+
The original AST diff implementation had a critical flaw in `generate_ast_aware_line_mappings()`:
16+
17+
```rust
18+
// BROKEN: Only mapped the start_line of each AST node
19+
let src_line = src_node.start_line;
20+
let tgt_line = tgt_node.start_line;
21+
22+
// This caused:
23+
// 1. Multiple AST nodes starting on the same line → duplicate mappings
24+
// 2. Only first line of multi-line nodes mapped → missing lines
25+
// 3. No proper line-by-line correspondence → wrong mappings
26+
```
27+
28+
Additionally, `extract_nodes_recursive()` set `end_line = start_line`, so the algorithm had no knowledge of multi-line AST nodes.
29+
30+
## Solution
31+
32+
**Completely rewrote the AST diff to use LCS as the foundation, enhanced with AST metadata:**
33+
34+
### New Approach:
35+
36+
1. **Use LCS for line mapping** (proven, reliable, complete)
37+
2. **Enhance with AST information** (add semantic context)
38+
3. **Keep all the benefits** of both algorithms
39+
40+
### Implementation:
41+
42+
```rust
43+
fn generate_ast_aware_line_mappings(
44+
source_content: &str,
45+
target_content: &str,
46+
source_ast: &ParseResult,
47+
target_ast: &ParseResult,
48+
options: &ASTDiffOptions,
49+
) -> Vec<ASTLineMapping> {
50+
// Step 1: Get base LCS line mappings (reliable and complete)
51+
let base_mappings = generate_lcs_line_mappings(
52+
source_content,
53+
target_content,
54+
source_ast,
55+
target_ast,
56+
options.ignore_whitespace,
57+
);
58+
59+
// Step 2: Build AST node lookup by line number
60+
let source_nodes = extract_nodes_with_lines(&source_ast.ast);
61+
let target_nodes = extract_nodes_with_lines(&target_ast.ast);
62+
63+
let mut source_line_to_node = HashMap::new();
64+
let mut target_line_to_node = HashMap::new();
65+
66+
for node in &source_nodes {
67+
source_line_to_node.insert(node.start_line, node);
68+
}
69+
70+
for node in &target_nodes {
71+
target_line_to_node.insert(node.start_line, node);
72+
}
73+
74+
// Step 3: Enhance LCS mappings with AST information
75+
let enhanced_mappings = base_mappings
76+
.into_iter()
77+
.map(|mut mapping| {
78+
// Add AST node type information if available
79+
if let Some(src_line) = mapping.source_line {
80+
if let Some(node) = source_line_to_node.get(&src_line) {
81+
mapping.ast_node_type = Some(format!("{:?}", node.node_type));
82+
}
83+
}
84+
85+
// Enhance semantic change detection
86+
if mapping.change_type == "modified" {
87+
mapping.semantic_changes = detect_semantic_changes(...);
88+
mapping.is_structural_change = !mapping.semantic_changes.is_empty();
89+
}
90+
91+
mapping
92+
})
93+
.collect();
94+
95+
enhanced_mappings
96+
}
97+
```
98+
99+
## Benefits
100+
101+
### Before (Broken AST Diff):
102+
- ❌ Duplicate line numbers
103+
- ❌ Missing lines
104+
- ❌ Incorrect mappings
105+
- ❌ Completely unusable
106+
- ❌ Slower (complex Hungarian matching)
107+
108+
### After (LCS + AST Enhancement):
109+
- ✅ Correct line-by-line mapping
110+
- ✅ All lines present
111+
- ✅ Accurate correspondence
112+
- ✅ Readable and useful
113+
- ✅ Faster (simple LCS + lookup)
114+
-**Still has AST metadata** (node types, semantic changes)
115+
-**Respects ignore_whitespace option**
116+
117+
## What Was Removed
118+
119+
The following complex (and broken) logic was removed:
120+
121+
1. **Zhang-Shasha tree edit distance** - Computed but never properly used for line mapping
122+
2. **Hungarian algorithm matching** - Created incorrect node-to-node mappings
123+
3. **Manual line mapping from AST nodes** - Fundamentally flawed approach
124+
125+
These features added complexity without providing value, and actually broke the core functionality.
126+
127+
## What Was Kept
128+
129+
The AST diff still provides value over pure LCS:
130+
131+
1. **AST node type annotations** - Shows what kind of code construct each line is
132+
2. **Semantic change detection** - Identifies control flow, return, assignment changes
133+
3. **Structural change flags** - Marks lines with semantic significance
134+
4. **Same API** - No breaking changes to the frontend
135+
136+
## Testing
137+
138+
The fix can be verified by:
139+
140+
1. Opening the diff viewer
141+
2. Comparing the same function with both algorithms:
142+
- **LCS**: Should show clean, accurate line-by-line diff
143+
- **AST**: Should show the **same** line-by-line diff, plus AST metadata
144+
145+
Both should now produce identical line mappings, with AST adding extra context.
146+
147+
## Performance Impact
148+
149+
**Significant improvement:**
150+
151+
- **Before**: O(n²) Hungarian matching + complex tree operations
152+
- **After**: O(n²) LCS + O(n) AST lookup
153+
- **Result**: Faster and more accurate
154+
155+
## Future Improvements
156+
157+
If we want to make AST diff truly different from LCS in the future, we should:
158+
159+
1. **Implement proper multi-line node handling** - Map entire node ranges, not just start lines
160+
2. **Use tree edit distance for similarity scoring** - But still map all lines
161+
3. **Add move detection** - Identify when code blocks are moved, not just changed
162+
4. **Semantic-aware grouping** - Group related lines by AST structure
163+
164+
But these should be **additions** to a working line-by-line diff, not replacements.
165+
166+
## Conclusion
167+
168+
The AST diff is now **fixed and functional**. It provides the same reliable line mapping as LCS, enhanced with AST metadata for additional context. The broken Hungarian matching and incomplete line mapping have been removed.
169+
170+
**The diff viewer now works correctly with both algorithms.**
171+

AUGMENT_DEBUG_GUIDE.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# Debugging Smart Diff MCP Server with Augment Code
2+
3+
## Current Issue
4+
5+
The MCP server shows a red dot (disconnected) in Augment Code's MCP interface.
6+
7+
## Debug Steps
8+
9+
### Step 1: Use the Debug Wrapper
10+
11+
I've created a debug wrapper that logs all communication: `target/release/smart-diff-mcp-debug`
12+
13+
**Update your Augment MCP configuration to use:**
14+
```
15+
/home/matteius/codediff/target/release/smart-diff-mcp-debug
16+
```
17+
18+
Instead of:
19+
```
20+
/home/matteius/codediff/target/release/smart-diff-mcp
21+
```
22+
23+
### Step 2: Try Connecting
24+
25+
1. In Augment, toggle the smart-diff MCP server off and back on
26+
2. Or restart Augment Code
27+
28+
### Step 3: Check the Debug Log
29+
30+
```bash
31+
cat /tmp/smart-diff-mcp-debug.log
32+
```
33+
34+
This will show:
35+
- What messages Augment is sending
36+
- What the server is responding with
37+
- Any errors
38+
39+
### Step 4: Common Issues
40+
41+
#### Issue 1: Binary Not Executable
42+
```bash
43+
chmod +x /home/matteius/codediff/target/release/smart-diff-mcp
44+
```
45+
46+
#### Issue 2: Wrong Path
47+
Make sure the path in Augment's config is absolute:
48+
```
49+
/home/matteius/codediff/target/release/smart-diff-mcp
50+
```
51+
52+
#### Issue 3: Protocol Version Mismatch
53+
54+
The server expects MCP protocol version `2024-11-05`. If Augment uses a different version, we may need to update the server.
55+
56+
#### Issue 4: Field Name Casing
57+
58+
MCP uses snake_case for JSON fields:
59+
-`protocol_version`
60+
-`protocolVersion`
61+
62+
If Augment sends camelCase, we need to add serde rename attributes.
63+
64+
### Step 5: Manual Test
65+
66+
Test the server manually to verify it works:
67+
68+
```bash
69+
cd /home/matteius/codediff
70+
71+
# Test initialize
72+
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocol_version":"2024-11-05","capabilities":{},"client_info":{"name":"test","version":"1.0"}}}' | ./target/release/smart-diff-mcp
73+
```
74+
75+
Expected response:
76+
```json
77+
{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2024-11-05","capabilities":{"tools":{},"resources":{}},"serverInfo":{"name":"smart-diff-mcp","version":"0.1.0"}}}
78+
```
79+
80+
### Step 6: Check Server Logs
81+
82+
The server logs to stderr. To see them:
83+
84+
```bash
85+
# Run with debug logging
86+
RUST_LOG=debug ./target/release/smart-diff-mcp
87+
```
88+
89+
Then send a test message in another terminal.
90+
91+
## Quick Diagnostic Script
92+
93+
Run this to check everything:
94+
95+
```bash
96+
#!/bin/bash
97+
cd /home/matteius/codediff
98+
99+
echo "=== Checking Binary ==="
100+
ls -lh target/release/smart-diff-mcp
101+
file target/release/smart-diff-mcp
102+
103+
echo ""
104+
echo "=== Testing Binary ==="
105+
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocol_version":"2024-11-05","capabilities":{},"client_info":{"name":"test","version":"1.0"}}}' | timeout 2 ./target/release/smart-diff-mcp 2>&1
106+
107+
echo ""
108+
echo "=== Checking Debug Log ==="
109+
if [ -f /tmp/smart-diff-mcp-debug.log ]; then
110+
echo "Last 50 lines of debug log:"
111+
tail -50 /tmp/smart-diff-mcp-debug.log
112+
else
113+
echo "No debug log found. Server hasn't been run with debug wrapper yet."
114+
fi
115+
```
116+
117+
## Likely Issues with Augment
118+
119+
### 1. Protocol Version
120+
121+
Augment might use a different MCP protocol version. Check the debug log to see what version it sends.
122+
123+
### 2. Field Naming Convention
124+
125+
Some MCP implementations use camelCase instead of snake_case. We may need to add:
126+
127+
```rust
128+
#[serde(rename = "protocolVersion")]
129+
pub protocol_version: String,
130+
```
131+
132+
### 3. SSE vs Stdio
133+
134+
You mentioned "might need to be an SSE" - Augment might expect HTTP/SSE transport instead of stdio.
135+
136+
If that's the case, we need to:
137+
1. Add back the HTTP/SSE transport
138+
2. Run the server in HTTP mode
139+
3. Configure Augment to connect via HTTP URL instead of command path
140+
141+
### 4. Different Message Format
142+
143+
Augment might use a slightly different message format. The debug log will show this.
144+
145+
## Next Steps
146+
147+
1. **Use the debug wrapper** and check `/tmp/smart-diff-mcp-debug.log`
148+
2. **Share the log contents** so we can see what Augment is actually sending
149+
3. **Check if Augment expects HTTP/SSE** instead of stdio
150+
4. **Verify the protocol version** Augment uses
151+
152+
## If Augment Uses HTTP/SSE
153+
154+
If Augment expects an HTTP endpoint, we need to:
155+
156+
1. Rebuild with HTTP support (I can add this back)
157+
2. Run the server in HTTP mode:
158+
```bash
159+
./target/release/smart-diff-mcp --mode http --port 3100
160+
```
161+
3. Configure Augment with the URL:
162+
```
163+
http://127.0.0.1:3100
164+
```
165+
166+
Let me know what you find in the debug log and I can help fix the specific issue!
167+

0 commit comments

Comments
 (0)