Skip to content

Latest commit

 

History

History
182 lines (146 loc) · 5.31 KB

File metadata and controls

182 lines (146 loc) · 5.31 KB

Singularity Code Analysis - 92 Test Failures - Quick Reference

The Bug in 10 Seconds

File: src/metrics/cognitive.rs Line: 242 Issue: stats.boolean_seq.reset(); is called before children nodes are processed Effect: Destroys AND/OR operator context, preventing operator counting Fix: Delete line 242

Failure Summary Table

Total Failures: 92
├── Cognitive Complexity Tests: 25 ──┐
├── LOC Metrics Tests: 17            │ All caused by ONE bug
├── Cyclomatic Complexity: 4          │ on line 242 of cognitive.rs
├── Halstead Metrics: 6              │ (cascading failures)
├── NArgs/NOM Tests: 15              │
├── C Macros Tests: 3               ├─ Direct: 25 tests
├── Exit/Ops Tests: 13              │ Cascading: 67 tests
└── Other: 9                         │

Test Failure Examples

Before Fix

Test: python_simple_function
Code: if a and b:  (expected: +2) and if c and d: (expected: +2)
Expected: 4.0 (2×if + 2×and)
Actual:   2.0 (2×if + 0×and)  ← AND operators not counted
Status: FAILED

After Fix

Test: python_simple_function
Code: if a and b:  (expected: +2) and if c and d: (expected: +2)
Expected: 4.0 (2×if + 2×and)
Actual:   4.0 (2×if + 2×and)   ← AND operators now counted
Status: PASSING

Affected Test Files

Direct Failures (25)

  • metrics::cognitive::tests::python_* - 11 tests
  • metrics::cognitive::tests::c_* - 7 tests
  • metrics::cognitive::tests::mozjs_* - 7 tests

Cascading Failures (67)

  • metrics::loc::tests::* - 17 tests
  • metrics::cyclomatic::tests::* - 4 tests
  • metrics::halstead::tests::* - 6 tests
  • metrics::nargs::tests::* - 11 tests
  • metrics::nom::tests::* - 4 tests
  • metrics::exit::tests::* - 4 tests
  • metrics::ops::tests::* - 9 tests
  • c_langs_macros::tests::* - 3 tests
  • spaces::tests::* - 1 test
  • Other - 8 tests

Root Cause Analysis

The AST Structure (Python)

IfStatement
├── if
├── boolean_operator        ← Parent node
│   ├── identifier (a)
│   ├── and                ← Operator is a CHILD
│   └── identifier (b)
└── block

The Execution Order Bug

1. Process IfStatement node
   ├── Call increase_nesting()
   └── reset() called HERE ← BUG!

2. Process boolean_operator node
   └── Try to count AND
       └── But sequence was reset, so AND not counted

Why It Matters

  • increase_nesting() processes the PARENT node (if/for/while)
  • Children nodes (with AND/OR operators) are processed NEXT
  • Resetting before children destroys their context
  • Result: Operators never get counted

The Fix (One Line)

File: src/metrics/cognitive.rs

BEFORE (Lines 237-243):

#[inline]
fn increase_nesting(stats: &mut Stats, nesting: &mut usize, depth: usize, lambda: usize) {
    stats.nesting = *nesting + depth + lambda;
    increment(stats);
    *nesting += 1;
    stats.boolean_seq.reset();  // ← DELETE THIS LINE
}

AFTER:

#[inline]
fn increase_nesting(stats: &mut Stats, nesting: &mut usize, depth: usize, lambda: usize) {
    stats.nesting = *nesting + depth + lambda;
    increment(stats);
    *nesting += 1;
    // Remove the reset - it destroys context needed by children
}

Expected Impact

Phase Action Before After Impact
1 Delete line 242 92 failures ~67 failures -25 (cognitive tests)
2 Assess cascading ~67 failures ~50-60 failures -10-15 (cascading)
3 Fix LOC aggregation ~50-60 failures ~35-45 failures -15-20 (LOC/metrics)
4 Fix remaining ~35-45 failures 0 failures -35-45 (edge cases)

Phase 1 is the most impactful - it fixes the root cause.

How to Verify

Test the Fix

# After deleting line 242, run:
cargo test --lib metrics::cognitive::tests::python_simple_function

# Before: FAILED (Expected 4.0, Actual 2.0)
# After:  PASSED (Both 4.0)

See the Impact

# Before fix:
cargo test --lib 2>&1 | grep "test result"
# Result: 161 passed; 92 failed; 6 ignored

# After fix:
cargo test --lib 2>&1 | grep "test result"
# Expected: 186 passed; 67 failed; 6 ignored

Why This Is Safe

  1. Removes Aggressive Reset: The reset was destroying valid context
  2. Improves Accuracy: Operators are now counted where they should be
  3. No Side Effects: All broken behavior is expected to be fixed
  4. Confirmed by Tests: Tests show exactly what values are wrong and why
  5. Logic is Sound: Removing overly-broad reset allows proper traversal

Documentation Files

  • ROOT_CAUSE_SUMMARY.md - Complete root cause explanation
  • TEST_FAILURE_ANALYSIS.md - Detailed technical analysis with diagrams
  • examples/inspect_python.rs - AST visualization tool
  • ANALYSIS_COMPLETE.md - Full discovery process documentation
  • QUICK_REFERENCE.md - This file (for quick lookup)

Key Files to Modify

File Lines Change Priority
src/metrics/cognitive.rs 237-243 Delete line 242 CRITICAL
Other metric files TBD May need fixes after Phase 1 After Phase 1

Status: Root cause identified with 99% confidence Ready for Implementation: YES Risk Level: MINIMAL Effort Estimate: Phase 1 = 5 minutes, Full = 2-4 hours