Skip to content

Add a cyclomatic-complexity and CRAP-score CI gate - #3670

Open
tom2drum wants to merge 5 commits into
mainfrom
issue-3663
Open

Add a cyclomatic-complexity and CRAP-score CI gate#3670
tom2drum wants to merge 5 commits into
mainfrom
issue-3663

Conversation

@tom2drum

Copy link
Copy Markdown
Collaborator

Resolves #3663

Adds a CI gate that flags code which is both complex and under-tested. A bespoke tools/code-complexity/ CLI computes per-function cyclomatic complexity from the TypeScript AST and joins it with per-function coverage to compute the CRAP score (c²·(1 − cov)³ + c), failing when a function a PR touches exceeds a configurable threshold — scoped to the diff so it never blocks on pre-existing debt.

Spec: .agents/tasks/3663-code-complexity-ci-gate/spec.md

This is a spec-first draft: the branch will receive the task's work ticket by ticket, and the final description will be written when the PR is marked ready for review.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tom2drum tom2drum added the tech Issues related to building, testing, and other project tooling label Aug 26, 2026
tom2drum and others added 4 commits August 26, 2026 18:57
Add the four-ticket decomposition for the complexity/CRAP CI gate:
complexity CLI (tracer bullet), CRAP + coverage/JSX, CI wiring, and
threshold calibration. Amend the spec to drop the third-party service
from the shipped tool and add focused mode + in-tool threshold config.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Introduce tools/code-complexity/: a CLI that computes per-function cyclomatic
complexity by walking the TS/TSX AST (ts.createSourceFile, no type-checker),
matching ESLint's complexity rule. It gates the raw complexity of functions a
diff touches against a configurable cap, prints a table of all checked functions
sorted by complexity, and exits non-zero on any violation. A focused mode scores
every function in explicit file paths, bypassing diff-scoping.

Wired to `pnpm test:code-complexity` via a compile-on-run wrapper (dist/ is
git-ignored). Coverage, CRAP, and JSX handling arrive in later tickets.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Extend tools/code-complexity with the second, coverage-aware gate and make
coverage self-service so the tool is usable in one command locally and in CI.

- CRAP: AST-based JSX detection, per-function line coverage joined from a
  v8/istanbul coverage-final.json, CRAP = c²·(1−cov)³+c, second configurable
  threshold. Report gains COV/CRAP/BROKE columns sorted by CRAP descending.
- Coverage sourcing, two axes mirroring vitest. Selection: full-repo (default),
  focused (paths), diff (--changed). Source: generated by running vitest scoped
  to the selection (default), --coverage-file to consume a prebuilt report (the
  CI path, skips vitest), or --no-coverage. Vitest output hidden unless
  --verbose; a no-test or failing run degrades gracefully.
- The CRAP half applies to JSX-less logic files and to JSX components with a
  co-located vitest spec; visual-only components stay complexity-only.
  *.primed.spec.tsx are excluded from coverage runs (page mounts, no behavior),
  and vitest is skipped entirely when nothing in the selection needs coverage.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The vitest_tests PR run now emits coverage-final.json, and a diff-scoped
post-step gates on it; under $GITHUB_ACTIONS the tool prints ::error
annotations for offenders and writes the report table to the job summary.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
tom2drum added a commit that referenced this pull request Aug 27, 2026
Reverted once the gate's ::error annotations are confirmed on PR #3670.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tom2drum
tom2drum marked this pull request as ready for review August 27, 2026 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tech Issues related to building, testing, and other project tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a cyclomatic-complexity and CRAP-score CI gate

1 participant