Documentation · Static metrics

km score

Code health score

Computes an overall code health score for the project, grading it from A++ (exceptional) to F— (severe issues). Uses only static metrics (no git required).

Breaking change in v0.14: The default scoring model changed from MI + Cyclomatic Complexity (6 dimensions) to Cognitive Complexity (5 dimensions). Use --model legacy to restore v0.13 behavior.

Non-code files (Markdown, TOML, JSON, etc.) are automatically excluded. Inline test blocks (#[cfg(test)]) are excluded from duplication analysis.

km score [path]
km score --model legacy [path]    # v0.13 scoring model

Dimensions and weights (default: cogcom)

DimensionWeightWhat it measures
Cognitive Complexity30%SonarSource method, penalizes nesting
Duplication20%Project-wide duplicate code %
Indentation Complexity15%Stddev of indentation depth
Halstead Effort20%Mental effort per LOC
File Size15%Optimal range 50-300 LOC

Dimensions and weights (—model legacy)

DimensionWeightWhat it measures
Maintainability Index30%Verifysoft MI, normalized to 0-100
Cyclomatic Complexity20%Max complexity per file
Duplication15%Project-wide duplicate code %
Indentation Complexity15%Stddev of indentation depth
Halstead Effort15%Mental effort per LOC
File Size5%Optimal range 50-300 LOC

Each dimension is aggregated as a LOC-weighted mean across all files (except Duplication which is a single project-level value). The project score is the weighted sum of all dimension scores.

Grade scale

GradeScore rangeGradeScore range
A++97-100C+73-76
A+93-96C70-72
A90-92C-67-69
A-87-89D+63-66
B+83-86D60-62
B80-82D-57-59
B-77-79F50-56
F-40-49
F—0-39

Options:

FlagDescription
--model MODELScoring model: cogcom (default, v0.14+) or legacy (MI + cyclomatic, v0.13)
--trend [REF]Compare current score against a git ref (default: HEAD). Shows change: B- → B (+2.3). Useful for PR review: --trend origin/main
--fail-if-worseWith --trend: exit with code 1 if the score dropped by more than --gate-tolerance
--gate-tolerance POINTSScore drop --fail-if-worse allows before failing (default: 0.01 with --gate-scope project, 0.5 with --gate-scope changed). Compares unrounded scores
--gate-scope {project,changed}What --fail-if-worse compares (default: project, the aggregate score). changed looks only at the files the diff touches: it fails if a modified or renamed file ends below the project score at the ref after dropping more than --gate-tolerance, or if the project’s duplicated lines grow. Files above the project score, new files and deleted files never fail it, so removing healthy code cannot lower the verdict. The report lists every changed file with its before/after score
--fail-below GRADEWith --trend: exit with code 1 if the score is below GRADE (e.g. B-). Overridable via .kimun.toml
--format {table,json,short,terse}Output format (default: table)
--include-testsInclude test files in analysis (excluded by default)
--bottom NNumber of worst files to show in “needs attention” (default: 10)
--min-lines NMinimum lines for a duplicate block (default: 6)

Example output:

Code Health Score
──────────────────────────────────────────────────────────────────
 Project Score:  B+ (84.3)
 Files Analyzed: 42
 Total LOC:      8,432
──────────────────────────────────────────────────────────────────
 Dimension                 Weight   Score   Grade
──────────────────────────────────────────────────────────────────
 Cognitive Complexity         30%    85.6   B+
 Duplication                  20%    91.3   A
 Indentation Complexity       15%    79.8   B-
 Halstead Effort              20%    85.1   B+
 File Size                    15%    89.2   A-
──────────────────────────────────────────────────────────────────

 Files Needing Attention (worst scores)
──────────────────────────────────────────────────────────────────
 Score  Grade  File                       Issues
──────────────────────────────────────────────────────────────────
  54.2  F      src/legacy/parser.rs       Cognitive: 42, Indent: 3.2
  63.7  D+     src/utils/helpers.rs       Effort: 15200, Indent: 2.4
  68.9  C-     src/core/engine.rs         Size: 1243 LOC
──────────────────────────────────────────────────────────────────

km score diff — Compare score against a git ref

Extracts the file tree at the given ref, computes the score for both snapshots, and shows a delta table per dimension. Useful for reviewing how commits impact code quality.

km score diff                          # compare vs HEAD (uncommitted changes)
km score diff --git-ref HEAD~1         # compare vs previous commit
km score diff --git-ref main           # compare vs main branch
km score diff --format json            # machine-readable output

Options:

FlagDescription
--git-ref REFGit ref to compare against (default: HEAD)
--model MODELScoring model: cogcom (default) or legacy
--format {table,json,short,terse}Output format (default: table)
--bottom NNumber of worst files to show (default: 10)
--min-lines NMinimum lines for a duplicate block (default: 6)