Repository navigation
feat: add cognitive complexity analysis with GazeCRAP-CC (#270) - #294
Conversation
2ed40f2 to
cce6005
Compare
3af0370 to
8631033
Compare
…ce#270) - Add SonarSource cognitive complexity analyzer (internal/cognitive) - Add CognitiveComplexityProvider interface with Go implementation - Add GazeCRAP-CC variant (CC^2*(1-cov)^3 + CC) reported alongside CRAP - Add --max-cognitive-complexity gate flag to gaze crap - Add optional cognitive_complexity method to external analyzer protocol - Emit cognitive_complexity and gaze_crap_cc in JSON output Assisted-by: deepseek-v4-pro Generated with AI assistance (deepseek-v4-pro)
8631033 to
83febf9
Compare
jflowers
left a comment
There was a problem hiding this comment.
Note: Could not post as REQUEST_CHANGES due to self-review restriction (PR authored by current user). Posted as COMMENT instead. Original verdict: REQUEST_CHANGES.
Review Summary
6 findings (0 CRITICAL, 2 HIGH, 4 MEDIUM). Verdict: REQUEST CHANGES.
HIGH (blocks merge):
Summary.CognitiveComplexityExceededis dead code (internal/crap/analyze.go:418) — initialized but never incremented;buildSummarydoesn't receive the threshold.- JSON Schema (
internal/report/schema.go) not updated withcognitive_complexity/gaze_crap_cc, violatingcrap-scoring/spec.md:77-84.
MEDIUM:
3. --max-cognitive-complexity is only on gaze crap, not gaze analyze (design goal #3 names both).
4. printCISummary (cmd/gaze/main.go:885) omits the cognitive complexity gate — it takes maxCrapload, maxGazeCrapload but not maxCognitiveComplexity, so the CI summary won't report the gate.
5. Protocol Capabilities (internal/protocol/types.go:147) lacks a CognitiveComplexity bool; every other optional method (discover, test_mapping, classify_signals, streaming, doc_coverage) declares one for handshake negotiation.
6. Logical operator counting deviates from the Sonar spec (see inline comment on cognitive_test.go).
Local go build and go test -race -count=1 -short ./... pass; MegaLinter passes; CI unit/e2e tests still running.
This review was generated by /uf.review-pr (AI-assisted).
| } | ||
| } | ||
|
|
||
| func TestMixedLogicalThreeOperators(t *testing.T) { |
There was a problem hiding this comment.
[MEDIUM] Logical operator counting deviates from the Sonar spec. TestMixedLogicalThreeOperators expects a && b || c && d = 3, but the Sonar cognitive-complexity spec counts sequences of identical operators (one && sequence + one || sequence + one && sequence = 2 increments). The implementation increments on each operator change from its parent, yielding 3, which diverges from the stated Sonar-compatibility goal. Other Sonar-compatible tools report 2 for this expression.
There was a problem hiding this comment.
Declined with evidence. The implementation matches the SonarSource cognitive complexity specification, and the claimed value of 2 for a && b || c && d is incorrect.
- SonarSource's Cognitive Complexity whitepaper, Appendix B, gives
if (a && b && c || d || e && f)as +3 for logical operators — one increment per sequence of like operators, i.e. three sequences (&&,||,&&). - G. Ann Campbell (SonarSource) confirms the same rule directly:
a || b && c || d= 3, whereasa && b && c && d= 1. - The implementation's
walkExprincrements on each operator change (parentLogOp != e.Op), which fora && b || c && dproduces 3 (a&&run, then a||run, then a&&run). This matches thecognitive-complexitynpm reference.
The review's own arithmetic ("one && + one || + one && = 2 increments") is internally contradictory — three operator-change runs cannot sum to two increments. No code change; TestMixedLogicalThreeOperators is correct as written.
Extract the cognitive-complexity lookup inlined into Analyze into a helper (applyCognitiveComplexity), lowering Analyze's cyclomatic complexity below the GazeCRAP threshold so the CI gate (--max-gaze-crapload=6) passes. Assisted-by: deepseek-v4-pro Signed-off-by: Jay Flowers <[email protected]>
Pass the cognitive complexity threshold into buildSummary and increment CognitiveComplexityExceeded when a function's score exceeds it, instead of leaving the aggregate permanently zero. Addresses PR unbound-force#294 review feedback from @jflowers (finding #1). Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Declare cognitive_complexity and gaze_crap_cc on Score and cognitive_complexity_total/cognitive_complexity_exceeded on Summary so JSON output validates against the schema. Addresses PR unbound-force#294 review feedback from @jflowers (finding #2). Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Wire the Go cognitive complexity provider in the crap Go path, add the --max-cognitive-complexity flag to gaze analyze with exit-code gating, and surface the cognitive gate in printCISummary. Addresses PR unbound-force#294 review feedback from @jflowers (findings unbound-force#3 and unbound-force#4). Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Add the CognitiveComplexity bool to protocol.Capabilities, expose it on adapter.Providers, and wire the external cognitive complexity provider so external analyzers can supply cognitive complexity scores. Addresses PR unbound-force#294 review feedback from @jflowers (finding unbound-force#5). Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Address review-council REQUEST CHANGES findings on the cognitive complexity feedback fixes: - Summary.CognitiveComplexityTotal/Exceeded now *int (nil = unavailable) - checkCIThresholds reads the summary field (single source of truth); fails with "unavailable" when the gate is set but CC was not computed - printCISummary reports "unavailable" when CC was not computed - goprovider wraps cognitive.AnalyzeFile (removes duplicated loop) - protocol: 11 methods / 6 optional, version 1.2.0, docs/protocol.md - spec scopes "computed and reported" to crap/report; analyze is gate-only - drop dead max-cognitive-complexity term from self-check thresholdSet - add tests for gate boundaries, unavailable signal, and analyze gate Addresses PR unbound-force#294 review feedback from @jflowers. Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Address review-council round-2 REQUEST CHANGES findings: - Fix multi-package enumeration: cognitive provider now passes the original ./... wildcard through to packages.Load instead of collapsing it via the gocyclo-oriented ResolvePatterns (gate under-enforced root pkg) - Scope spec "computed and reported" to gaze crap only (analyze gate-only); remove over-claimed gaze report - runAnalyze gate: error on empty cognitive result instead of silent PASS - CognitiveFormula delegates to Formula (DRY) - Fix arithmetically wrong spec example values (110.0 / 8.125 / 3.0) - Add tests: CognitiveFormula, applyCognitiveComplexity, buildCognitiveLookup, convertCognitiveComplexity; fix import grouping Addresses PR unbound-force#294 review feedback from @jflowers. Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Address review-council round-3 REQUEST CHANGES findings: - Fix method name mismatch: cognitive provider now emits gocyclo-compatible receiver-qualified names ((*T).M / (T).M), so methods join correctly and are no longer silently dropped from cognitive/gaze_crap_cc output - Traverse switch/select case bodies (case clauses were never walked, under-counting control flow inside cases) - Detect method self-recursion via SelectorExpr (s.M()) - Correct spec else-if-chain scenario (else increments, value 4) - Make runAnalyze cognitive provider injectable for testing FAIL branches - Wrap packages.Load error with %w - Extend fake analyzer with cognitive_complexity capability + handler, bump protocol_version to 1.2.0 - Document --max-cognitive-complexity + GazeCRAP-CC in README - Correct tasks.md/design.md bookkeeping (defer text-report column, un-check 6.5) - Add tests: function-name format, case-body traversal, provider integration, runAnalyze gate FAIL branches, JSON omitempty, schema validation Addresses PR unbound-force#294 review feedback from @jflowers. Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Address review-council round-4 REQUEST CHANGES findings: - runAnalyze gate now evaluates before the empty-results early return, so --max-cognitive-complexity can no longer silently pass on zero functions - Provider integration test asserts exact cognitive complexity values (new cognitive fixture with hand-computed NestedIf=3) - External-analyzer cognitive path tested end-to-end: capability present yields provider + values (add/multiply/divide); --no-cognitive-complexity yields nil provider (graceful degradation) - External-analyzer crap test asserts cognitive_complexity/gaze_crap_cc Addresses PR unbound-force#294 review feedback from @jflowers. Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Correct task 6.2 (ExternalCognitiveComplexityProvider, not FetchCognitiveComplexity) and design D6 (*int → int threshold pattern). Addresses PR unbound-force#294 review feedback from @jflowers. Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
The external-analyzer cognitive round-trip test (TestSession_CognitiveComplexityCapability + TestCrapWithExternalAnalyzer cognitive assertions) was added in commit 45ef3d4. Addresses PR unbound-force#294 review feedback from @jflowers. Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
|
Addressed in |
|
Addressed in |
|
Addressed in |
Pass the cognitive complexity threshold into buildSummary and increment CognitiveComplexityExceeded when a function's score exceeds it, instead of leaving the aggregate permanently zero. Addresses PR #294 review feedback from @jflowers (finding #1). Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Declare cognitive_complexity and gaze_crap_cc on Score and cognitive_complexity_total/cognitive_complexity_exceeded on Summary so JSON output validates against the schema. Addresses PR #294 review feedback from @jflowers (finding #2). Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Wire the Go cognitive complexity provider in the crap Go path, add the --max-cognitive-complexity flag to gaze analyze with exit-code gating, and surface the cognitive gate in printCISummary. Addresses PR #294 review feedback from @jflowers (findings #3 and #4). Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Add the CognitiveComplexity bool to protocol.Capabilities, expose it on adapter.Providers, and wire the external cognitive complexity provider so external analyzers can supply cognitive complexity scores. Addresses PR #294 review feedback from @jflowers (finding #5). Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Address review-council REQUEST CHANGES findings on the cognitive complexity feedback fixes: - Summary.CognitiveComplexityTotal/Exceeded now *int (nil = unavailable) - checkCIThresholds reads the summary field (single source of truth); fails with "unavailable" when the gate is set but CC was not computed - printCISummary reports "unavailable" when CC was not computed - goprovider wraps cognitive.AnalyzeFile (removes duplicated loop) - protocol: 11 methods / 6 optional, version 1.2.0, docs/protocol.md - spec scopes "computed and reported" to crap/report; analyze is gate-only - drop dead max-cognitive-complexity term from self-check thresholdSet - add tests for gate boundaries, unavailable signal, and analyze gate Addresses PR #294 review feedback from @jflowers. Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Address review-council round-2 REQUEST CHANGES findings: - Fix multi-package enumeration: cognitive provider now passes the original ./... wildcard through to packages.Load instead of collapsing it via the gocyclo-oriented ResolvePatterns (gate under-enforced root pkg) - Scope spec "computed and reported" to gaze crap only (analyze gate-only); remove over-claimed gaze report - runAnalyze gate: error on empty cognitive result instead of silent PASS - CognitiveFormula delegates to Formula (DRY) - Fix arithmetically wrong spec example values (110.0 / 8.125 / 3.0) - Add tests: CognitiveFormula, applyCognitiveComplexity, buildCognitiveLookup, convertCognitiveComplexity; fix import grouping Addresses PR #294 review feedback from @jflowers. Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Address review-council round-3 REQUEST CHANGES findings: - Fix method name mismatch: cognitive provider now emits gocyclo-compatible receiver-qualified names ((*T).M / (T).M), so methods join correctly and are no longer silently dropped from cognitive/gaze_crap_cc output - Traverse switch/select case bodies (case clauses were never walked, under-counting control flow inside cases) - Detect method self-recursion via SelectorExpr (s.M()) - Correct spec else-if-chain scenario (else increments, value 4) - Make runAnalyze cognitive provider injectable for testing FAIL branches - Wrap packages.Load error with %w - Extend fake analyzer with cognitive_complexity capability + handler, bump protocol_version to 1.2.0 - Document --max-cognitive-complexity + GazeCRAP-CC in README - Correct tasks.md/design.md bookkeeping (defer text-report column, un-check 6.5) - Add tests: function-name format, case-body traversal, provider integration, runAnalyze gate FAIL branches, JSON omitempty, schema validation Addresses PR #294 review feedback from @jflowers. Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Address review-council round-4 REQUEST CHANGES findings: - runAnalyze gate now evaluates before the empty-results early return, so --max-cognitive-complexity can no longer silently pass on zero functions - Provider integration test asserts exact cognitive complexity values (new cognitive fixture with hand-computed NestedIf=3) - External-analyzer cognitive path tested end-to-end: capability present yields provider + values (add/multiply/divide); --no-cognitive-complexity yields nil provider (graceful degradation) - External-analyzer crap test asserts cognitive_complexity/gaze_crap_cc Addresses PR #294 review feedback from @jflowers. Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Correct task 6.2 (ExternalCognitiveComplexityProvider, not FetchCognitiveComplexity) and design D6 (*int → int threshold pattern). Addresses PR #294 review feedback from @jflowers. Signed-off-by: Jay Flowers <[email protected]> Assisted-by: deepseek-v4-pro
Summary
internal/cognitive).CC^2 * (1 - coverage)^3 + CC, reported alongside existing CRAP (not replacing it).--max-cognitive-complexity=<N>gate flag togaze crap— exits non-zero when any function exceeds the threshold.cognitive_complexityandgaze_crap_ccper function in JSON output.cognitive_complexitymethod (graceful degradation when a language analyzer does not implement it).Closes #270.
How to Test
go test -race -count=1 -short ./... go run ./cmd/gaze crap ./... --max-cognitive-complexity=15How to Demo
Key Files Changed
internal/cognitive/— new Sonar cognitive complexity analyzer + 22 unit testsinternal/crap/crap.go—CognitiveFormula,Score.CognitiveComplexity+Score.GazeCRAPCCinternal/crap/analyze.go/provider.go— provider wiring +CognitiveComplexityProviderinternal/provider/goprovider/cognitive.go— Go providerinternal/adapter/cognitive.go— external analyzer adapterinternal/protocol/types.go—cognitive_complexitymethod constant + typescmd/gaze/main.go—--max-cognitive-complexityflag + gate evaluationThis PR was generated by /uf.finale (AI-assisted).