Skip to content

9. Churn

codeknife churn reads your git history and combines it with the code model to find files that are both complex and edited often, files edited many times in the last week, files that grew fast, and pairs of files that keep changing together. Use it to decide where refactoring pays off first, or to spot modules whose interface is still unstable.

Git history is read in-process through the gix Rust library. The blade does not start a git subprocess.

The demo repository below has 16 commits. The first commit is 200 days old and falls outside the default 90-day window. One commit on main is a --no-ff merge. The last seven commits each edit app/engine.py, db/schema.py and tests/test_engine.py on seven consecutive days. Five commits dated 30 days ago grow app/big.py from 85 to 600 lines and append a line to app/registry.py.

Terminal window
codeknife churn --no-cache --format human
warning[CK_CHURN_HOTSPOT]: Complexity × Churn hotspot: app/big.py touched 6 times in 90d with complexity sum 240 (score 1440)
--> app/big.py:1-600
hotspot │ 30 ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
= fix: Decompose the most volatile functions and cover branches before further feature edits.
warning[CK_CHURN_BLOAT_VELOCITY]: Rapid file growth: app/big.py grew by +515 lines in 90d (now 600 lines)
--> app/big.py:1-600
= fix: Split newly added responsibilities into cohesive submodules before reaching the hard file cap.
warning[CK_CHURN_HOTSPOT]: Complexity × Churn hotspot: app/engine.py touched 7 times in 90d with complexity sum 24 (score 168)
--> app/engine.py:1-25
hotspot │ 16 ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
= fix: Decompose the most volatile functions and cover branches before further feature edits.
warning[CK_CHURN_THRASH]: Commit thrash: app/engine.py modified 7 times in the last 7 days (threshold 4)
--> app/engine.py:1-25
= fix: Stabilize the module interface and verify behavior with focused tests before editing again.
warning[CK_CHURN_THRASH]: Commit thrash: db/schema.py modified 7 times in the last 7 days (threshold 4)
--> db/schema.py:1-2
= fix: Stabilize the module interface and verify behavior with focused tests before editing again.
warning[CK_CHURN_TEMPORAL_COUPLING]: Temporal coupling: app/engine.py and db/schema.py changed together in 7 of commits (100% over 90d)
--> app/engine.py:1-25
= note: db/schema.py:1 — co-changed 7 times
= fix: Consider co-locating shared logic behind one boundary so changes do not require synchronized edits.
info[CK_CHURN_TEMPORAL_COUPLING]: Temporal coupling: app/big.py and app/registry.py changed together in 5 of commits (83% over 90d)
--> app/big.py:1-600
= note: app/registry.py:1 — co-changed 5 times
= fix: Consider co-locating shared logic behind one boundary so changes do not require synchronized edits.
note: analyzed 14 commit(s) over the last 90 day(s)
codeknife churn: FAIL (6 files, 0 errors, 6 warnings, 1 info, 10ms)

The exit code is 1 because warnings are present.

Points to notice in this output:

  • analyzed 14 commit(s): the 200-day-old commit is outside the window and the merge commit is skipped, so 14 of 16 commits are counted.
  • tests/test_engine.py changed in the same seven commits as app/engine.py, but no coupling is reported for that pair. A pair made of one test file and one non-test file is treated as expected.
  • app/engine.py and db/schema.py sit in different directories, so their coupling is a warning. app/big.py and app/registry.py share a directory, so theirs is info.

The same repository with --days 7 shows how the window changes severities:

Terminal window
codeknife churn --no-cache --days 7 --format agent
CODEKNIFE churn status=fail files=6 errors=0 warnings=2 infos=2 elapsed_ms=9
[1] WARNING CK_CHURN_HOTSPOT app/engine.py:1-25 sym=- impact=78 | Complexity × Churn hotspot: app/engine.py touched 7 times in 7d with complexity sum 24 (score 168) | fix: Decompose the most volatile functions and cover branches before further feature edits.
[2] WARNING CK_CHURN_TEMPORAL_COUPLING app/engine.py:1-25 sym=- impact=66 | Temporal coupling: app/engine.py and db/schema.py changed together in 7 of commits (100% over 7d) | fix: Consider co-locating shared logic behind one boundary so changes do not require synchronized edits.
[3] INFO CK_CHURN_THRASH app/engine.py:1-25 sym=- impact=35 | Commit thrash: app/engine.py modified 7 times in the last 7 days (threshold 4) | fix: Stabilize the module interface and verify behavior with focused tests before editing again.
[4] INFO CK_CHURN_THRASH db/schema.py:1-2 sym=- impact=35 | Commit thrash: db/schema.py modified 7 times in the last 7 days (threshold 4) | fix: Stabilize the module interface and verify behavior with focused tests before editing again.
NOTE analyzed 7 commit(s) over the last 7 day(s)

