Skip to content

feat: add cognitive complexity and make cyclomatic a backstop - #28

Merged
ElbertePlinio merged 4 commits into
mainfrom
feat/7-cognitive-complexity
Sep 28, 2026
Merged

ElbertePlinio merged 4 commits into
mainfrom
feat/7-cognitive-complexity

Conversation

@ElbertePlinio

@ElbertePlinio ElbertePlinio commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

PR 2 of #7, stacked on #27 (the spec). Retarget to main once #27 merges so the closing keywords fire.

Implements the cognitive metric exactly as pinned in docs/spec.md, in its own traversal (crates/core/src/cognitive.rs) beside the cyclomatic one. It adds limits.cognitive (default 15) with per-language overrides and a cognitive violation. The JSON shape and exit codes are unchanged.

complexity is off by default. Limits fields are now optional: null turns a check off in any config layer, and a later layer turns it back on with a number. In languages.<name>.limits, null means off for that language, while an absent key still inherits. init writes "complexity": null.

Svelte scoring follows #25's units. Each top-level block, each snippet, and the root <template> is scored on its own from nesting 0, and nested units are excluded from their parent, as #25 does for cyclomatic.

Fixtures: every function has a hand-derived cognitive value, and every reference entry carries a cognitive_derivation, which the golden test now requires. Each language gains the spec's cases: flat dispatch scoring 1, mixed boolean sequences, labeled jumps, three-deep nesting, unbraced else <loop>, guard arms, and a function at and over the limit.

The metric registry from the #7 checklist is not built. cognitive has the same shape as the existing metrics, and only widget_depth is language-specific, so a registry has no second user yet.

Also fixes #24 (Dart on T {} handlers missing from cyclomatic) and #26 (Rust let-chain && missing from complexity and bool_ops), which the new fixtures exercise.

Corpus measurement, 32,805 functions across 19 repos: the old complexity 15 gate fails 118 functions and cognitive 15 fails 220. 27 stop failing, mostly flat dispatch, and 129 start, because they nest conditions cyclomatic counting could not see. cognitive 20 would fail 121 and 25 would fail 64. A backstop at complexity 40 would add only two functions, _corpusReadinessGate and _isSensitiveEnvironmentKey in pickarena. Both are long boolean chains that bool_ops already fails at 106 and 28, so the backstop was dropped. The release notes must call out the new gate.

Field summarizer: summarizeProfileField (cyclomatic 30) scores cognitive 40 and still fails. The #6 prediction of "low single digits" assumed trivial arms, but its switch arms hold about 16 conditional expressions, several of them nested ternaries, and Sonar's algorithm scores it the same way. Like the env validator correction on #7, this is a real refactor rather than a metric artifact, so the acceptance criterion should be amended rather than the algorithm bent.

Rust match criterion: a large enum match scores 1 on cognitive and complexity is off, so no override is needed. No corpus repo carries a complexity override today.

Hook latency: a whole-repo scan of pickforge (about 10,000 functions) went from 0.82s to 0.85s. Single-file hook checks are unaffected in practice.

Review: an earlier Astra review found three edge cases (catch-binding conditional scored as body, Dart cascade else charged twice, a comment shifting a Python conditional's operands). Each reproduced at runtime and is fixed with a regression fixture. The rebuilt branch is under a second, focused Astra review.

Validation on 469116c: cargo test --workspace --locked --all-targets, cargo clippy --workspace --all-targets -- -D warnings, cargo fmt --check, cargo llvm-cov --fail-under-lines 93 (93.26%, floor ratcheted from 92), and cargo run -- check crates all pass.

Closes #7
Closes #24
Closes #26

Specify cognitive complexity as a per-function metric, with rule tables
for every supported language and independent Svelte template rules.
Cyclomatic complexity becomes a backstop whose default is calibrated
when the metric ships.

Refs #7
cognitive replaces complexity as the default gate; a null limit turns
any check off. Cognitive template rules follow the per-block and
snippet units from #8.
Implement the cognitive complexity metric pinned in docs/spec.md with its
own traversal, config key, per-language overrides, and hand-derived golden
fixtures for every language, including the per-block Svelte template
units. Cyclomatic complexity stays measured but its default limit is now
null, and any limit accepts null to turn that check off.

Also count Dart on-T handlers without a catch clause and the && tokens of
Rust let chains, which the fixtures exercise.

Closes #7
Closes #24
Closes #26
@ElbertePlinio
ElbertePlinio changed the base branch from docs/7-cognitive-spec to main September 28, 2026 21:18
@ElbertePlinio
ElbertePlinio merged commit 07a7ee2 into main Sep 28, 2026
8 checks passed
@ElbertePlinio
ElbertePlinio deleted the feat/7-cognitive-complexity branch September 28, 2026 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant