Date: 2026-02-06
Location: /Users/michaelassraf/Documents/GitHub/CodeWiki/codewiki/src/be/cluster_modules.py
Lines Tested: 352-376
Result: ✅ 10/10 validation tests passed
The NEW json.loads() validation code added around line 338 in cluster_modules.py:
# CRITICAL: Validate all component IDs are integers
max_id = len(id_to_fqdn) - 1
for module_name, module_info in module_tree.items():
if "components" not in module_info:
continue
component_ids = module_info["components"]
invalid_ids = []
for comp_id in component_ids:
# Check if ID is an integer
if not isinstance(comp_id, int):
invalid_ids.append(f"{comp_id} (type: {type(comp_id).__name__})")
# Check if ID is in valid range
elif comp_id < 0 or comp_id > max_id:
invalid_ids.append(f"{comp_id} (out of range 0-{max_id})")
if invalid_ids:
logger.error(f"❌ Module '{module_name}' contains invalid component IDs:")
logger.error(f" Invalid IDs: {invalid_ids}")
logger.error(f" Expected: Integers in range 0-{max_id}")
logger.error(f" LLM ignored instructions and returned non-integer IDs!")
return {}Input: [0, 1, 2]
Expected: PASS
Actual: PASS
Result: All IDs are integers in valid range
Input: ["0", "1", "2"]
Expected: FAIL with type error
Actual: FAIL
Error: Module 'auth_module' contains invalid component IDs: ['0 (type: str)', '1 (type: str)', '2 (type: str)']. Expected: Integers in range 0-9
Input: ["AuthService", "UserController"]
Expected: FAIL with type error
Actual: FAIL
Error: Module 'auth_module' contains invalid component IDs: ['AuthService (type: str)', 'UserController (type: str)']. Expected: Integers in range 0-9
Input: [0, 999]
Expected: FAIL with range error
Actual: FAIL
Error: Module 'auth_module' contains invalid component IDs: ['999 (out of range 0-9)']. Expected: Integers in range 0-9
Input: [0, "1", 2]
Expected: FAIL with type error
Actual: FAIL
Error: Module 'auth_module' contains invalid component IDs: ['1 (type: str)']. Expected: Integers in range 0-9
Input: [-1, 0, 1]
Expected: FAIL with range error
Actual: FAIL
Error: Module 'auth_module' contains invalid component IDs: ['-1 (out of range 0-9)']. Expected: Integers in range 0-9
Input: [0.0, 1.0, 2.0]
Expected: FAIL with type error
Actual: FAIL
Error: Module 'auth_module' contains invalid component IDs: ['0.0 (type: float)', '1.0 (type: float)', '2.0 (type: float)']. Expected: Integers in range 0-9
Input: []
Expected: PASS
Actual: PASS
Result: All IDs are integers in valid range
Input: { "path": "src/auth" } (no components key)
Expected: PASS
Actual: PASS
Result: All IDs are integers in valid range
Input: Malformed JSON with missing bracket
Expected: FAIL with JSON parse error
Actual: FAIL
Error: Invalid JSON in LLM response: Expecting ',' delimiter: line 6 column 3 (char 75)
✅ Type checking catches quoted integers and strings ✅ Range checking catches out-of-bounds IDs ✅ Error messages clearly state what's wrong and what's expected ✅ Valid cases pass without errors
-
Type Validation:
isinstance(comp_id, int)successfully catches:- Quoted integers (
"0","1") - String class names (
"AuthService") - Float values (
0.0,1.0)
- Quoted integers (
-
Range Validation: Successfully catches:
- Negative IDs (
-1) - Out-of-bounds IDs (
999when max is9)
- Negative IDs (
-
Error Reporting: All error messages include:
- Invalid value with actual type
- Expected range (0-max_id)
- Clear actionable message
-
Edge Cases: Handles:
- Empty component arrays
- Missing component keys
- Malformed JSON
- Mixed valid/invalid IDs
This validation logic prevents the ID-based clustering system from accepting invalid LLM responses, ensuring:
- No fuzzy string matching needed (replaced 200+ lines of code)
- LLM responses are strictly validated before normalization
- Clear error messages help debug prompt issues
- System fails fast on invalid data instead of producing incorrect results
Location: /Users/michaelassraf/Documents/GitHub/CodeWiki/tests/test_cluster_validation.py
Run with:
cd /Users/michaelassraf/Documents/GitHub/CodeWiki
python3 tests/test_cluster_validation.py🎉 All 10 validation tests passed!
The validation logic is working correctly and provides robust protection against invalid LLM responses. The system will now:
- Only accept integer component IDs
- Verify all IDs are in valid range
- Provide clear error messages when validation fails
- Fail early before attempting normalization