Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(health): stop flagging flat boolean chains as compound conditions (#4754)
Raghav Chamadiyaee71c4c60e13
diff --git a/docs/architecture/code-health.md b/docs/architecture/code-health.mdindex bc02653a2..f7a3e1b9b 100644--- a/docs/architecture/code-health.md+++ b/docs/architecture/code-health.md@@ -131,7 +131,7 @@ the defect weight (§6.1), "maint" the maintainability and "perf" the performanc| `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 |-| `complex_conditional` | Structural | A boolean expression with many operators | 3 operators (low); medium 4, high 5, critical 6; a flat chain stays low | 1.33; maint 1.0 |+| `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 || `primitive_obsession` | Size | A long list of scalar parameters | 5 parameters (constructors 7), over half the typed ones primitive, file at 60 NLOC, not a test (low); medium at threshold + 2, high at + 4 | 0.5; maint 1.0 |diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/complex_conditional.py b/packages/core/src/repowise/core/analysis/health/biomarkers/complex_conditional.pyindex bc08a31af..e587caaeb 100644--- a/packages/core/src/repowise/core/analysis/health/biomarkers/complex_conditional.py+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/complex_conditional.py@@ -8,8 +8,8 @@ conditions are routinely cited in defect-mining studies as the noisyend of a branch's lifetime.A flat chain of one operator with no negation (``a || b || c || d``)-reads as a list of alternatives, not a knot, so it stays LOW however-long it runs; ``details.flat_chain`` says so.+reads as a list of alternatives, not a knot, so it produces no finding+however long it runs.Category: ``structural_complexity``.@@ -46,23 +46,18 @@ class ComplexConditionalDetector:out: list[BiomarkerResult] = []for fc in ctx.all_functions:for cond in fc.complex_conditions or []:- if cond.operator_count < _LOW:+ if cond.operator_count < _LOW or cond.flat_chain:continueout.append(BiomarkerResult(biomarker_type=self.name,- severity=(- Severity.LOW- if cond.flat_chain- else _severity_for(cond.operator_count)- ),+ severity=_severity_for(cond.operator_count),function_name=fc.name,line_start=cond.line,line_end=cond.line,details={"operator_count": cond.operator_count,"enclosing_construct": cond.enclosing_construct,- "flat_chain": cond.flat_chain,"dispatch_share": fc.dispatch_share,},reason=(diff --git a/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py b/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.pyindex 8832dcc28..5cdef3ccb 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py@@ -255,7 +255,7 @@ def _enclosing_construct(node: Node, lmap: LanguageNodeMap) -> str:return "if"-def _condition_subtrees(node: Node) -> list[Node]:+def _condition_subtrees(node: Node, lmap: LanguageNodeMap) -> list[Node]:"""Best-effort: pull the *condition* parts out of a branch/loop node.Prefers the tree-sitter ``condition`` named field where exposed@@ -266,6 +266,11 @@ def _condition_subtrees(node: Node) -> list[Node]:cond = node.child_by_field_name("condition")if cond is not None:return [cond]+ # Python's ``X if C else Y`` names no fields: only the middle child is+ # the condition, so the arms' operators are not counted.+ named = node.named_children+ if lmap.ternary_arms_positional and node.type in lmap.flat_ternary_kinds and len(named) == 3:+ return [named[1]]# Switch case value (TS, Java)value = node.child_by_field_name("value")if value is not None and "case" in node.type:@@ -468,7 +473,7 @@ def _walk_function_body(op_count = 0op_kinds: set[str] = set()negated = False- for sub in _condition_subtrees(node):+ for sub in _condition_subtrees(node, lmap):sub_count, sub_kinds, sub_negated = _count_boolean_ops_in_condition(sub, lmap)op_count += sub_countop_kinds |= sub_kindsdiff --git a/tests/unit/health/golden/health_engine.json b/tests/unit/health/golden/health_engine.jsonindex 0070a2b3d..7c7fc4d5e 100644--- a/tests/unit/health/golden/health_engine.json+++ b/tests/unit/health/golden/health_engine.json@@ -1226,7 +1226,7 @@},{"file_path": "go/conditionals.go",- "score": 9.24,+ "score": 10.0,"max_ccn": 8,"max_nesting": 1,"nloc": 19,@@ -1235,10 +1235,10 @@"duplication_pct": null,"line_coverage_pct": null,"branch_coverage_pct": null,- "defect_score": 9.24,- "maintainability_score": 9.4,+ "defect_score": 10.0,+ "maintainability_score": 10.0,"performance_score": 10.0,- "structure_deduction": 0.758,+ "structure_deduction": 0.0,"history_deduction": 0.0,"is_test": false,"code_origin": "production"@@ -1283,7 +1283,7 @@},{"file_path": "java/Conditionals.java",- "score": 9.24,+ "score": 10.0,"max_ccn": 8,"max_nesting": 1,"nloc": 20,@@ -1292,10 +1292,10 @@"duplication_pct": null,"line_coverage_pct": null,"branch_coverage_pct": null,- "defect_score": 9.24,- "maintainability_score": 9.4,+ "defect_score": 10.0,+ "maintainability_score": 10.0,"performance_score": 10.0,- "structure_deduction": 0.758,+ "structure_deduction": 0.0,"history_deduction": 0.0,"is_test": false,"code_origin": "production"@@ -1682,7 +1682,7 @@},{"file_path": "python/conditionals.py",- "score": 9.24,+ "score": 10.0,"max_ccn": 8,"max_nesting": 1,"nloc": 12,@@ -1691,10 +1691,10 @@"duplication_pct": null,"line_coverage_pct": null,"branch_coverage_pct": null,- "defect_score": 9.24,- "maintainability_score": 9.4,+ "defect_score": 10.0,+ "maintainability_score": 10.0,"performance_score": 10.0,- "structure_deduction": 0.758,+ "structure_deduction": 0.0,"history_deduction": 0.0,"is_test": false,"code_origin": "production"@@ -1853,7 +1853,7 @@},{"file_path": "rust/conditionals.rs",- "score": 9.24,+ "score": 10.0,"max_ccn": 8,"max_nesting": 1,"nloc": 18,@@ -1862,10 +1862,10 @@"duplication_pct": null,"line_coverage_pct": null,"branch_coverage_pct": null,- "defect_score": 9.24,- "maintainability_score": 9.4,+ "defect_score": 10.0,+ "maintainability_score": 10.0,"performance_score": 10.0,- "structure_deduction": 0.758,+ "structure_deduction": 0.0,"history_deduction": 0.0,"is_test": false,"code_origin": "production"@@ -2176,7 +2176,7 @@},{"file_path": "typescript/conditionals.ts",- "score": 9.24,+ "score": 10.0,"max_ccn": 8,"max_nesting": 1,"nloc": 26,@@ -2185,10 +2185,10 @@"duplication_pct": null,"line_coverage_pct": null,"branch_coverage_pct": null,- "defect_score": 9.24,- "maintainability_score": 9.4,+ "defect_score": 10.0,+ "maintainability_score": 10.0,"performance_score": 10.0,- "structure_deduction": 0.758,+ "structure_deduction": 0.0,"history_deduction": 0.0,"is_test": false,"code_origin": "production"@@ -3136,44 +3136,6 @@"dimension": "advisory","id": "finding_c1cb731e126a1224c410"},- {- "biomarker_type": "complex_conditional",- "severity": "low",- "file_path": "go/conditionals.go",- "function_name": "ThreeOps",- "line_start": 4,- "line_end": 4,- "details": {- "operator_count": 3,- "enclosing_construct": "if",- "flat_chain": true,- "dispatch_share": 0.0,- "symbol_line": 3- },- "health_impact": 0.379,- "reason": "if condition combines 3 boolean operators",- "dimension": "defect",- "id": "finding_b4706faf8eea57f3c000"- },- {- "biomarker_type": "complex_conditional",- "severity": "low",- "file_path": "go/conditionals.go",- "function_name": "SixOps",- "line_start": 11,- "line_end": 11,- "details": {- "operator_count": 6,- "enclosing_construct": "for",- "flat_chain": true,- "dispatch_share": 0.0,- "symbol_line": 10- },- "health_impact": 0.379,- "reason": "for condition combines 6 boolean operators",- "dimension": "defect",- "id": "finding_ed03ae737c057c537559"- },{"biomarker_type": "nested_complexity","severity": "high",@@ -3427,44 +3389,6 @@"dimension": "advisory","id": "finding_fd14e71841abee247de5"},- {- "biomarker_type": "complex_conditional",- "severity": "low",- "file_path": "java/Conditionals.java",- "function_name": "threeOps",- "line_start": 3,- "line_end": 3,- "details": {- "operator_count": 3,- "enclosing_construct": "if",- "flat_chain": true,- "dispatch_share": 0.0,- "symbol_line": 2- },- "health_impact": 0.379,- "reason": "if condition combines 3 boolean operators",- "dimension": "defect",- "id": "finding_92b0b6c1f3be5fe497d3"- },- {- "biomarker_type": "complex_conditional",- "severity": "low",- "file_path": "java/Conditionals.java",- "function_name": "sixOps",- "line_start": 10,- "line_end": 10,- "details": {- "operator_count": 6,- "enclosing_construct": "while",- "flat_chain": true,- "dispatch_share": 0.0,- "symbol_line": 9- },- "health_impact": 0.379,- "reason": "while condition combines 6 boolean operators",- "dimension": "defect",- "id": "finding_13a165fcb9f91a97a666"- },{"biomarker_type": "nested_complexity","severity": "high",@@ -3992,44 +3916,6 @@"dimension": "advisory","id": "finding_9ce0eaced1fca0186c01"},- {- "biomarker_type": "complex_conditional",- "severity": "low",- "file_path": "python/conditionals.py",- "function_name": "three_ops",- "line_start": 2,- "line_end": 2,- "details": {- "operator_count": 3,- "enclosing_construct": "if",- "flat_chain": true,- "dispatch_share": 0.0,- "symbol_line": 1- },- "health_impact": 0.379,- "reason": "if condition combines 3 boolean operators",- "dimension": "defect",- "id": "finding_c86a6881aaec275e9ff1"- },- {- "biomarker_type": "complex_conditional",- "severity": "low",- "file_path": "python/conditionals.py",- "function_name": "six_ops",- "line_start": 8,- "line_end": 8,- "details": {- "operator_count": 6,- "enclosing_construct": "while",- "flat_chain": true,- "dispatch_share": 0.0,- "symbol_line": 7- },- "health_impact": 0.379,- "reason": "while condition combines 6 boolean operators",- "dimension": "defect",- "id": "finding_3ce2bf004e5ad8e70bed"- },{"biomarker_type": "nested_complexity","severity": "high",@@ -4668,44 +4554,6 @@"dimension": "advisory","id": "finding_7e8892978e25daab749c"},- {- "biomarker_type": "complex_conditional",- "severity": "low",- "file_path": "rust/conditionals.rs",- "function_name": "three_ops",- "line_start": 2,- "line_end": 2,- "details": {- "operator_count": 3,- "enclosing_construct": "if",- "flat_chain": true,- "dispatch_share": 0.0,- "symbol_line": 1- },- "health_impact": 0.379,- "reason": "if condition combines 3 boolean operators",- "dimension": "defect",- "id": "finding_5e2e7ed7cf94318d223b"- },- {- "biomarker_type": "complex_conditional",- "severity": "low",- "file_path": "rust/conditionals.rs",- "function_name": "six_ops",- "line_start": 9,- "line_end": 9,- "details": {- "operator_count": 6,- "enclosing_construct": "while",- "flat_chain": true,- "dispatch_share": 0.0,- "symbol_line": 8- },- "health_impact": 0.379,- "reason": "while condition combines 6 boolean operators",- "dimension": "defect",- "id": "finding_a4bb8ffa6c328be7035f"- },{"biomarker_type": "nested_complexity","severity": "high",@@ -5315,44 +5163,6 @@"dimension": "advisory","id": "finding_97168cde0d08fc10589a"},- {- "biomarker_type": "complex_conditional",- "severity": "low",- "file_path": "typescript/conditionals.ts",- "function_name": "threeOps",- "line_start": 2,- "line_end": 2,- "details": {- "operator_count": 3,- "enclosing_construct": "if",- "flat_chain": true,- "dispatch_share": 0.0,- "symbol_line": 1- },- "health_impact": 0.379,- "reason": "if condition combines 3 boolean operators",- "dimension": "defect",- "id": "finding_5da1fdb1c287eae427a0"- },- {- "biomarker_type": "complex_conditional",- "severity": "low",- "file_path": "typescript/conditionals.ts",- "function_name": "sixOps",- "line_start": 17,- "line_end": 17,- "details": {- "operator_count": 6,- "enclosing_construct": "while",- "flat_chain": true,- "dispatch_share": 0.0,- "symbol_line": 8- },- "health_impact": 0.379,- "reason": "while condition combines 6 boolean operators",- "dimension": "defect",- "id": "finding_35d87c20e1ce38c81d46"- },{"biomarker_type": "nested_complexity","severity": "high",@@ -5485,21 +5295,21 @@],"kpis": {"hotspot_health": null,- "average_health": 9.62,+ "average_health": 9.65,"worst_performer_path": "scala/nested.scala","worst_performer_score": 7.77,"worst_test_path": "apex/HealthMarkersTest.cls","worst_test_score": 10.0,"file_count": 71,- "maintainability_average": 9.65,+ "maintainability_average": 9.67,"maintainability_hotspot": null,"performance_average": 9.48,"performance_hotspot": null,- "structure_average": 0.39,+ "structure_average": 0.35,"structure_hotspot": null,"history_average": 0.0,"history_hotspot": null,- "production_average": 9.61,+ "production_average": 9.64,"production_file_count": 69,"unanalysed_file_count": 4,"analysed_share": 0.9467diff --git a/tests/unit/health/test_complex_conditional.py b/tests/unit/health/test_complex_conditional.pyindex 39003441f..df81b55bd 100644--- a/tests/unit/health/test_complex_conditional.py+++ b/tests/unit/health/test_complex_conditional.py@@ -45,22 +45,16 @@ def _find(results, name):def _assert_three_six_two(metrics, three: str, six: str, two: str):- findings = ComplexConditionalDetector().detect(_ctx(metrics))- by_fn = {f.function_name: f for f in findings}+ # The walker counts every condition; the 3-op and 6-op fixtures are+ # flat ``&&`` chains.+ for name, count in ((three, 3), (six, 6)):+ cond = _find(metrics, name).complex_conditions[0]+ assert cond.operator_count == count+ assert cond.flat_chain is True- # 3-op → flagged at LOW- assert three in by_fn, f"expected finding on {three} (3-op condition)"- assert by_fn[three].severity == Severity.LOW- assert by_fn[three].details["operator_count"] == 3-- # 6-op flat ``&&`` chain → flagged, but held at LOW- assert six in by_fn, f"expected finding on {six} (6-op condition)"- assert by_fn[six].severity == Severity.LOW- assert by_fn[six].details["operator_count"] == 6- assert by_fn[six].details["flat_chain"] is True-- # 2-op → not flagged- assert two not in by_fn, f"{two} (2-op) should not flag"+ # Flat chains and the 2-op condition produce no finding.+ by_fn = {f.function_name for f in ComplexConditionalDetector().detect(_ctx(metrics))}+ assert not by_fn & {three, six, two}def test_python_conditions():@@ -144,18 +138,56 @@ def test_flat_chain_python(condition: str, flat: bool):assert cond.flat_chain is flat-def test_flat_chain_holds_low_and_mixed_keeps_count_severity():+def test_flat_chain_has_no_finding_and_mixed_keeps_count_severity():src = ("function flat(a, b, c, d, e, g, h) { if (a || b || c || d || e || g || h) { return 1; } }\n""function knot(a, b, c, d, e, g, h) { if (a && b || c && !d || e && g || h) { return 1; } }\n""function mixedLow(a, b, c, d) { if (a && b || c && d) { return 1; } }\n"+ "function negated(a, b, c, d) { if (a || !b || c || d) { return 1; } }\n")results = walk_file_complexity("x.ts", "typescript", src.encode())if not results:pytest.skip("tree-sitter language pack missing for typescript")by_fn = {f.function_name: f for f in ComplexConditionalDetector().detect(_ctx(results))}- assert by_fn["flat"].severity == Severity.LOW- assert by_fn["flat"].details["operator_count"] == 6+ assert "flat" not in by_fnassert by_fn["knot"].severity == Severity.CRITICAL- assert by_fn["knot"].details["flat_chain"] is Falseassert by_fn["mixedLow"].severity == Severity.LOW+ assert by_fn["negated"].severity == Severity.LOW+++def test_python_flat_chain_has_no_finding_and_mixed_flags():+ src = (+ "def flat(a, b, c, d, e):\n if a or b or c or d or e:\n return 1\n\n"+ "def mixed(a, b, c, d):\n if a and b or c and d:\n return 1\n"+ )+ results = walk_file_complexity("x.py", "python", src.encode())+ if not results:+ pytest.skip("tree-sitter language pack missing for python")+ by_fn = {f.function_name: f for f in ComplexConditionalDetector().detect(_ctx(results))}+ assert set(by_fn) == {"mixed"}+ assert by_fn["mixed"].severity == Severity.LOW+++ ("body", "count"),+ [+ ("x = a and b if c or d else e or g", 1),+ ("x = (a and b and c) if d else (e or g)", 0),+ ("x = a if b and c or d and e else g", 3),+ ],+)+def test_python_ternary_counts_condition_only(body: str, count: int):+ src = f"def f(a, b, c, d, e, g):\n {body}\n return x\n"+ conds = _conditions(src, "python", "f").complex_conditions+ assert [c.operator_count for c in conds] == ([count] if count else [])+++def test_reason_operator_count_matches_details_and_code():+ # Three operators in the condition, one more in each arm.+ src = "def f(a, b, c, d, e, g):\n x = a or b if c and d or e and g else a and b\n return x\n"+ results = walk_file_complexity("x.py", "python", src.encode())+ if not results:+ pytest.skip("tree-sitter language pack missing for python")+ (finding,) = ComplexConditionalDetector().detect(_ctx(results))+ assert finding.details["operator_count"] == 3+ assert "combines 3 boolean operators" in finding.reason