Only 7 commits fall in the window. With 12 or fewer commits scanned, thrash is always info (see Thrash).

  1. CodeKnife looks for a git repository at or above the analysis root (--path). If none is found, the blade adds a note and returns no findings.
  2. It walks commits reachable from HEAD, newest first, and stops descending at commits older than the window. The window starts days × 86400 seconds before the current time.
  3. The commit time used is the committer timestamp. A commit with a recent author date but an old committer date is outside the window. Because the walk stops at the first commit older than the cutoff, commits behind it are not reached even if their own dates are recent.
  4. Merge commits (more than one parent) are skipped. They are not counted and their changes are not recorded.
  5. Each remaining commit is diffed against its first parent. A root commit is diffed against the empty tree.
  6. Only paths that are files in the current code model are recorded. Files that are excluded, not a supported language, or deleted before HEAD are ignored.
  7. The walk stops after 2,000 commits. When it stops early, the notes include churn walk capped at 2000 commits within <days>d window.

The blade does not read commit authors and computes no ownership metrics.

A file counts once per commit that touches it, no matter how many lines change. The 7-day count uses the same rule for commits whose committer time is within the last 7 days, measured from now. That 7-day period is fixed and does not follow --days.

For each file, CodeKnife adds up max(cognitive, cyclomatic) over every function, method and test in the file. A file with no functions, or a sum of 0, gets a complexity sum of 1. The largest single cognitive complexity in the file is kept separately for hotspot severity. How the two metrics are computed is covered in How Metrics Are Computed.

hotspot score = commits in window × complexity sum

In the example, app/engine.py has 7 commits and a complexity sum of 24, so its score is 168.

None of these rules has a fixer in the Autofix Engine. File-level rules (hotspot, thrash, bloat velocity) never fire on test files. The full catalogue across all blades is on Rule Catalogue.

Rule ID Severity Fires when Controlled by --strict effect
CK_CHURN_HOTSPOT warning or info commits ≥ max(thrash_min_edits, 4), complexity sum ≥ 20 and score ≥ 100. Warning when commits ≥ 6 and either the file’s largest cognitive complexity is above max_cognitive_complexity or score ≥ 240. Otherwise info. thresholds.thrash_min_edits, thresholds.max_cognitive_complexity --strict lowers max_cognitive_complexity to at most 10, so more hotspots become warnings
CK_CHURN_THRASH warning or info commits in the last 7 days ≥ max(thrash_min_edits, 3) thresholds.thrash_min_edits see Thrash
CK_CHURN_BLOAT_VELOCITY warning the file had at least 60 lines at its oldest in-window commit, grew by at least 200 lines since then, and now has at least warn_file_lines lines thresholds.warn_file_lines none
CK_CHURN_TEMPORAL_COUPLING warning or info two files changed together in at least max(temporal_coupling_min_commits, 3) commits and the coupling ratio is at least temporal_coupling_ratio thresholds.temporal_coupling_min_commits, thresholds.temporal_coupling_ratio none

The finding spans the whole file. Its metric_value is the hotspot score and its impact score is 78 for a warning and 42 for info. It carries a bar picture labelled hotspot whose length is the score divided by 10, clamped to between 1 and 30. Human output draws it; JSON output includes it as picture.

The threshold is max(thrash_min_edits, 3), which is 4 with the default config. The finding is a warning only when both of these hold:

  • More than 12 commits were scanned in the window. With 12 or fewer, the repository is treated as new and thrash is always info.
  • The 7-day count is at least the threshold plus 2, or strictness is strict and the count is above the threshold.

Otherwise it is info. metric_value is the 7-day count. Impact is 72 for a warning and 35 for info.

Growth is measured from the version of the file stored in the oldest in-window commit that touched it, to the file on disk now. Blobs that contain a NUL byte are treated as binary and give no line count, so they never trigger this rule. metric_value is the growth in lines. Impact is 68.

Pairs are counted from commits that touch between 2 and 35 model files. A commit touching more than 35 files still counts toward each file’s commit total but adds no pairs. These pairs are never counted:

  • a test file paired with a non-test file
  • a pair where either path is not in the code model

The coupling ratio is:

ratio = commits together / max(commits of file A, commits of file B)

A pair is reported when both thresholds pass. Pairs are sorted by ratio, highest first, and at most 15 are reported per run. The finding is a warning when the two files are in different directories, they changed together at least min_commits + 2 times, and the ratio is at least 0.75. Otherwise it is info. The finding spans the first file of the pair and lists the second file as evidence. metric_value is the ratio as a percentage. Impact is 66 for a warning and 38 for info.

Flag Default Effect
--days <DAYS> 90 Length of the history window in days. 0 is treated as 1.
--changed off File-level findings are limited to changed files. A coupling pair is reported when either file is changed.
--since <REF> none Base ref used to compute the --changed set. See Paths, Excludes & Changed Scope.
--strict off Affects hotspot and thrash severity as described above.
--path, --format, --no-cache, --max-items Global flags. See CLI Reference.

