Repository navigation
feat: add cognitive complexity and make cyclomatic a backstop - #28
Merged
Merged
Conversation
8 tasks done
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
force-pushed
the
feat/7-cognitive-complexity
branch
from
September 28, 2026 12:39
7590816 to
469116c
Compare
ElbertePlinio
force-pushed
the
docs/7-cognitive-spec
branch
from
September 28, 2026 12:40
60a09db to
92e861c
Compare
This was referenced Sep 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR 2 of #7, stacked on #27 (the spec). Retarget to
mainonce #27 merges so the closing keywords fire.Implements the
cognitivemetric exactly as pinned indocs/spec.md, in its own traversal (crates/core/src/cognitive.rs) beside the cyclomatic one. It addslimits.cognitive(default 15) with per-language overrides and acognitiveviolation. The JSON shape and exit codes are unchanged.complexityis off by default.Limitsfields are now optional:nullturns a check off in any config layer, and a later layer turns it back on with a number. Inlanguages.<name>.limits,nullmeans off for that language, while an absent key still inherits.initwrites"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
cognitivevalue, and every reference entry carries acognitive_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, unbracedelse <loop>, guard arms, and a function at and over the limit.The metric registry from the #7 checklist is not built.
cognitivehas the same shape as the existing metrics, and onlywidget_depthis 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 fromcomplexityandbool_ops), which the new fixtures exercise.Corpus measurement, 32,805 functions across 19 repos: the old
complexity15 gate fails 118 functions andcognitive15 fails 220. 27 stop failing, mostly flat dispatch, and 129 start, because they nest conditions cyclomatic counting could not see.cognitive20 would fail 121 and 25 would fail 64. A backstop atcomplexity40 would add only two functions,_corpusReadinessGateand_isSensitiveEnvironmentKeyin pickarena. Both are long boolean chains thatbool_opsalready 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
matchcriterion: a large enummatchscores 1 oncognitiveandcomplexityis off, so no override is needed. No corpus repo carries acomplexityoverride 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
elsecharged 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), andcargo run -- check cratesall pass.Closes #7
Closes #24
Closes #26