feat: optional change-frequency (churn) term in risk scoring#555
Open
michael-denyer wants to merge 1 commit into
Open
feat: optional change-frequency (churn) term in risk scoring#555michael-denyer wants to merge 1 commit into
michael-denyer wants to merge 1 commit into
Conversation
Risk scores were 100% structural (flows, communities, test coverage, security keywords, caller count). Hotspot research (Tornhill) shows defect risk correlates with structural complexity x change frequency — the temporal half was missing. Add compute_file_churn(): per-file commit counts over a trailing window (CRG_CHURN_WINDOW_DAYS, default 90 days), parsed natively from git log --numstat with no new dependencies. compute_risk_score() takes an optional churn_counts mapping contributing up to 0.15 (saturating at 10 commits). Opt-in via analyze_changes(include_churn=True) or detect-changes --churn; default-off so existing structural-only scores and published benchmark numbers are unchanged.
Contributor
Author
|
Note on the failing |
6 tasks
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.
Linked issue
No linked issue — small opt-in enhancement to existing risk scoring. Happy to file a feature request via the issue form first if you'd prefer one for tracking.
What & why
compute_risk_scoreis currently 100% structural: flow criticality, cross-community callers, test coverage, security keywords, and caller count. Hotspot research (Adam Tornhill, Your Code as a Crime Scene) shows defect risk correlates with structural complexity × change frequency — the temporal half was missing.This PR adds an opt-in change-frequency (churn) term:
compute_file_churn(repo_root, window_days=None)— per-file commit counts over a trailing window (default 90 days, configurable viaCRG_CHURN_WINDOW_DAYS, matching the existingCRG_*env-var pattern). Parsed natively fromgit log --numstat— no new dependencies. Same subprocess hardening as the adjacent diff parsers (arg list, timeout,errors="replace", empty dict on any failure).compute_risk_score(..., churn_counts=None)— optional term contributingmin(commits / 10, 1.0) × 0.15. Omitted or empty → byte-identical scores.analyze_changes(..., include_churn=False)anddetect-changes --churn— opt-in plumbing. Churn keys are stored under both repo-relative and absolute paths, mirroring the existing Windows: detect-changes CLI maps 0 functions while the MCP detect_changes_tool maps them correctly #528 diff-key remap, so lookups work however the graph stored node paths.Default-off by design: structural-only scores, the GitHub Action output, and the published benchmark numbers are unchanged unless the flag is passed.
Deliberately left out to keep the diff small (one-line follow-ups if wanted): the MCP
detect_changes_toolparameter andupdate --briefchurn support.How it was tested
13 new tests in
tests/test_changes.pyfollowing the suite's conventions: numstat parsing (multi-commit counting, binary files, non-numstat noise), churn counts from a real two-commit git repo, error paths (non-git dir, subprocess failure, non-positive window), saturation cap at exactly 0.15, backward compatibility of the old call signature, relative/absolute path matching, and churn-never-computed-by-default plumbing.Manual end-to-end check on this repository (graph built at HEAD):
detect-changes --base HEAD~1produces identical scores with the flag off (0.55); with--churn, files with ≥10 commits in the last 90 days gain exactly +0.15 (0.70).Checklist
uv run pytest tests/ --tb=short -quv run ruff check code_review_graph/uv run mypy code_review_graph/ --ignore-missing-imports --no-strict-optionaldocs/, docstrings)