File size: 7,537 Bytes
dce5ebb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Simple demo to test the Sponge MCP Server tools directly

"""
import asyncio
import json
import sys
import os

# Add the current directory to Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from sponge_mcp_server import (
    get_flaky_index,
    detect_regression_window,
    compare_log_diffs,
    locate_error_root,
    correlate_cross_service_logs,
    summarize_log_anomalies,
    fetch_stack_trace_context,
    inspect_visual_state,
    compare_visual_baselines,
    analyze_concurrency_drift
)

def print_section(title):
    """Print a formatted section header"""
    print("\n" + "=" * 60)
    print(f" {title}")
    print("=" * 60)

def print_result(tool_name, result):
    """Print tool result in a formatted way"""
    print(f"\n--- {tool_name} ---")
    print(json.dumps(result, indent=2))

async def demo_all_tools():
    """Demo all Sponge tools directly"""
    print_section("SPONGE MCP SERVER TOOLS DEMO")
    
    # Test 1: Flaky Index
    print_result("get_flaky_index", 
                get_flaky_index("//service/auth:flaky_login_test", 30))
    
    # Test 2: Regression Window
    print_result("detect_regression_window",
                detect_regression_window("//service/payment:timeout_test", 7))
    
    # Test 3: Log Diffs
    print_result("compare_log_diffs",
                compare_log_diffs("inv_failed_12345", "inv_passed_12344", "//service/auth:test"))
    
    # Test 4: Error Root
    print_result("locate_error_root",
                locate_error_root("inv_failed_12345", "//service/auth:test"))
    
    # Test 5: Cross-Service Logs
    print_result("correlate_cross_service_logs",
                correlate_cross_service_logs("inv_cross_12345", 
                                           ["//service/auth:test", "//service/payment:test"], 300))
    
    # Test 6: Log Anomalies
    print_result("summarize_log_anomalies",
                summarize_log_anomalies("inv_anomaly_12345", "//service/auth:test", 2))
    
    # Test 7: Stack Trace Context
    print_result("fetch_stack_trace_context",
                fetch_stack_trace_context("inv_stack_12345", "//service/auth:test", 5))
    
    # Test 8: Visual State
    print_result("inspect_visual_state",
                inspect_visual_state("inv_visual_12345", "//ui/checkout:test", "click_submit"))
    
    # Test 9: Visual Baselines
    print_result("compare_visual_baselines",
                compare_visual_baselines("inv_visual_fail_12345", "inv_visual_pass_12344", "//ui/checkout:test"))
    
    # Test 10: Concurrency Drift
    print_result("analyze_concurrency_drift",
                analyze_concurrency_drift("//service/database:connection_test", 30))

def demo_analysis_workflow():
    """Demo a complete analysis workflow"""
    print_section("COMPLETE ANALYSIS WORKFLOW DEMO")
    
    target_id = "//service/auth:flaky_login_test"
    failing_invocation = "inv_failed_auth_12345"
    passing_invocation = "inv_passed_auth_12344"
    
    print(f"Analyzing test failure for: {target_id}")
    print(f"Failing invocation: {failing_invocation}")
    print(f"Passing invocation: {passing_invocation}")
    
    # Step 1: Check flakiness
    print("\nπŸ” Step 1: Checking for flakiness...")
    flaky_result = get_flaky_index(target_id, 50)
    print(f"Flaky ratio: {flaky_result['flaky_ratio']:.2%}")
    print(f"Recommendation: {flaky_result['recommendation']}")
    
    # Step 2: Look for regression
    print("\nπŸ“ˆ Step 2: Detecting regression window...")
    regression_result = detect_regression_window(target_id, 7)
    print(f"Analysis: {regression_result['analysis']}")
    
    # Step 3: Compare logs
    print("\nπŸ“‹ Step 3: Comparing logs...")
    log_diff_result = compare_log_diffs(failing_invocation, passing_invocation, target_id)
    print(f"Analysis: {log_diff_result['analysis']}")
    print(f"New errors found: {len(log_diff_result['new_errors'])}")
    
    # Step 4: Find error root
    print("\n🎯 Step 4: Locating error root...")
    error_root_result = locate_error_root(failing_invocation, target_id)
    print(f"Error line: {error_root_result['error_line']}")
    
    # Step 5: Analyze anomalies
    print("\nπŸ” Step 5: Summarizing anomalies...")
    anomaly_result = summarize_log_anomalies(failing_invocation, target_id, 2)
    print(f"Unique error types: {anomaly_result['total_error_types']}")
    
    # Step 6: Check for drift
    print("\n⏱️ Step 6: Analyzing performance drift...")
    drift_result = analyze_concurrency_drift(target_id, 30)
    drift_pct = drift_result['duration_trend']['drift_percentage']
    print(f"Duration drift: {drift_pct:.1f}%")
    
    # Generate summary
    print_section("ANALYSIS SUMMARY")
    
    recommendations = []
    confidence_factors = []
    
    if flaky_result['flaky_ratio'] > 0.2:
        recommendations.append("🚨 High flakiness detected - consider stabilizing test")
        confidence_factors.append(0.8)
    
    if regression_result['confidence'] == 'high':
        recommendations.append(f"πŸ“… Regression detected - review {regression_result.get('changelist', 'recent changes')}")
        confidence_factors.append(0.9)
    
    if log_diff_result['new_errors']:
        recommendations.append("πŸ” New errors found - investigate error patterns")
        confidence_factors.append(0.7)
    
    if error_root_result['error_line'] != "No error found after last INFO message":
        recommendations.append("🎯 Primary error location identified")
        confidence_factors.append(0.8)
    
    if abs(drift_pct) > 20:
        recommendations.append("⚑ Significant performance drift - environment may need refresh")
        confidence_factors.append(0.7)
    
    overall_confidence = sum(confidence_factors) / len(confidence_factors) if confidence_factors else 0.5
    
    print(f"Overall Confidence Score: {overall_confidence:.2f}")
    print("\nRecommendations:")
    for i, rec in enumerate(recommendations, 1):
        print(f"{i}. {rec}")
    
    if not recommendations:
        print("βœ… No major issues detected. Test appears to be functioning normally.")
    
    print("\nNext Steps:")
    if flaky_result['flaky_ratio'] > 0.3:
        print("1. Add test to flaky test list")
        print("2. Investigate root cause of flakiness")
    elif regression_result['confidence'] == 'high':
        print("1. Review recent code changes")
        print("2. Consider reverting problematic changes")
    else:
        print("1. Monitor test for additional failures")
        print("2. Consider adding more detailed logging")

async def main():
    """Run the complete demo"""
    print("πŸš€ Starting Sponge MCP Server Demo")
    print("This demo showcases all 10 analysis tools and a complete workflow")
    
    # Demo individual tools
    await demo_all_tools()
    
    # Demo complete workflow
    demo_analysis_workflow()
    
    print_section("DEMO COMPLETED SUCCESSFULLY!")
    print("βœ… All 10 Sponge tools are working correctly")
    print("βœ… Complete analysis workflow demonstrated")
    print("βœ… System is ready for production use")
    
    print("\nπŸ“š Next steps:")
    print("1. Integrate with your test infrastructure")
    print("2. Configure MCP server in your IDE")
    print("3. Start analyzing real test failures!")

if __name__ == "__main__":
    asyncio.run(main())