# Code health markers

Every marker behind the code health score, what each one counts, how deductions combine, and how the weights were calibrated against real defects.

This page is the reference for the markers behind the
[code health](https://docs.repowise.dev/intelligence/code-health) score: what each one detects,
how its deductions combine, which tier it scores into, and where the weights
come from. Read it when a finding needs explaining or a rule needs tuning.

## The markers

| Category | Markers |
|---|---|
| **Structural complexity** | `nested_complexity`, `complex_conditional`, `complex_method`, `large_method`, `primitive_obsession`. `brain_method` and `repeated_nesting` are advisory: listed as refactoring hints, not scored. A rule naming the old id `bumpy_road` still applies |
| **Cohesion & size** | `god_class`. `low_cohesion` (LCOM4) is advisory in every language |
| **Duplication** | `dry_violation` (native Rabin-Karp clone detection), scored in TypeScript and listed as advisory elsewhere |
| **Error handling** | `error_handling` (empty catches, swallowed errors, bare panics), scored in Python and listed as advisory elsewhere |
| **Test coverage** | `untested_hotspot`, `coverage_gap`, `coverage_gradient`; `untested_complexity` is advisory (see [per-function coverage](https://docs.repowise.dev/intelligence/code-health-biomarkers#per-function-coverage-and-crap)) |
| **Test quality** | `large_assertion_block`, `duplicated_assertion_block` deduct; `assertion_free_test`, `mock_saturated_test` are advisory markers that surface but never deduct |
| **Organizational / git** | `developer_congestion`, `knowledge_loss` (shown as fading ownership), `function_hotspot`, `code_age_volatility`, `ownership_risk`, `churn_risk`, `change_entropy`, `co_change_scatter`, `prior_defect`. `hidden_coupling` is also computed here and is advisory: it surfaces with no score effect |
| **Performance** | `io_in_loop` (N+1), `string_concat_in_loop`, `blocking_sync_in_async`, `resource_construction_in_loop`, `serial_await_in_loop`, `lazy_load_in_loop`, `unbounded_read_reduced_in_memory`, and more (see [Performance](https://docs.repowise.dev/intelligence/performance)) |

Five markers carry plain labels in reports and the web UI: `nested_complexity`
reads Deep nesting, `large_method` Long function, `complex_conditional`
Compound condition, `developer_congestion` Many authors and `knowledge_loss`
Fading ownership. The ownership markers are explained on
[Ownership](https://docs.repowise.dev/intelligence/ownership#detectors). Which markers are
advisory in which language is on
[Code health](https://docs.repowise.dev/intelligence/code-health#advisory-per-language).

## How deductions combine

Each file starts at 10.0 and marker findings deduct from it, bounded per
category so no single category can dominate. Code categories bend toward
their bound instead of stopping at it, so shrinking a critical function raises
the score even while it stays critical.

- Each code category's raw total `R` deducts `A * (1 - exp(-R / A))`, with `A`
  at 3.09 times the category's cap. The score breakdown reports each
  category's ceiling.
- Several findings on one function count the largest in full and the rest at
  0.25.
- Several history findings on one file count the largest in full and the rest
  at a quarter.
- A file with no code-shape finding keeps at least 9.0, so git history alone
  costs it at most one point.

## What the size markers count

Size markers read the judged CCN and code lines, so `large_method` reports
"N lines of code (M with blanks and comments)". The judged CCN counts each
decision a reader has to hold, so repeated shapes count once:

- A run of error guards, early exits, guarded copies or optional setters counts
  as one decision.
- A switch's simple arms count once, and its default arm never counts.
- `#if` conditions the compiler decides, Scala DSL words and Pascal bitwise
  `and` / `or` are not decisions.

`large_method` counts logic, not data: literal tables, multi-line strings,
switch arms that only return a literal, builder chains and SwiftUI, Compose and
Flutter view trees count as data. `complex_method` names what it measures on
every finding.

Parameter checks read C and C++ declarators, PHP and Swift types, rest and
keyword parameters, and constructors in Rust, Go, Ruby and PHP. A signature a
framework, the compiler or a same-file interface owns is left alone, since the
author cannot change it.

`god_class` skips Rust trait impls and Ruby mixin modules. Idiomatic Python, Go
and Rust error handling reads as handled, and `error_handling` finds swallowed
catches in Ruby, Swift, Dart and Scala too. Coverage markers skip a file that
holds only imports and re-exports, such as a package `__init__.py` or an
`index.ts` barrel.

## Per-function coverage and CRAP

`untested_complexity` reads each function's CRAP score,
`CC² × (1 - coverage)³ + CC`, and lists functions at 30 or more. Coverage is
the share a report measured as run. Without a report it is a tier, `direct`
(a test calls the function), `reached` (tests reach it or its file) or
`none`, and the finding carries `coverage_basis: estimated`. Measured findings
are listed by default; estimated ones appear when you name the type or ask for
unverified findings. A file that declares only types is never untested.

`coverage_gap`, and `untested_complexity` on measured coverage or a direct
test, carry a ready prompt to add tests (or split the function at complexity
30 and above) naming the nearest test file, plus the
`.repowise/health-rules.json` rule that silences the finding for that file.
See [Test intelligence](https://docs.repowise.dev/intelligence/test-intelligence).

## How the detectors split across tiers

The registry holds **59 detectors** (counting the three governance findings
written by a separate additive pass, **62 marker ids** in total). Each one
affects only its own dimension:

| Group | Count | Scores into |
|---|---:|---|
| Defect-scoring | 22 | `defect` (8 of them also `maintainability`), unless listed as advisory for the file's language |
| Performance | 21 | `performance` only; six loop markers listed at weight 0 |
| SQL | 4 | `maintainability` or `performance` only |
| Advisory | 12 | nothing: `brain_method`, `repeated_nesting`, `low_cohesion`, `untested_complexity`, `hidden_coupling`, `assertion_free_test`, `mock_saturated_test`, the four Apex markers and `cobol_unstructured_flow` surface but never deduct |
| Governance | 3 | nothing: the finding surfaces, the score is untouched |

The three governance markers (`ungoverned_hotspot`, `stale_governance`,
`contradictory_decision`) are written by a pass that runs after scoring
completes and never touches the stored score.

## History markers are context

Git-history markers (ownership, churn, co-change and similar) describe what a
file has been through, not a defect in its code. On production files they stay
out of the default defect lists and are counted under `history`, while scores,
KPIs, `get_risk` and the Fix first order still use them. Findings on vendored
and generated files, and history markers on test files, are held back at
serving. `include=["unverified"]` shows all of them.

The headline's deficit (10 minus the score) splits into a code-shape half and a
history half that add up to it. The CLI findings table lists advisory rows
after the counted ones, marked `advisory`, with the header naming them
separately (`+N advisory, not scored`). A file with fewer than 5 recorded
commits reports `history: "short"`, and the CLI and web UI mark it as short
history.

## Which files are scored

- **Non-code files are never scored.** No marker walks markdown, JSON, YAML or
  TOML, so they are left out of the population and adding documentation never
  lifts an average.
- **A code file in a language with no health walker is not analysed.** It
  stores no score, is left out of averages, hotspot health, bands and trends,
  and is drawn grey on the map. `get_health` reports `unanalysed_file_count`.
  A headline band such as "Excellent" is printed when at least half of the
  code files were analysed, and the headline says "analysed N of M code files"
  whenever some were left out.
- **Production code** (the file set Fix first and Worst Performer read) leaves
  out tests, tooling, build scripts, vendored, generated and docs example
  files.

Every score names the scoring it came from: `analyzer_version` appears in JSON
output, `repowise health --trend` rows and `get_health`, so a move caused by
new scoring is told apart from a move caused by new code.

## Calibrated weights

The marker weights are learned offline from a real defect corpus. Each file is
scored at the commit immediately *before* a 6-month defect window, so the
measurement cannot see the future, and an L2-regularized logistic regression,
with file size (NLOC) as an explicit control, fits each marker's defect lift
*beyond* size. Only the learned constants ship; the runtime stays fully
deterministic.

The strongest calibrated predictors are `co_change_scatter`,
`change_entropy`, `ownership_risk` and `nested_complexity`. Markers that
matter for maintainability and carry little defect signal
(`primitive_obsession`, `dry_violation`, `error_handling`) are floored in the
defect calibration and deducted at full weight in the maintainability signal,
against its own caps.

## Tuning with health-rules.json

`.repowise/health-rules.json` disables markers per glob and remaps
severities, including a named `small-team` profile. The calibrated weights
stay locked.

```json
{
  "profile": "small-team",
  "disabled_biomarkers": ["developer_congestion"],
  "rules": [
    { "path": "vendor/**", "disabled_biomarkers": ["ownership_risk", "knowledge_loss"] }
  ]
}
```

The full schema is on [Configuration](https://docs.repowise.dev/installation/config-reference#the-health-rulesjson-file).

## Next

- [Code health](https://docs.repowise.dev/intelligence/code-health) the score, bands and Fix first.
- [Performance](https://docs.repowise.dev/intelligence/performance) the performance markers and their fix queue.
- [Ownership](https://docs.repowise.dev/intelligence/ownership) the git markers behind fading ownership and congestion.
- [`get_health`](https://docs.repowise.dev/mcp/get-health) read findings from your agent.
