Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(health): repeated_nesting becomes advisory (#4757)

Raghav Chamadiya7655e769c460
diff --git a/docs/architecture/code-health.md b/docs/architecture/code-health.md
index 9cf0f56fe..86bbc1899 100644
--- a/docs/architecture/code-health.md
+++ b/docs/architecture/code-health.md
@@ -110,11 +110,11 @@ divide by what each is permitted to affect:
| Group | Count | Scores into |
|---|---:|---|
-| Defect-scoring | 24 | `defect` (10 of them also `maintainability`) |
+| Defect-scoring | 23 | `defect` (9 of them also `maintainability`) |
| Performance | 22 | `performance` only |
| SQL | 3 | `maintainability` only |
| Governance | 3 | nothing: the finding surfaces, the score is untouched |
-| Advisory | 10 | nothing: measured by construction, out of impact-ranked lists unless requested |
+| Advisory | 11 | nothing: measured by construction, out of impact-ranked lists unless requested |
The authority is `scoring._BIOMARKER_DIMENSIONS`. An unlisted biomarker defaults into
`defect`, so every `sql_*` and performance name is listed explicitly: an omission would
@@ -130,7 +130,7 @@ the defect weight (§6.1), "maint" the maintainability and "perf" the performanc
| `low_cohesion` | Structural | A class split into unrelated method groups | LCOM4 2 and 5 methods; high at LCOM4 3 or 20 methods; critical at LCOM4 4 and 15 methods | 0.5; maint 1.0 |
| `god_class` | Structural | A large class hiding a brain method | 200 NLOC, 15 methods, one method at NLOC 70 and CCN 9; high at 300 NLOC or 20 methods; critical at 400 and 25 | 1.13; maint 1.0 |
| `nested_complexity` | Structural | Deep nesting in a function | Judged depth 4; high at 5; critical at 7 | 1.34; maint 1.0 |
-| `repeated_nesting` | Structural | Several blocks at one nesting depth | 3 bumps (low) and judged CCN 5; medium at 4 bumps, high at 5 | 0.5; maint 1.0 |
+| `repeated_nesting` | Structural | Several blocks at one nesting depth | 3 bumps (low) and judged CCN 5; medium at 4 bumps, high at 5 | Advisory |
| `complex_conditional` | Structural | A boolean expression with many operators | 3 operators (low); medium 4, high 5, critical 6; a flat chain (one operator, no negation) is not flagged | 1.33; maint 1.0 |
| `complex_method` | Size | High cyclomatic complexity | Judged CCN 9; high 15; critical 25 | 1.21; maint 1.0 |
| `large_method` | Size | A long function | 60 code lines (low) and judged CCN 3; medium 90, high 120, critical 200 | 1.25; maint 1.0 |
@@ -323,6 +323,8 @@ language because it does not transfer:
repositories no earlier health study used found the score non-inferior without it.
- `brain_method`: fires only where `complex_method` and `large_method` already charge;
dropping it left defect AUC unchanged on the 66-repo corpus.
+- `repeated_nesting`: 29% in a blind study (7 of 24 findings), mostly short functions
+ with a few simple loops; dropping it with four other weak markers cost 0.004 defect AUC.
- `untested_complexity`: complexity and coverage already deduct elsewhere
([user guide](../layers/TEST_INTELLIGENCE.md#untested-complexity)).
- The four `apex_*` markers: every finding on 13 Salesforce repos hand-read, none
@@ -372,7 +374,7 @@ NLOC as a control fits each marker's lift beyond size, and only the constants sh
- **Prior weights kept:** `untested_hotspot`, `churn_risk`, `code_age_volatility`, which
are coverage-dependent or rare, so the corpus could not fairly measure them.
- **Floored at 0.5:** `developer_congestion`, `dry_violation`, `low_cohesion`,
- `primitive_obsession`, `repeated_nesting` fire widely but proved weak under
+ `primitive_obsession` fire widely but proved weak under
leakage-free scoring; `knowledge_loss` is de-rated to 0.4, the weakest.
`error_handling` is not fitted.
diff --git a/docs/layers/CODE_HEALTH.md b/docs/layers/CODE_HEALTH.md
index 548862f72..785d8365d 100644
--- a/docs/layers/CODE_HEALTH.md
+++ b/docs/layers/CODE_HEALTH.md
@@ -147,7 +147,7 @@ Every id with its threshold and weight:
| Family | Detectors | Cap | Catches |
|---|---:|---|---|
-| Structural complexity | 6 | 2.5 | Deep nesting, brain methods (listed, not scored), god classes, low cohesion, repeated nesting, complex conditions |
+| Structural complexity | 6 | 2.5 | Deep nesting, god classes, low cohesion, complex conditions; brain methods and repeated nesting are listed, not scored |
| Size and complexity | 3 | 1.5 | Long or high-CCN functions, primitive obsession |
| Duplication | 1 | 1.0 | Cloned blocks |
| Error handling | 1 | 0.5 | Swallowed or catch-all handlers, `unwrap`/panic, discarded Go errors |
diff --git a/packages/core/src/repowise/core/analysis/health/scoring.py b/packages/core/src/repowise/core/analysis/health/scoring.py
index 94b3a9704..74689e989 100644
--- a/packages/core/src/repowise/core/analysis/health/scoring.py
+++ b/packages/core/src/repowise/core/analysis/health/scoring.py
@@ -165,7 +165,6 @@ _BIOMARKER_WEIGHT_MULTIPLIER: dict[str, float] = {
# --- floored: fired widely but weak / non-predictive at T0 ---
"developer_congestion": 0.5, # was 1.5 - the HEAD-leakage hero; weak under T0
"low_cohesion": 0.5,
- "repeated_nesting": 0.5,
"primitive_obsession": 0.5,
"dry_violation": 0.5,
"knowledge_loss": 0.4, # confirmed weak-negative since Phase 1
@@ -286,7 +285,6 @@ _BIOMARKER_DIMENSIONS: dict[str, set[str]] = {
# maintainability problem whether or not it is also a defect predictor.
"complex_method": {"defect", "maintainability"},
"complex_conditional": {"defect", "maintainability"},
- "repeated_nesting": {"defect", "maintainability"},
# Performance-risk detectors contribute to ``performance`` ONLY. They are
# NOT defect predictors and must never move the surfaced (defect) score -
# that exclusion is what keeps the defect golden guarantee intact.
@@ -333,6 +331,10 @@ _BIOMARKER_DIMENSIONS: dict[str, set[str]] = {
# Fires only beside complex_method and large_method on the same function, so
# scoring it charged that function twice; dropping it moved no defect AUC.
"brain_method": {ADVISORY_DIMENSION},
+ # A blind precision study judged 7 of 24 findings real: it fires on short
+ # functions with a few simple loops. Dropping it with four other weak markers
+ # cost 0.004 defect AUC.
+ "repeated_nesting": {ADVISORY_DIMENSION},
# Its two inputs already deduct: complexity through complex_method, missing
# coverage through coverage_gap and coverage_gradient. Uncalibrated; it ranks
# which complex functions most need tests rather than moving the score.
@@ -361,7 +363,6 @@ _MAINTAINABILITY_WEIGHT_MULTIPLIER: dict[str, float] = {
"nested_complexity": 1.0,
"complex_method": 1.0,
"complex_conditional": 1.0,
- "repeated_nesting": 1.0,
# SQL smells ship advisory (0.7) pending a precision spot-check on a
# migrations-heavy corpus, mirroring how new perf markers land.
"sql_high_complexity": 0.7,
@@ -377,7 +378,6 @@ _MAINTAINABILITY_CATEGORY: dict[str, str] = {
"nested_complexity": "structural_complexity",
"large_method": "structural_complexity",
"complex_conditional": "structural_complexity",
- "repeated_nesting": "structural_complexity",
"complex_method": "size_and_complexity",
"primitive_obsession": "size_and_complexity",
"dry_violation": "duplication",
@@ -570,6 +570,7 @@ _ADVISORY_HOME: frozenset[str] = frozenset(
"mock_saturated_test",
"hidden_coupling",
"brain_method",
+ "repeated_nesting",
"untested_complexity",
"apex_soql_injection",
"apex_missing_sharing",
diff --git a/packages/ui/src/health/biomarker-glossary.ts b/packages/ui/src/health/biomarker-glossary.ts
index 68a46cb29..cbc7fd7b2 100644
--- a/packages/ui/src/health/biomarker-glossary.ts
+++ b/packages/ui/src/health/biomarker-glossary.ts
@@ -86,7 +86,7 @@ export const BIOMARKER_GLOSSARY: Record<string, BiomarkerInfo> = {
label: BIOMARKER_LABELS.repeated_nesting,
category: "structural_complexity",
description:
- "A function with several separate nested blocks stitched together. No single block is bad, but the whole reads as a sequence of mini-functions.",
+ "A function with several separate nested blocks stitched together. No single block is bad, but the whole reads as a sequence of mini-functions. Advisory: it costs this file no points.",
},
complex_method: {
label: BIOMARKER_LABELS.complex_method,
@@ -481,6 +481,7 @@ export const ADVISORY_HOME_BIOMARKERS: ReadonlySet<string> = new Set([
"mock_saturated_test",
"hidden_coupling",
"brain_method",
+ "repeated_nesting",
"untested_complexity",
"apex_soql_injection",
"apex_missing_sharing",
diff --git a/tests/unit/health/test_advisory_dimension.py b/tests/unit/health/test_advisory_dimension.py
index bcc304428..7a74c7b36 100644
--- a/tests/unit/health/test_advisory_dimension.py
+++ b/tests/unit/health/test_advisory_dimension.py
@@ -101,6 +101,14 @@ def test_brain_method_is_advisory():
assert deductions[2] == 0.0
+def test_repeated_nesting_is_advisory():
+ """Low precision in a blind study; still detected, never deducted."""
+ assert is_advisory("repeated_nesting")
+ scores, deductions = score_file([_result("repeated_nesting", Severity.CRITICAL)])
+ assert scores["defect"] == scores["maintainability"] == 10.0
+ assert deductions == [0.0]
+
+
def test_advisory_findings_do_not_dilute_a_real_deduction():
"""Adding an advisory finding to a scored one leaves the score alone."""
scored_only, _ = score_file([_result("low_cohesion")])
diff --git a/tests/unit/health/test_scoring_dimensions.py b/tests/unit/health/test_scoring_dimensions.py
index 642173d32..240be0dfb 100644
--- a/tests/unit/health/test_scoring_dimensions.py
+++ b/tests/unit/health/test_scoring_dimensions.py
@@ -225,11 +225,11 @@ def test_maintainability_dimension_matches_golden():
def test_code_shape_markers_reach_maintainability():
"""A method too complex to follow is a maintainability problem.
- These three are calibrated defect predictors and were scored as nothing
- else, which left the pillar a reader watches while refactoring blind to the
- most common thing a refactor removes.
+ These are calibrated defect predictors and were scored as nothing else,
+ which left the pillar a reader watches while refactoring blind to the most
+ common thing a refactor removes.
"""
- for marker in ("complex_method", "complex_conditional", "repeated_nesting"):
+ for marker in ("complex_method", "complex_conditional"):
scores, _ = score_file([_r(marker, Severity.CRITICAL)])
assert scores["maintainability"] < 10.0, marker
diff --git a/tests/unit/health/test_scoring_snapshot.py b/tests/unit/health/test_scoring_snapshot.py
index 2dfebbdbb..659c52397 100644
--- a/tests/unit/health/test_scoring_snapshot.py
+++ b/tests/unit/health/test_scoring_snapshot.py
@@ -70,7 +70,6 @@ _EXPECTED_BIOMARKER_WEIGHT_MULTIPLIER = {
# floored - fired widely but weak/non-predictive at T0
"developer_congestion": 0.5,
"low_cohesion": 0.5,
- "repeated_nesting": 0.5,
"primitive_obsession": 0.5,
"dry_violation": 0.5,
"knowledge_loss": 0.4,