An example with --changed, after appending a line to db/schema.py in the working tree:

Terminal window
codeknife churn --no-cache --changed --format agent
CODEKNIFE churn status=fail files=6 errors=0 warnings=2 infos=0 elapsed_ms=10
[1] WARNING CK_CHURN_THRASH db/schema.py:1-3 sym=- impact=72 | Commit thrash: db/schema.py modified 7 times in the last 7 days (threshold 4) | fix: Stabilize the module interface and verify behavior with focused tests before editing again.
[2] WARNING CK_CHURN_TEMPORAL_COUPLING app/engine.py:1-25 sym=- impact=66 | Temporal coupling: app/engine.py and db/schema.py changed together in 7 of commits (100% over 90d) | fix: Consider co-locating shared logic behind one boundary so changes do not require synchronized edits.
NOTE analyzed 14 commit(s) over the last 90 day(s)

The history itself is the same; only the reported findings are filtered.

All keys live in codeknife.toml. See codeknife.toml for the file format.

Key Default Used for
[churn] days 90 Window length when churn runs inside the static suite (check, plan, map, and other commands built on it). Not read by codeknife churn, which uses --days.
[blades] churn true Turns churn off inside the static suite. Not read by codeknife churn, which always runs.
[thresholds] thrash_min_edits 4 Thrash threshold (floor 3) and hotspot minimum commits (floor 4).
[thresholds] temporal_coupling_min_commits 5 Minimum shared commits for a pair (floor 3).
[thresholds] temporal_coupling_ratio 0.60 Minimum coupling ratio.
[thresholds] max_cognitive_complexity 15 Hotspot severity.
[thresholds] warn_file_lines 500 Minimum current size for bloat velocity.

Raising thrash_min_edits to 8 on the demo repository removes every hotspot and thrash finding, because no file has 8 commits:

[thresholds]
thrash_min_edits = 8
CODEKNIFE churn status=fail files=6 errors=0 warnings=2 infos=1 elapsed_ms=9
[1] WARNING CK_CHURN_BLOAT_VELOCITY app/big.py:1-600 sym=- impact=68 | Rapid file growth: app/big.py grew by +515 lines in 90d (now 600 lines) | fix: Split newly added responsibilities into cohesive submodules before reaching the hard file cap.
[2] WARNING CK_CHURN_TEMPORAL_COUPLING app/engine.py:1-25 sym=- impact=66 | Temporal coupling: app/engine.py and db/schema.py changed together in 7 of commits (100% over 90d) | fix: Consider co-locating shared logic behind one boundary so changes do not require synchronized edits.
[3] INFO CK_CHURN_TEMPORAL_COUPLING app/big.py:1-600 sym=- impact=38 | Temporal coupling: app/big.py and app/registry.py changed together in 5 of commits (83% over 90d) | fix: Consider co-locating shared logic behind one boundary so changes do not require synchronized edits.
NOTE analyzed 14 commit(s) over the last 90 day(s)
CODEKNIFE churn status=pass files=1 errors=0 warnings=0 infos=0 elapsed_ms=2
NOTE no git repository found; skipping churn forensics
CODEKNIFE churn status=pass files=1 errors=0 warnings=0 infos=0 elapsed_ms=2
NOTE git HEAD has no commits yet; skipping churn forensics

Only commits present in the clone are walked. The oldest commit in a shallow clone has no parent object, so it is counted in analyzed N commit(s) but its file changes are not recorded. On a git clone --depth 5 of the demo repository, the five newest commits are analyzed but each file is credited with only four edits:

CODEKNIFE churn status=pass files=6 errors=0 warnings=0 infos=2 elapsed_ms=6
[1] INFO CK_CHURN_THRASH app/engine.py:1-25 sym=- impact=35 | Commit thrash: app/engine.py modified 4 times in the last 7 days (threshold 4) | fix: Stabilize the module interface and verify behavior with focused tests before editing again.
[2] INFO CK_CHURN_THRASH db/schema.py:1-2 sym=- impact=35 | Commit thrash: db/schema.py modified 4 times in the last 7 days (threshold 4) | fix: Stabilize the module interface and verify behavior with focused tests before editing again.
NOTE analyzed 5 commit(s) over the last 90 day(s)

Renames are not followed. Edits are credited to the path in each commit, and only current paths are recorded. After git mv app/engine.py app/core.py and a commit, app/core.py has one commit and the seven earlier edits to app/engine.py no longer count for any file.

Skipped, as described in How history is read. The commits on a merged branch are still walked and counted individually.

Code Meaning
0 No warning or error findings remain after the baseline is applied.
1 At least one warning or error finding.
2 The command failed, for example because codeknife.toml could not be parsed.

Inside the static suite, churn findings join the check gate. The per-file commit counts also feed:

There is no dedicated MCP tool for churn. The codeknife_check MCP tool runs the static suite, which includes churn unless [blades] churn = false. See MCP Tools.