forked from FSoft-AI4Code/CodeWiki
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_module_disambiguation.py
More file actions
305 lines (250 loc) · 11.2 KB
/
Copy pathtest_module_disambiguation.py
File metadata and controls
305 lines (250 loc) · 11.2 KB
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
#!/usr/bin/env python3
"""
Test demonstrating the module-context disambiguation fix for CodeWiki.
This test shows how the enhanced _find_best_path_match() function now uses
module name context to resolve ambiguous component matches like DeviceController.
"""
import sys
import logging
from typing import List, Optional
# Setup logging
logging.basicConfig(
level=logging.DEBUG,
format='[%(levelname)s] %(message)s'
)
logger = logging.getLogger(__name__)
def _find_best_path_match_original(llm_id: str, candidates: List[str]) -> Optional[str]:
"""Original implementation WITHOUT module context."""
llm_segments = llm_id.split('.')
scores = []
for candidate in candidates:
candidate_segments = candidate.split('.')
matches = 0
for i, llm_seg in enumerate(llm_segments):
if llm_seg in candidate_segments:
try:
candidate_idx = candidate_segments.index(llm_seg)
position_diff = abs(i - candidate_idx)
matches += 1 + (1.0 / (1 + position_diff))
except ValueError:
pass
scores.append((candidate, matches))
scores.sort(key=lambda x: x[1], reverse=True)
if len(scores) > 1 and scores[0][1] > scores[1][1]:
logger.debug(f"Original - Best match score: {scores[0][1]:.2f} vs next: {scores[1][1]:.2f}")
return scores[0][0]
return None
def _find_best_path_match_enhanced(llm_id: str, candidates: List[str], module_name: str = None) -> Optional[str]:
"""Enhanced implementation WITH module context."""
llm_segments = llm_id.split('.')
module_segments = []
if module_name:
module_segments = module_name.replace('_', '-').split('-')
module_segments = [seg.lower() for seg in module_segments if seg]
scores = []
for candidate in candidates:
candidate_segments = candidate.split('.')
matches = 0
for i, llm_seg in enumerate(llm_segments):
if llm_seg in candidate_segments:
try:
candidate_idx = candidate_segments.index(llm_seg)
position_diff = abs(i - candidate_idx)
matches += 1 + (1.0 / (1 + position_diff))
except ValueError:
pass
# Module context boost
if module_segments:
candidate_lower = candidate.lower()
candidate_path_parts = candidate_lower.replace('.', '-').replace('_', '-').split('-')
module_boost = 0
extra_segments_penalty = 0
# Try to find exact consecutive match
for i in range(len(candidate_path_parts) - len(module_segments) + 1):
window = candidate_path_parts[i:i + len(module_segments)]
if window == module_segments:
module_boost = len(module_segments) * 3.0
logger.debug(
f"Enhanced - EXACT module match for '{candidate}': "
f"segments {module_segments} at position {i}, +{module_boost:.1f} points"
)
break
# If no exact match, count individual segments with penalty
if module_boost == 0:
matched_segments = []
for module_seg in module_segments:
if module_seg in candidate_path_parts:
matched_segments.append(module_seg)
module_boost += 1.5
if matched_segments:
extra_segments = [seg for seg in candidate_path_parts if seg not in module_segments]
if len(extra_segments) > 10:
extra_segments_penalty = len(extra_segments) * 0.3
module_boost -= extra_segments_penalty
logger.debug(
f"Enhanced - Partial module match for '{candidate}': "
f"{len(matched_segments)}/{len(module_segments)} segments, "
f"penalty -{extra_segments_penalty:.1f}, net +{module_boost:.1f} points"
)
matches += module_boost
scores.append((candidate, matches))
scores.sort(key=lambda x: x[1], reverse=True)
if len(scores) > 1 and scores[0][1] > scores[1][1]:
logger.debug(
f"Enhanced - Winner: {scores[0][0]} (score: {scores[0][1]:.2f}) "
f"vs runner-up: {scores[1][0]} (score: {scores[1][1]:.2f})"
)
return scores[0][0]
return None
def test_device_controller_disambiguation():
"""Test the exact scenario from the production bug."""
print("\n" + "="*80)
print("TEST: DeviceController Disambiguation")
print("="*80)
# Real data from production logs
llm_id = "DeviceController"
module_name = "openframe-api-service"
candidates = [
"deps.openframe-oss-lib.openframe-api-service-core.src.main.java.com.openframe.api.controller.DeviceController.DeviceController",
"deps.openframe-oss-lib.openframe-external-api-service-core.src.main.java.com.openframe.external.controller.DeviceController.DeviceController"
]
print(f"\nContext:")
print(f" LLM returned: '{llm_id}'")
print(f" Module: '{module_name}'")
print(f" Candidates:")
for i, c in enumerate(candidates, 1):
print(f" {i}. {c}")
# Test original implementation (should fail)
print(f"\n{'-'*80}")
print("ORIGINAL Implementation (no module context):")
print(f"{'-'*80}")
original_result = _find_best_path_match_original(llm_id, candidates)
if original_result is None:
print("❌ Result: None (AMBIGUOUS - failed to resolve)")
else:
print(f"✅ Result: {original_result}")
# Test enhanced implementation (should succeed)
print(f"\n{'-'*80}")
print("ENHANCED Implementation (with module context):")
print(f"{'-'*80}")
enhanced_result = _find_best_path_match_enhanced(llm_id, candidates, module_name)
if enhanced_result is None:
print("❌ Result: None (AMBIGUOUS)")
else:
print(f"✅ Result: {enhanced_result}")
# Verify correct match
expected = candidates[0] # Should match openframe-api-service, not openframe-external-api-service
if enhanced_result == expected:
print("✅ CORRECT: Matched to openframe-api-service variant (expected)")
else:
print("❌ WRONG: Matched to openframe-external-api-service variant (unexpected)")
return original_result, enhanced_result
def test_external_api_disambiguation():
"""Test disambiguation for openframe-external-api-service module."""
print("\n" + "="*80)
print("TEST: DeviceController for External API Service")
print("="*80)
llm_id = "DeviceController"
module_name = "openframe-external-api-service"
candidates = [
"deps.openframe-oss-lib.openframe-api-service-core.src.main.java.com.openframe.api.controller.DeviceController.DeviceController",
"deps.openframe-oss-lib.openframe-external-api-service-core.src.main.java.com.openframe.external.controller.DeviceController.DeviceController"
]
print(f"\nContext:")
print(f" LLM returned: '{llm_id}'")
print(f" Module: '{module_name}'")
print(f"\n{'-'*80}")
print("ENHANCED Implementation (should match external variant):")
print(f"{'-'*80}")
enhanced_result = _find_best_path_match_enhanced(llm_id, candidates, module_name)
if enhanced_result is None:
print("❌ Result: None (AMBIGUOUS)")
else:
print(f"✅ Result: {enhanced_result}")
# Verify correct match
expected = candidates[1] # Should match openframe-external-api-service
if enhanced_result == expected:
print("✅ CORRECT: Matched to openframe-external-api-service variant (expected)")
else:
print("❌ WRONG: Matched to openframe-api-service variant (unexpected)")
return enhanced_result
def test_security_config_disambiguation():
"""Test disambiguation for SecurityConfig (another common ambiguous case)."""
print("\n" + "="*80)
print("TEST: SecurityConfig Disambiguation")
print("="*80)
llm_id = "SecurityConfig"
module_name = "openframe-gateway-service"
candidates = [
"deps.openframe-oss-lib.openframe-gateway-service-core.src.main.java.com.openframe.gateway.config.SecurityConfig.SecurityConfig",
"deps.openframe-oss-lib.openframe-api-service-core.src.main.java.com.openframe.api.config.SecurityConfig.SecurityConfig",
"deps.openframe-oss-lib.openframe-auth-service-core.src.main.java.com.openframe.auth.config.SecurityConfig.SecurityConfig"
]
print(f"\nContext:")
print(f" LLM returned: '{llm_id}'")
print(f" Module: '{module_name}'")
print(f" Candidates:")
for i, c in enumerate(candidates, 1):
print(f" {i}. {c}")
print(f"\n{'-'*80}")
print("ENHANCED Implementation:")
print(f"{'-'*80}")
enhanced_result = _find_best_path_match_enhanced(llm_id, candidates, module_name)
if enhanced_result is None:
print("❌ Result: None (AMBIGUOUS)")
else:
print(f"✅ Result: {enhanced_result}")
# Verify correct match
expected = candidates[0] # Should match openframe-gateway-service
if enhanced_result == expected:
print("✅ CORRECT: Matched to openframe-gateway-service variant (expected)")
else:
print("❌ WRONG: Did not match gateway variant")
return enhanced_result
def main():
print("\n" + "="*80)
print("CodeWiki Module Disambiguation Enhancement Test")
print("="*80)
print("\nThis test demonstrates the fix for ambiguous component resolution")
print("by using module name context to disambiguate candidates.")
# Run all tests
tests_passed = 0
tests_failed = 0
# Test 1: DeviceController for openframe-api-service
orig_result, enh_result = test_device_controller_disambiguation()
if orig_result is None and enh_result is not None:
tests_passed += 1
print("\n✅ Test 1 PASSED: Original failed (ambiguous), Enhanced succeeded")
else:
tests_failed += 1
print("\n❌ Test 1 FAILED")
# Test 2: DeviceController for openframe-external-api-service
ext_result = test_external_api_disambiguation()
if ext_result and "external" in ext_result:
tests_passed += 1
print("\n✅ Test 2 PASSED: Correctly matched external variant")
else:
tests_failed += 1
print("\n❌ Test 2 FAILED")
# Test 3: SecurityConfig for openframe-gateway-service
sec_result = test_security_config_disambiguation()
if sec_result and "gateway" in sec_result:
tests_passed += 1
print("\n✅ Test 3 PASSED: Correctly matched gateway variant")
else:
tests_failed += 1
print("\n❌ Test 3 FAILED")
# Summary
print("\n" + "="*80)
print("Test Summary")
print("="*80)
print(f"✅ Passed: {tests_passed}/3")
print(f"❌ Failed: {tests_failed}/3")
if tests_failed == 0:
print("\n🎉 All tests passed! Module disambiguation is working correctly.")
return 0
else:
print(f"\n⚠️ {tests_failed} test(s) failed. Please review the implementation.")
return 1
if __name__ == "__main__":
sys.exit(main())