Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
feat(risk): gate a change on its risk percentile and weight patch coverage by risk (#2645)
Raghav Chamadiya78d59338d2cb
diff --git a/action.yml b/action.ymlindex 1a5de52a1..3cfbdc6b4 100644--- a/action.yml+++ b/action.yml@@ -1,7 +1,7 @@name: Repowise CI gatesdescription: >-- Gate a pull request on patch coverage, documentation drift and new security- findings. Needs git and nothing else: no index, no API key.+ Gate a pull request on patch coverage, documentation drift, new security+ findings and change risk. Needs git and nothing else: no index, no API key.author: repowise-devbranding:icon: check-circle@@ -10,9 +10,11 @@ branding:inputs:checks:description: >-- Comma-separated gates to run: coverage, doc-drift, security. Coverage- needs a report, so it is not on by default. Doc drift checks the whole- tree; on a repository that already has drift, commit a baseline first.+ Comma-separated gates to run: coverage, doc-drift, security, risk.+ Coverage needs a report, so it is not on by default. Doc drift checks the+ whole tree; on a repository that already has drift, commit a baseline+ first. Risk is opt-in too, and gates only when risk-fail-above-percentile+ is set.default: doc-drift,securityversion:description: Repowise version to install from PyPI. Empty installs the latest.@@ -25,8 +27,8 @@ inputs:default: "."base:description: >-- Revision range for coverage and security, e.g. origin/main...HEAD. Empty- reads the pull request's target branch, else the default branch.+ Revision range for coverage, security and risk, e.g. origin/main...HEAD.+ Empty reads the pull request's target branch, else the default branch.default: ""coverage-report:description: >-@@ -46,6 +48,13 @@ inputs:than this is reported but never fails the coverage gate. Empty usescoverage.min_coverable_lines from .repowise/config.yaml.default: ""+ coverage-fail-under-risky:+ description: >-+ Minimum patch coverage percent over risky files only: hotspots or bug+ magnets from the index; on git alone, the top quartile of files with+ bug-fix history. A change with no risky file is not gated. Empty uses+ coverage.fail_under_risky from .repowise/config.yaml, else no risky gate.+ default: ""doc-drift-baseline:description: Committed doc-drift baseline file; only findings it does not list fail.default: ""@@ -55,6 +64,11 @@ inputs:security-baseline:description: Committed security baseline file; only findings it does not list fail.default: ""+ risk-fail-above-percentile:+ description: >-+ Fail when the change ranks above this percentile of the repository's+ recent commits by size and spread. Empty reports the rank without gating.+ default: ""upload-sarif:description: >-"true" uploads doc-drift and security findings to GitHub code scanning.@@ -71,6 +85,9 @@ outputs:security:description: Exit code of the security gate, empty when not run.value: ${{ steps.gates.outputs.security }}+ risk:+ description: Exit code of the change-risk gate, empty when not run.+ value: ${{ steps.gates.outputs.risk }}sarif-dir:description: Directory holding the SARIF files, when upload-sarif is on.value: ${{ steps.gates.outputs.sarif-dir }}@@ -98,9 +115,11 @@ runs:COVERAGE_REPORT: ${{ inputs.coverage-report }}COVERAGE_FAIL_UNDER: ${{ inputs.coverage-fail-under }}COVERAGE_MIN_COVERABLE_LINES: ${{ inputs.coverage-min-coverable-lines }}+ COVERAGE_FAIL_UNDER_RISKY: ${{ inputs.coverage-fail-under-risky }}DOC_DRIFT_BASELINE: ${{ inputs.doc-drift-baseline }}SECURITY_FAIL_ON: ${{ inputs.security-fail-on }}SECURITY_BASELINE: ${{ inputs.security-baseline }}+ RISK_FAIL_ABOVE_PERCENTILE: ${{ inputs.risk-fail-above-percentile }}SARIF: ${{ inputs.upload-sarif }}SARIF_DIR: ${{ runner.temp }}/repowise-sarifPYTHONUTF8: "1"diff --git a/ci/github/run-gates.sh b/ci/github/run-gates.shindex d695369bb..269b2cf14 100644--- a/ci/github/run-gates.sh+++ b/ci/github/run-gates.sh@@ -14,19 +14,20 @@ checks=" $names "wants() { [[ "$checks" == *" $1 "* ]]; }if [[ -z "${names// /}" ]]; then- echo "::error::No check selected; set checks to coverage, doc-drift or security."+ echo "::error::No check selected; set checks to coverage, doc-drift, security or risk."exit 2fifor name in $names; docase "$name" in- coverage | doc-drift | security) ;;- *) echo "::error::Unknown check '$name'; use coverage, doc-drift or security." ; exit 2 ;;+ coverage | doc-drift | security | risk) ;;+ *) echo "::error::Unknown check '$name'; use coverage, doc-drift, security or risk." ; exit 2 ;;esacdone-# Coverage and security diff the change, so they need its history and the-# target branch. A shallow clone has neither; fetch them rather than exit 2.-if wants coverage || wants security; then+# Coverage, security and risk diff the change, so they need its history and+# the target branch; risk also ranks it against recent commits. A shallow+# clone has neither; fetch them rather than exit 2.+if wants coverage || wants security || wants risk; thenif [[ "$(git rev-parse --is-shallow-repository)" == "true" ]]; thengit fetch --quiet --no-tags --unshallow origin || truefi@@ -64,6 +65,7 @@ if wants coverage; thendone <<<"$COVERAGE_REPORT"[[ -n "$COVERAGE_FAIL_UNDER" ]] && args+=(--fail-under "$COVERAGE_FAIL_UNDER")[[ -n "$COVERAGE_MIN_COVERABLE_LINES" ]] && args+=(--min-coverable-lines "$COVERAGE_MIN_COVERABLE_LINES")+ [[ -n "$COVERAGE_FAIL_UNDER_RISKY" ]] && args+=(--fail-under-risky "$COVERAGE_FAIL_UNDER_RISKY")run coverage repowise "${args[@]}"fi@@ -75,6 +77,14 @@ security=(security check ${base[@]+"${base[@]}"} --fail-on "$SECURITY_FAIL_ON")wants doc-drift && run doc-drift repowise "${drift[@]}" --format githubwants security && run security repowise "${security[@]}" --format github+if wants risk; then+ # Without a base, --format github scores the pull request against its target+ # branch. Without a percentile the rank is reported, not gated.+ risk=(risk ${base[@]+"${base[@]}"} --format github)+ [[ -n "$RISK_FAIL_ABOVE_PERCENTILE" ]] && risk+=(--fail-above-percentile "$RISK_FAIL_ABOVE_PERCENTILE")+ run risk repowise "${risk[@]}"+fi+# SARIF is a second run in its own format. A gate that could not evaluate# writes an empty file, which code scanning would reject, so it is dropped.if [[ "$SARIF" == "true" ]] && { wants doc-drift || wants security; }; thendiff --git a/ci/gitlab/repowise.gitlab-ci.yml b/ci/gitlab/repowise.gitlab-ci.ymlindex 61e9eafe1..c31857d5c 100644--- a/ci/gitlab/repowise.gitlab-ci.yml+++ b/ci/gitlab/repowise.gitlab-ci.yml@@ -22,9 +22,14 @@# REPOWISE_COVERAGE_MIN_COVERABLE_LINES# Small-change tolerance: fewer changed executable# lines than this never fails the coverage gate.+# REPOWISE_COVERAGE_FAIL_UNDER_RISKY+# Minimum patch coverage percent over risky files only.# REPOWISE_DOC_DRIFT_BASELINE Committed doc-drift baseline file.# REPOWISE_SECURITY_BASELINE Committed security baseline file.# REPOWISE_SECURITY_FAIL_ON Lowest severity that fails: high (default), med, low.+# REPOWISE_RISK_FAIL_ABOVE_PERCENTILE+# Fail when the change ranks above this percentile of+# recent commits. The risk job runs only when it is set..repowise:image: python:3.12@@ -48,6 +53,7 @@ repowise-coverage:$(printf -- '--report %s ' $REPOWISE_COVERAGE_REPORT)${REPOWISE_COVERAGE_FAIL_UNDER:+--fail-under "$REPOWISE_COVERAGE_FAIL_UNDER"}${REPOWISE_COVERAGE_MIN_COVERABLE_LINES:+--min-coverable-lines "$REPOWISE_COVERAGE_MIN_COVERABLE_LINES"}+ ${REPOWISE_COVERAGE_FAIL_UNDER_RISKY:+--fail-under-risky "$REPOWISE_COVERAGE_FAIL_UNDER_RISKY"}--format markdown > repowise-coverage.mdafter_script:- cat repowise-coverage.md@@ -130,3 +136,18 @@ repowise-code-quality:codequality: gl-code-quality-doc-drift.jsonrules:- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH++repowise-risk:+ extends: .repowise+ script:+ - >+ repowise risk "origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME...HEAD"+ --fail-above-percentile "$REPOWISE_RISK_FAIL_ABOVE_PERCENTILE"+ --format markdown > repowise-risk.md+ after_script:+ - cat repowise-risk.md+ artifacts:+ when: always+ paths: [repowise-risk.md]+ rules:+ - if: $CI_PIPELINE_SOURCE == "merge_request_event" && $REPOWISE_RISK_FAIL_ABOVE_PERCENTILEdiff --git a/docs/agent/MCP_TOOLS.md b/docs/agent/MCP_TOOLS.mdindex 71aac25a1..23e966cb9 100644--- a/docs/agent/MCP_TOOLS.md+++ b/docs/agent/MCP_TOOLS.md@@ -679,6 +679,13 @@ reads `fail` when one that is not informational fails). They are judged onlyon coverage measured at the change's head and valid config; otherwise theyread `no_data`, and `scope.config_errors` names each invalid entry. The blockis absent when no coverage is stored.+Each file row carries `risk`: `fix_pressure` (recency-weighted bug-fix weight+from the checkout's git history), `dependents`, `hotspot` and `bug_magnet`+(from the index), `risky`, `reasons` and `basis` (`git_and_index`, `git`,+`index` or `unavailable`). Rows are listed riskiest first, and `risky`+(`file_count`, `covered_line_count`, `coverable_line_count`,+`patch_coverage_pct`, `threshold`, `gate`) summarizes coverage over the risky+files, null when no row's risk was assessed.In workspace mode the response also carries `cross_repo`, and every`cross_repo.consumers[]` row gains a `tests` block: a `state` (`measured`,diff --git a/docs/layers/CHANGE_RISK.md b/docs/layers/CHANGE_RISK.mdindex 15321a497..bed24ae4c 100644--- a/docs/layers/CHANGE_RISK.md+++ b/docs/layers/CHANGE_RISK.md@@ -49,6 +49,50 @@ those patterns apply automatically and are combined with any command-lineflags. For example, `tests/` excludes that directory recursively, while`test_*.py` excludes matching test filenames anywhere in the repository.+## In CI++`--fail-above-percentile P` turns the command into a gate on+`risk_percentile`: it ranks the diff-shape score against recent commits, and+the absolute 0-10 value never decides.++```bash+repowise risk --fail-above-percentile 95 --format github+repowise risk origin/main...HEAD --fail-above-percentile 95 --format markdown+```++With the flag, or with `--format markdown` or `github`, and no revspec, the+subject is the CI change: the pull request's target branch (from the CI's+variables, else the remote's default branch) `...HEAD`. A CI checkout of a+pull request is a merge commit, so `HEAD` alone would be the wrong change.+Otherwise the default stays "uncommitted work, else `HEAD`".++Exit codes are the shared CI ones: `0` at or below the percentile, `1` above+it, `2` when the gate cannot evaluate. A change with no percentile cannot be+gated: `--baseline 0` turns ranking off, and a shallow clone with fewer than 8+recent commits has nothing to rank against, so fetch full history. At a+percentile P roughly (100 - P)% of changes fail by construction; a failure asks+for a split or a second reviewer, not a code fix.++The table prints the verdict under the review priority. `json` adds+`"gate": {"fail_above_percentile": P, "percentile": <unrounded>, "status":+"pass" | "fail"}`; `percentile` is what the gate compared, since+`risk_percentile` is rounded. `markdown` leads with the verdict ("**Above the+95th percentile gate**: larger and more spread out than 97% of recent+commits"), then the range, the sample, the lines, files and directories, the+fix-history rank, and the files with bug-fix history. `github` writes an+`::error::` when the gate fails (a `::notice::` otherwise) plus the markdown in+the job summary. `markdown` and `github` are not for `--target`, which stays+`table` or `json`.++A revspec git cannot read (an unknown revision, not a repository, a missing+merge-base) exits `2` on every revspec run, gated or not. It used to exit `1`,+which a pipeline would have read as a failed gate.++The GitHub Action runs it as the `risk` check (input+`risk-fail-above-percentile`; empty reports the rank without gating), and the+GitLab template as the `repowise-risk` job when+`REPOWISE_RISK_FAIL_ABOVE_PERCENTILE` is set. See [CI](../start/CI.md).+## Fix history: where the change landsThe first block in the result is `fix_history`, and it is the one to act on. Itdiff --git a/docs/layers/TEST_INTELLIGENCE.md b/docs/layers/TEST_INTELLIGENCE.mdindex ca26d07db..83c5078a3 100644--- a/docs/layers/TEST_INTELLIGENCE.md+++ b/docs/layers/TEST_INTELLIGENCE.md@@ -279,12 +279,47 @@ no report found, readable or matching a repository file, an unknown revision,no merge-base (a shallow clone), a single commit at a shallow clone'sboundary, bad config, or not a git repository.-`--format github` writes up to 10 `::warning` annotations (largest uncovered-ranges first), a notice counting the rest, an `::error::` when the gate fails-and a notice when the small-change tolerance exempts it, then appends the-markdown summary to `$GITHUB_STEP_SUMMARY`. `markdown` and `json` suit other CI-systems. A coverage.py `.coverage` database is not a text report: export it-with `coverage lcov` or `coverage xml` first.+`--format github` writes up to 10 `::warning` annotations (riskiest file first,+then largest uncovered range), a notice counting the rest, an `::error::` for+each gate that fails and a notice when the small-change tolerance exempts one,+then appends the markdown summary to `$GITHUB_STEP_SUMMARY`. `markdown` and+`json` suit other CI systems.++### Risk of each changed file++Every row carries the risk of its file (`risk` on each file in `json`, the REST+response and `get_change_risk`'s `patch_coverage` block): `fix_pressure` (the+recency-weighted bug-fix count from git, read at the change's merge-base),+`dependents` (import fan-in from the index graph), the index's `hotspot` and+`bug_magnet` flags, `risky`, `reasons`, and `basis`. Risky reuses existing+rules: with index data for the file, a hotspot or a bug magnet; on git alone+(no index, or a file the change adds, which the index cannot know), the top+quartile of files with bug-fix history, by fix pressure. Ranking only among+files that have been fixed keeps "fixed once" from qualifying in a repository+where most files never were. `basis` is `git_and_index`, `git`, `index` (the+git walk failed) or `unavailable`. `coverage check` always reads git and adds+the index when one opens; a missing or unreadable index never fails it.++Rows, annotations and the markdown table list risky files first, then by fix+pressure, dependents and uncovered lines. The table gains a "Risk" column in+words ("hotspot, bug-fix weight 3.2, 14 dependents", "none known", "unknown"),+and a line states the basis when not every row had index data.++`--fail-under-risky PCT` (or `coverage.fail_under_risky`, validated like+`fail_under`) adds a stricter gate over the risky files' changed executable+lines. `risky` in `json` carries its covered and coverable counts, percentage,+file count and `gate`. It uses the same rule as the other gates, including the+whole change's small-change tolerance: a small change is exempt (`too_small`),+a small risky slice of a big one is not. The overall `gate` fails when the flat+gate, a path-scoped gate that is not informational, or the risky gate fails;+the flat gate's own rule is unchanged. No risky file with a changed executable+line leaves the risky gate not applied (`no_data`, exit `0`). With the flag+set, a shallow clone or a measured row whose risk could not be read exits `2`,+unless the flat or a path-scoped gate already failed, which is then reported+as the failure.++A coverage.py `.coverage` database is not a text report: export it with+`coverage lcov` or `coverage xml` first.CI checkouts are often shallow, which leaves no merge-base to diff from. Fetchfull history (`fetch-depth: 0` on GitHub Actions, `GIT_DEPTH: 0` on GitLab).diff --git a/docs/reference/CLI_REFERENCE.md b/docs/reference/CLI_REFERENCE.mdindex 6cb94eed8..98fdd3717 100644--- a/docs/reference/CLI_REFERENCE.md+++ b/docs/reference/CLI_REFERENCE.md@@ -822,7 +822,8 @@ to the model's baseline commit, not this repo.| `--baseline` | Recent commits to sample for the repo-relative percentile (default 200; `0` shows only the absolute per-commit model-score band) || `--target` / `-t` | Score what history says about these **files** instead of a change. Repeatable; switches the command to the `get_risk` tool || `--changed-file` | With `--target`: PR mode. Leads with a directive naming what may break, which co-changes and tests are missing, and what to run |-| `--format` | Output format: `table` (default) or `json` |+| `--fail-above-percentile` | CI gate (0-100): exit `1` when the change ranks above this percentile of recent commits (`risk_percentile`, never the 0-10 score), `2` when it has no percentile (`--baseline 0`, or fewer than 8 commits to rank against). Without `REVSPEC` it scores the CI change: the target branch `...HEAD` |+| `--format` | Output format: `table` (default), `json`, `markdown` or `github`. `markdown` and `github` are not for `--target`; without `REVSPEC` they score the CI change. `github` writes an `::error::` when the gate fails (else a `::notice::`) and the markdown to `$GITHUB_STEP_SUMMARY` || `--full` | With `--target`: emit the complete tool payload as JSON (implies `--format json`) |```bash@@ -831,8 +832,18 @@ repowise risk HEAD # score the last commitrepowise risk main..HEAD # score a branch / PR range as one changerepowise risk --ext .ts,.tsx # restrict to specific suffixesrepowise risk main..HEAD -x 'tests/' -x '*.spec.ts' # omit tests from scoring+repowise risk --fail-above-percentile 95 --format github # gate the CI change```+With `--fail-above-percentile`, `--format json` adds `"gate":+{"fail_above_percentile": P, "percentile": <unrounded>, "status": "pass" |+"fail"}`; `percentile` is what the gate compared, since `risk_percentile` is+rounded. A revspec git cannot read exits `2` (it used to exit `1`), gated or+not, with a `{"error", "message"}` document under `--format json`. At a+percentile P roughly (100 - P)% of changes fail by construction; a failure asks+for a split or a second reviewer, not a code fix, and there is no baseline to+accept it with.+**`--target`: what history says about touching some files.** Two questions, onecommand, because they are the same question asked of different subjects. A`REVSPEC` scores a *change* from its diff shape; `--target` reports bug-fix@@ -1324,57 +1335,72 @@ are out of scope and only counted.| `--report-format` | Force a parser: `lcov`, `cobertura`, `clover`, `repowise-json`, `go-coverprofile`, `jacoco` || `--fail-under` | Exit 1 when patch coverage is below this percentage (0-100). Defaults to `coverage.fail_under` in `.repowise/config.yaml`; with neither set the command reports without gating || `--min-coverable-lines` | Small-change tolerance: a change with fewer changed executable lines than this (counting only lines the report measures, as the percentage does) is reported against the threshold but never fails (`gate` reads `too_small`). Defaults to `coverage.min_coverable_lines` |+| `--fail-under-risky` | Exit 1 when patch coverage of the risky files alone is below this percentage. Risky: hotspots or bug magnets from the index; on git alone, the top quartile of files with bug-fix history. Defaults to `coverage.fail_under_risky`. With no risky file changed the gate is not applied (`no_data`, exit `0`) || `--path` | A path inside the repository (defaults to cwd) || `--format` | `table` (default), `json`, `markdown`, or `github` |-`github` writes up to 10 `::warning` annotations (largest uncovered ranges-first), a `::notice` counting the rest, an `::error::` when the gate fails, one-per failing path-scoped gate that is not informational, a `::notice::` per-informational gate below its threshold, and-a `::notice::` when the small-change tolerance exempts it, then appends the-markdown summary to `$GITHUB_STEP_SUMMARY` when set. `json` carries-`patch_coverage_pct`, `covered_line_count`, `coverable_line_count`,+Each changed file carries its risk: git bug-fix history always, plus hotspot,+bug-magnet and dependent counts when an index opens (a missing index never+fails the check). Rows, annotations and the markdown table list risky files+first, and the table has a "Risk" column in words.++`github` writes up to 10 `::warning` annotations (riskiest file, then largest+uncovered range, first; titled "Uncovered change in a risky file" for a risky+one), a `::notice` counting the rest, an `::error::` for each gate that fails+(the whole change, each path-scoped gate that is not informational, the+risky-file gate), a `::notice::` per informational gate below its threshold,+and a `::notice::` when the small-change tolerance exempts a gate, then+appends the markdown summary to `$GITHUB_STEP_SUMMARY` when set. `json`+carries `patch_coverage_pct`, `covered_line_count`, `coverable_line_count`,`threshold`, `min_coverable_lines`, `gate` (`pass`, `fail`, `no_data`,-`not_set`, `too_small`), `file_counts` (`measured`, `not_in_report`,-`no_line_data`, `no_coverable_changes`, `out_of_scope`), `files[]`-(`file_path`, `status`, `changed_line_count`, `coverable_line_count`,-`covered_line_count`, `patch_coverage_pct`, `uncovered_ranges`) and `scope`-(`label`, `source_formats`, `reports`, `report_path_count`,-`unmatched_report_path_count`, `ignored_file_count`, `config_errors`) and+`not_set`, `too_small`; `fail` when any gate fails), `file_counts`+(`measured`, `not_in_report`, `no_line_data`, `no_coverable_changes`,+`out_of_scope`), `files[]` (`file_path`, `status`, `changed_line_count`,+`coverable_line_count`, `covered_line_count`, `patch_coverage_pct`,+`uncovered_ranges`, `risk`: `fix_pressure`, `dependents`, `hotspot`,+`bug_magnet`, `basis`, `risky`, `reasons`), `scope` (`label`,+`source_formats`, `reports`, `report_path_count`,+`unmatched_report_path_count`, `ignored_file_count`, `config_errors`),`path_gates[]` (`name`, `paths`, `threshold`, `informational`,`measured_file_count`, `unmeasured_file_count`, `covered_line_count`,-`coverable_line_count`, `patch_coverage_pct`, `gate`).-Changed files and report-entries matching `coverage.ignore` (gitignore syntax) are left out and counted-as ignored. Go coverprofile paths under a `go.mod`'s module path are mapped to+`coverable_line_count`, `patch_coverage_pct`, `gate`) and `risky`+(`file_count`, `covered_line_count`, `coverable_line_count`,+`patch_coverage_pct`, `threshold`, `gate`). Changed files and report entries+matching `coverage.ignore` (gitignore syntax) are left out and counted as+ignored. Go coverprofile paths under a `go.mod`'s module path are mapped tothat module's directory unless the report has a per-report prefix or`coverage.path_prefix` is set. Percentages are shown floored to one decimal(79.99% reads 79.9%); the gate compares the unrounded figure.**Exit codes:** `0` the gate passes or there is nothing to judge (nothreshold, no measurable changed lines, or a change under the small-change-tolerance); `1` patch coverage is below `--fail-under`, or a path-scoped gate-that is not informational fails; `2` the check could not-run: no report found, a `--report` path or glob matching no file, none-readable, none matching a repository file, or every entry matching-`coverage.ignore`; an unknown revision; no merge-base (a shallow clone); a-single commit at a shallow clone's boundary; bad config or a malformed-`coverage.fail_under` / `coverage.min_coverable_lines`, or an unusable-`coverage.gates` entry (named in the message); not a git repository.+tolerance); `1` patch coverage is below `--fail-under`, a path-scoped gate+that is not informational fails, or the risky files' is below+`--fail-under-risky`; `2` the check could not run: no report found, a+`--report` path or glob matching no file, none readable, none matching a+repository file, or every entry matching `coverage.ignore`; an unknown+revision; no merge-base (a shallow clone); a single commit at a shallow+clone's boundary; bad config or a malformed `coverage.fail_under` /+`coverage.min_coverable_lines` / `coverage.fail_under_risky`, or an unusable+`coverage.gates` entry (named in the message); not a git repository;+`--fail-under-risky` set on a shallow clone, or with a measured file whose+risk could not be read (unless the flat or a path-scoped gate already failed,+which exits `1`).**Path-scoped gates.** Each entry of `coverage.gates` (`name`, `paths` asgitignore-style globs relative to the repository root, optional `fail_under`0-100, optional `informational`) is judged on the measured changed files itsglobs match, with the same rule as the whole change; a file can count inseveral gates. The small-change tolerance is the whole change's: every gate-reads `too_small` when the change is under `min_coverable_lines`, never a-small slice of a big one. Matching changed files the report does not measure-are counted as `unmeasured_file_count`, outside the percentage. `gate` reads-`fail` when any gate that is not informational fails, whatever the-whole-change figure, and the headline then leads with that gate and its-counts. The table output lists every gate; the markdown lists up to 10,-failing ones first. See [Path-scoped gates](../start/CI.md#path-scoped-gates)-and `repowise coverage suggest-gates`.+(the risky-file gate too) reads `too_small` when the change is under+`min_coverable_lines`, never a small slice of a big one. Matching changed+files the report does not measure are counted as `unmeasured_file_count`,+outside the percentage. `gate` reads `fail` when any gate that is not+informational fails, whatever the whole-change figure, and the headline then+leads with that gate and its counts. The table output lists every gate; the+markdown lists up to 10, failing ones first. See+[Path-scoped gates](../start/CI.md#path-scoped-gates) and+`repowise coverage suggest-gates`.A coverage.py `.coverage` database is not a text report: export it first with`coverage lcov` or `coverage xml`. A new file no test loads must still appeardiff --git a/docs/start/CI.md b/docs/start/CI.mdindex 90f007d59..24397dd34 100644--- a/docs/start/CI.md+++ b/docs/start/CI.md@@ -1,6 +1,6 @@# Repowise in CI-Three gates judge a pull request, each on what the change adds rather than on+Four gates judge a pull request, each on what the change adds rather than onthe whole repository's past:| Gate | Question | Needs | History |@@ -8,20 +8,23 @@ the whole repository's past:| `repowise coverage check` | Did the tests run the lines this change touched? | git and a coverage report | the merge-base with the target branch || `repowise doc-drift --check` | Does the documentation still describe files, links and commands that exist? | git | none (full history only improves rename suggestions) || `repowise security check` | Did this change add a secret or a risky call? | git | every commit of the change |+| `repowise risk --fail-above-percentile P` | Is this change bigger and more spread out than P% of this repository's recent commits? | git | recent commits to rank against (at least 8) |None of them needs an index, a database, a model or an API key, and nonestores anything. They share one set of exit codes, output formats and baseresolution, described once below. Each layer's own page has the detail:[test intelligence](../layers/TEST_INTELLIGENCE.md#patch-coverage-in-ci),[documentation drift](../layers/DOC_DRIFT.md#in-ci),-[security](../layers/SECURITY.md#in-ci-repowise-security-check).+[security](../layers/SECURITY.md#in-ci-repowise-security-check),+[change risk](../layers/CHANGE_RISK.md#in-ci).## What every gate does the same way**Exit codes.** `0` passed, or had nothing to judge. `1` the change failed thegate. `2` the gate could not evaluate: not a git repository, an unknown-revision, no merge-base in a shallow clone, an unreadable report or baseline.-A setup problem never reads as a pass.+revision, no merge-base in a shallow clone, an unreadable report or baseline,+too few recent commits to rank a change against. A setup problem never reads+as a pass.**Formats.** `--format table` (the default) for a terminal, `json` for ascript, `markdown` for a comment or a job artifact, and `github` for GitHub@@ -33,14 +36,16 @@ When a gate cannot evaluate, `gitlab` still prints a valid list, `[]`, and theGitLab template writes `[]` whenever the output does not parse, so the reportartifact is never an invalid file.-**The change being judged.** Coverage and security take a revision range:+**The change being judged.** Coverage, security and risk take a revision range:`origin/main...HEAD` (three dots: what the branch did since it forked, thepull request's view), `base..head`, or one commit. Without one they read thetarget branch from the CI's own variables (`GITHUB_BASE_REF`,`CI_MERGE_REQUEST_TARGET_BRANCH_NAME`, `CHANGE_TARGET`,-`BITBUCKET_PR_DESTINATION_BRANCH`), else the remote's default branch. Doc-drift has no range: it checks the whole working tree, and a baseline limits-it to new findings.+`BITBUCKET_PR_DESTINATION_BRANCH`), else the remote's default branch. `risk`+does so with `--fail-above-percentile` or `--format markdown` / `github`;+otherwise an omitted range still means your uncommitted work, else `HEAD`. Doc+drift has no range: it checks the whole working tree, and a baseline limits it+to new findings.**History.** CI checkouts are usually shallow, which leaves no merge-base.Fetch full history (`fetch-depth: 0` on GitHub Actions, `GIT_DEPTH: 0` on@@ -91,9 +96,10 @@ jobs:- run: pytest --cov=src --cov-report=lcov:coverage/lcov.info- uses: repowise-dev/repowise@main # pin a release tag in productionwith:- checks: coverage,doc-drift,security+ checks: coverage,doc-drift,security,riskcoverage-report: coverage/lcov.infocoverage-fail-under: 80+ risk-fail-above-percentile: 95```Pin a release tag rather than `main`, and set `version:` to pin the Repowise@@ -101,23 +107,30 @@ release the action installs.| Input | Default | Meaning ||-------|---------|---------|-| `checks` | `doc-drift,security` | Gates to run. Coverage needs a report, so it is opt-in. |+| `checks` | `doc-drift,security` | Gates to run. Coverage needs a report and risk is a policy choice, so both are opt-in. || `version` | latest | Repowise version to install. || `python-version` | `3.12` | Python to run it with (3.11 or newer). || `working-directory` | `.` | Where to run from. |-| `base` | from the pull request | Revision range for coverage and security. |+| `base` | from the pull request | Revision range for coverage, security and risk. || `coverage-report` | config, else discovery | Reports, one per line: a path or a glob (`artifacts/**/lcov.info`), optionally `path=prefix`. || `coverage-fail-under` | `coverage.fail_under` | Minimum patch coverage percent. || `coverage-min-coverable-lines` | `coverage.min_coverable_lines` | Small-change tolerance: a change with fewer changed executable lines than this never fails. |+| `coverage-fail-under-risky` | `coverage.fail_under_risky` | Minimum patch coverage percent over risky files only: hotspots or bug magnets from the index; on git alone, the top quartile of files with bug-fix history. || `doc-drift-baseline` | none | Committed baseline file. || `security-fail-on` | `high` | Lowest severity that fails: `high`, `med`, `low`. || `security-baseline` | none | Committed baseline file. |+| `risk-fail-above-percentile` | none | With `risk` in `checks`, fail when the change ranks above this percentile of recent commits. Empty reports the rank without gating. || `upload-sarif` | `false` | Upload doc drift and security findings to code scanning. |-Outputs: `coverage`, `doc-drift` and `security` hold each gate's exit code-(empty when not run), and `sarif-dir` the SARIF directory.+Outputs: `coverage`, `doc-drift`, `security` and `risk` hold each gate's exit+code (empty when not run), and `sarif-dir` the SARIF directory.-A shallow checkout still works when coverage or security runs: the action+The risk gate ranks the change against the repository's own recent commits,+so at a percentile P roughly (100 - P)% of changes fail it by construction.+A failure asks for a split or a second reviewer, not a code fix, and there is+no baseline to accept it with.++A shallow checkout still works when coverage, security or risk runs: the actionfetches the missing history and the target branch first. Checking out with`fetch-depth: 0` skips that step.@@ -134,6 +147,7 @@ there with a warning; the gates still run and still decide the check.- run: repowise coverage check --report coverage/lcov.info --fail-under 80 --format github- run: repowise doc-drift --check --format github- run: repowise security check --format github+- run: repowise risk --fail-above-percentile 95 --format github```On a pull request the gates read the target branch from `GITHUB_BASE_REF`@@ -158,20 +172,22 @@ repowise-coverage:```It adds one job per gate on merge request pipelines: `repowise-coverage` (only-when `REPOWISE_COVERAGE_REPORT` is set), `repowise-doc-drift` and-`repowise-security`. Each prints its markdown report to the log and keeps it as-an artifact. The doc drift and security jobs also write a Code Quality report-(`gl-code-quality-doc-drift.json`, `gl-code-quality-security.json`), so their-findings show in the merge request's Code Quality widget, even when the gate-fails the job. A fourth job, `repowise-code-quality`, runs on the default-branch and never fails the pipeline: it publishes the report the widget-compares a merge request against, so only issues the merge request introduces-show as new. It publishes doc drift only: security judges only what a change-adds, and the default branch has no change to judge.+when `REPOWISE_COVERAGE_REPORT` is set), `repowise-doc-drift`,+`repowise-security` and `repowise-risk` (only when+`REPOWISE_RISK_FAIL_ABOVE_PERCENTILE` is set). Each prints its markdown report+to the log and keeps it as an artifact. The doc drift and security jobs also+write a Code Quality report (`gl-code-quality-doc-drift.json`,+`gl-code-quality-security.json`), so their findings show in the merge+request's Code Quality widget, even when the gate fails the job. One more job,+`repowise-code-quality`, runs on the default branch and never fails the+pipeline: it publishes the report the widget compares a merge request against,+so only issues the merge request introduces show as new. It publishes doc+drift only: security judges only what a change adds, and the default branch+has no change to judge.`REPOWISE_COVERAGE_REPORT` is space separated and may hold globs and`path=prefix` entries; Repowise expands the globs, not the shell. Othervariables: `REPOWISE_VERSION`, `REPOWISE_COVERAGE_MIN_COVERABLE_LINES`,-`REPOWISE_DOC_DRIFT_BASELINE`,+`REPOWISE_COVERAGE_FAIL_UNDER_RISKY`, `REPOWISE_DOC_DRIFT_BASELINE`,`REPOWISE_SECURITY_BASELINE`, `REPOWISE_SECURITY_FAIL_ON`. Override any job's`image`, `rules` or `needs` in your own file as usual.@@ -238,6 +254,7 @@ coverage:paths: [coverage/lcov.info]fail_under: 80min_coverable_lines: 5 # small-change tolerance+ fail_under_risky: 90 # optional, see Risk-weighted patch coverage```With `min_coverable_lines` set, a change touching fewer changed executable@@ -399,15 +416,40 @@ source in a comment. The block is indented to paste directly below your`coverage:` line. It writes nothing and sets no `fail_under`: keep the gatesyou want and choose their thresholds.+### Risk-weighted patch coverage++An uncovered line in a file that keeps breaking matters more than one in a+file nothing depends on. Every changed file in the coverage report carries its+risk, and the table, the annotations and the summary list the riskiest files+first, with a "Risk" column in plain words ("hotspot, bug-fix weight 3.2, 14+dependents", "none known", or "unknown" when it could not be read). A file is+risky when the index flags it a hotspot or a bug magnet; without an index, or+for a file the change adds, when its recency-weighted bug-fix weight from git+is in the top quartile of the files with bug-fix history. The report says+which basis each row used when not every file had index data.++`--fail-under-risky` (the action's `coverage-fail-under-risky`) adds one more+gate, stricter, over the risky files' changed lines alone, and the check fails+when any gate fails. It follows the same rules as the others, the whole+change's small-change tolerance included. A change that touches no risky file+leaves it not applied (`no_data`, exit `0`). With the gate set, a shallow+clone, or a measured file whose risk could not be read (no git history and no+index row), exits `2`, unless the whole-change or a path-scoped gate already+failed: that failure is reported.+## When a gate exits 2| Message | Fix ||---------|-----|-| no merge-base, or a commit cut off by a shallow clone | fetch full history |-| unknown revision `origin/<branch>` | fetch the target branch |-| no coverage report found | pass `--report`, or set `coverage.paths` |-| `--report ...`: no coverage report matches | fix the path or glob, or upload the report from the test job |-| `coverage.gates[N]`: ... | fix that entry of `coverage.gates` |-| all report entries match coverage.ignore | narrow `coverage.ignore` |-| report paths match no file in the repository | set `coverage.strip_prefix` or `coverage.path_prefix` |-| baseline unreadable | commit the file, or drop `--baseline` |+| "Could not diff ...: ... A shallow CI clone needs the base branch and enough history for a merge-base" | fetch full history |+| "Could not tell which branch this change targets. Pass REVSPEC, e.g. origin/main...HEAD." | fetch the target branch, or pass the range |+| "No coverage report found. Pass one with --report ..." | pass `--report`, or set `coverage.paths` |+| "--report ...: no coverage report matches ..." | fix the path or glob, or upload the report from the test job |+| "coverage.gates[N] ...: ..." | fix that entry of `coverage.gates` |+| "All N report entries match coverage.ignore, so nothing is left to measure." | narrow `coverage.ignore` |+| "No report path matched a file in this repository." | set `coverage.strip_prefix` or `coverage.path_prefix` |+| "cannot read baseline ..." or "baseline ... is not valid JSON" | commit the file, or drop `--baseline` |+| "Only N recent commits could be sampled; ranking a change needs at least 8." (risk) | fetch full history |+| "The change-risk gate ranks the change against recent commits, and --baseline 0 turns that off." | drop `--baseline 0` |+| "The risky-file gate reads bug-fix history, and this clone is shallow." | fetch full history |+| "Could not read the risk of N changed files, so the risky-file gate cannot run." | fetch full history, or index the repository |diff --git a/packages/cli/src/repowise/cli/ci.py b/packages/cli/src/repowise/cli/ci.pyindex 7a1ea10c1..c47c9d79f 100644--- a/packages/cli/src/repowise/cli/ci.py+++ b/packages/cli/src/repowise/cli/ci.py@@ -83,17 +83,23 @@ def repo_root(path: str | None) -> Path:return Path(root)+def ci_revspec(root: str, revspec: str | None) -> str:+ """*revspec*, else ``<CI target branch>...HEAD``: the change a gate judges."""+ from repowise.core.ci.base import BaseNotFoundError, default_revspec++ try:+ return revspec or default_revspec(root)+ except BaseNotFoundError as exc:+ raise CannotEvaluateError("base_not_found", str(exc)) from exc++def change_lines(root: str, revspec: str | None) -> tuple[dict[str, set[int]], str]:"""``({file: new-side lines}, label)`` for *revspec*, else the CI's target branch."""import subprocessfrom repowise.core.analysis.changed_lines import changed_lines- from repowise.core.ci.base import BaseNotFoundError, default_revspec- try:- revspec = revspec or default_revspec(root)- except BaseNotFoundError as exc:- raise CannotEvaluateError("base_not_found", str(exc)) from exc+ revspec = ci_revspec(root, revspec)try:return changed_lines(root, revspec)except ValueError as exc:diff --git a/packages/cli/src/repowise/cli/commands/coverage_check_cmd.py b/packages/cli/src/repowise/cli/commands/coverage_check_cmd.pyindex 7b6578bb3..581baaea8 100644--- a/packages/cli/src/repowise/cli/commands/coverage_check_cmd.py+++ b/packages/cli/src/repowise/cli/commands/coverage_check_cmd.py@@ -9,11 +9,12 @@ executable-line data; otherwise it cannot evaluate.Exit codes and output channels are the shared CI ones (:mod:`repowise.cli.ci`):0 when the gate passes or there is nothing to judge, 1 when patch coverage is-below ``--fail-under`` or a path-scoped gate (``coverage.gates``) that is not-informational fails, 2 when the check could not run (no report, a-``--report`` matching no file, unreadable report, unknown revision, missing-history, bad config). A change under ``--min-coverable-lines`` is reported-against the threshold but exits 0.+below ``--fail-under``, a path-scoped gate (``coverage.gates``) that is not+informational fails, or the risky files' is below ``--fail-under-risky``, 2+when the check could not run (no report, a ``--report`` matching no file,+unreadable report, unknown revision, missing history, bad config, risk+unreadable or a shallow clone under ``--fail-under-risky``). A change under+``--min-coverable-lines`` is reported against the threshold but exits 0."""from __future__ import annotations@@ -37,6 +38,7 @@ from repowise.cli.ci import (from repowise.cli.helpers import console, repo_index_session, run_asyncfrom repowise.cli.output import emit_json, format_optionfrom repowise.core.analysis.health.coverage import PARSERS as COVERAGE_PARSERS+from repowise.core.ci.markdown import pluralfrom repowise.core.persistence.database import has_db_store@@ -72,6 +74,15 @@ from repowise.core.persistence.database import has_db_store"than this is reported but never fails the gate. ""Defaults to coverage.min_coverable_lines in .repowise/config.yaml.",)+ "--fail-under-risky",+ type=click.FloatRange(0, 100),+ default=None,+ help="Exit 1 when patch coverage of the risky files is below this percentage. "+ "Risky: hotspots or bug magnets from the index; on git alone, the top quartile "+ "of files with bug-fix history. No risky file changed: not applied. "+ "Defaults to coverage.fail_under_risky in .repowise/config.yaml.",+)@click.option("--path","repo",@@ -89,6 +100,7 @@ def coverage_check(report_format: str | None,fail_under: float | None,min_coverable_lines: int | None,+ fail_under_risky: float | None,repo: str | None,fmt: str,) -> None:@@ -111,12 +123,17 @@ def coverage_check(repowise coverage check --report 'artifacts/**/lcov.info' --min-coverable-lines 5repowise coverage check --report web/coverage/lcov.info=webrepowise coverage check HEAD --format json++ Each changed file carries its risk (git bug-fix history, plus hotspot,+ bug-magnet and dependent counts when an index exists), and the rows read+ riskiest first. ``--fail-under-risky`` gates the risky files alone."""notices = ci_notices(fmt)try:pc = _evaluate(revspec, reports, report_format, fail_under, min_coverable_lines, repo, notices)+ pc = _with_risk(repo, pc, fail_under_risky)except CannotEvaluateError as exc:cannot_evaluate(fmt, exc.code, str(exc))_emit(pc, fmt)@@ -261,6 +278,86 @@ async def _stored() from exc+def _with_risk(repo, pc, fail_under_risky):+ """*pc* with each row's risk and the risky-file gate.++ Git answers alone; an index, when one opens, adds hotspot, bug-magnet and+ dependent counts. A missing or unreadable index never fails the check. A+ risky-file gate that cannot trust the risk (a shallow clone's truncated+ history, or a measured row with no risk at all) cannot evaluate, unless+ the flat or a path-scoped gate already failed: that verdict stands.+ """+ from repowise.core.analysis.patch_coverage import assess_risks, attach_risk++ root = repo_root(repo)+ threshold = _risky_threshold(root, fail_under_risky)+ paths = [f.file_path for f in pc.files]+ git, index = _read_risk(root, pc.scope.label or None, paths)+ pc = attach_risk(pc, assess_risks(paths, git, index), risky_threshold=threshold)+ # A failure the flat or a path-scoped gate already found stands and is reported.+ if threshold is not None and pc.flat_gate != "fail" and not pc.failing_path_gates:+ _require_trusted_risk(root, pc)+ return pc+++def _risky_threshold(root: Path, flag: float | None) -> float | None:+ """``--fail-under-risky``, else ``coverage.fail_under_risky`` (validated)."""+ if flag is not None:+ return flag+ cfg = _coverage_config(+ root, validate_threshold=False, validate_min_lines=False, validate_risky=True+ )+ return cfg.fail_under_risky+++def _read_risk(root: Path, revspec: str | None, paths: list[str]):+ """``(git fix history or None, index facts)``; git first, never under an open store."""+ from repowise.core.analysis.patch_coverage import read_git_fix_history++ git = read_git_fix_history(str(root), revspec)+ index = run_async(_index_facts(root, paths)) if paths and has_db_store(root) else {}+ return git, index+++def _require_trusted_risk(root: Path, pc) -> None:+ """Raise when the risky-file gate cannot trust the risk it would read."""+ from repowise.core import git_refs+ from repowise.core.analysis.patch_coverage import risk_unreadable++ if git_refs.is_shallow(str(root)):+ # Fix pressure from a truncated history undercounts every file, and the+ # top-quartile rule would call risky files calm.+ raise CannotEvaluateError(+ "history_shallow",+ "The risky-file gate reads bug-fix history, and this clone is shallow. "+ "Fetch full history (fetch-depth: 0, or git fetch --unshallow).",+ )+ if unread := risk_unreadable(pc):+ raise CannotEvaluateError(+ "risk_unavailable",+ f"Could not read the risk of {plural(len(unread), 'changed file')}, so the "+ "risky-file gate cannot run. It needs git history: fetch it (fetch-depth: 0), "+ "or index the repository with `repowise init`.",+ )+++async def _index_facts(root: Path, paths: list[str]) -> dict:+ """Hotspot, bug-magnet and dependent counts from the index; ``{}`` when it cannot say."""+ from sqlalchemy.exc import SQLAlchemyError++ from repowise.core.analysis.patch_coverage import read_index_facts++ async with repo_index_session(root) as opened:+ if opened is None:+ return {}+ session, repo_id = opened+ try:+ return await read_index_facts(session, repo_id, paths)+ except (SQLAlchemyError, OSError, LookupError):+ # An index written by an older version can fail the query itself.+ return {}++def _resolve_reports(root, cfg, report_prefixes, report_format, notices):"""Parse the reports against the files git tracks, failing when nothing usable remains."""from repowise.core import git_refs@@ -302,7 +399,9 @@ def _resolve_reports(root, cfg, report_prefixes, report_format, notices):return resolved-def _coverage_config(root: Path, *, validate_threshold: bool, validate_min_lines: bool):+def _coverage_config(+ root: Path, *, validate_threshold: bool, validate_min_lines: bool, validate_risky: bool = False+):from repowise.core.analysis.health.coverage import CoverageConfigfrom repowise.core.repo_config import RepoConfigError, load_repo_config@@ -315,11 +414,15 @@ def _coverage_config(root: Path, *, validate_threshold: bool, validate_min_linesblock = block if isinstance(block, dict) else {}# A gate that silently stops gating (or starts failing tiny changes) is# worse than no gate, so a value the config cannot use stops the check.- if validate_threshold and block.get("fail_under") is not None and cfg.fail_under is None:- raise CannotEvaluateError(- "config_invalid",- f"coverage.fail_under must be a number from 0 to 100, got {block['fail_under']!r}."- )+ for key, parsed, validate in (+ ("fail_under", cfg.fail_under, validate_threshold),+ ("fail_under_risky", cfg.fail_under_risky, validate_risky),+ ):+ if validate and block.get(key) is not None and parsed is None:+ raise CannotEvaluateError(+ "config_invalid",+ f"coverage.{key} must be a number from 0 to 100, got {block[key]!r}."+ )raw_min = block.get("min_coverable_lines")if validate_min_lines and raw_min is not None and cfg.min_coverable_lines is None:raise CannotEvaluateError(@@ -350,10 +453,19 @@ def _emit(pc, fmt: str) -> None:def _print_summary(pc) -> None:- from repowise.core.analysis.patch_coverage import headline, scope_line+ from repowise.core.analysis.patch_coverage import (+ headline,+ risk_basis_line,+ risky_line,+ scope_line,+ )console.print(escape(headline(pc, markdown=False)))+ if risky := risky_line(pc, markdown=False):+ console.print(escape(risky))console.print(f"[dim]{escape(scope_line(pc, markdown=False))}[/dim]")+ if basis := risk_basis_line(pc):+ console.print(f"[dim]{escape(basis)}.[/dim]")def _print_path_gates(pc) -> None:@@ -381,6 +493,7 @@ def _print_table(pc) -> None:STATUS_TEXT,attention_rows,format_ranges,+ risk_words,)rows = attention_rows(pc)@@ -388,6 +501,7 @@ def _print_table(pc) -> None:returntable = Table(show_edge=False, pad_edge=False)table.add_column("File")+ table.add_column("Risk")table.add_column("Covered", justify="right")table.add_column("Uncovered changed lines")for f in rows:@@ -396,5 +510,5 @@ def _print_table(pc) -> None:gaps = format_ranges(f.uncovered_ranges, RANGE_LIMIT)else:covered, gaps = "", STATUS_TEXT[f.status]- table.add_row(escape(f.file_path), covered, gaps)+ table.add_row(escape(f.file_path), escape(risk_words(f.risk)), covered, gaps)console.print(table)diff --git a/packages/cli/src/repowise/cli/commands/risk_cmd.py b/packages/cli/src/repowise/cli/commands/risk_cmd.pyindex a19284129..e316e76b5 100644--- a/packages/cli/src/repowise/cli/commands/risk_cmd.py+++ b/packages/cli/src/repowise/cli/commands/risk_cmd.py@@ -15,30 +15,50 @@ is the ``get_risk`` MCP tool, and this command is a thin adapter over it — thesame seam ``ask`` / ``context`` / ``symbol`` / ``why`` use. It reads the indexrather than git, so the repo must be indexed.+``--fail-above-percentile P`` makes the REVSPEC path a CI gate on the+repo-relative percentile (never the 0-10 score), with the shared CI exit codes+(:mod:`repowise.cli.ci`): 1 above the gate, 2 when the change cannot be read+or ranked.+Examples:repowise risk # score uncommitted work, else HEADrepowise risk HEAD # score the last commitrepowise risk abc123 # score a single commitrepowise risk main..HEAD # score a branch / PR range as one change+ repowise risk --fail-above-percentile 95 --format github # gate the CI changerepowise risk --target a.py -t b.py # what history says about those files"""from __future__ import annotationsimport json+from dataclasses import dataclassfrom pathlib import Pathimport clickfrom rich.table import Table+from repowise.cli.ci import (+ CI_FORMATS,+ EXIT_GATE_FAILED,+ CannotEvaluateError,+ append_step_summary,+ cannot_evaluate,+ ci_notices,+ ci_revspec,+ repo_root,+)from repowise.cli.commands import _tool_adapters as _ta-from repowise.cli.helpers import console, err_console, repo_index_session, run_async+from repowise.cli.helpers import console, repo_index_session, run_asyncfrom repowise.cli.output import emit_json, format_option, full_optionfrom repowise.core.analysis.change_risk import (+ ChangeRiskResult,change_risk_payload,review_priority_classification,score_live_change,)+from repowise.core.analysis.change_risk.render import ordinal as _ordinal+from repowise.core.analysis.change_risk.render import percentile_text_PRIORITY_LEAD = {"low": "Smaller or more focused than a typical commit in this repo",@@ -450,12 +470,6 @@ def _print_independent_changes(block: dict) -> None:console.print(f" [dim]Basis: {escape(str(block.get('basis') or ''))}.[/dim]")-def _ordinal(n: int) -> str:- """1 -> '1st', 2 -> '2nd', 93 -> '93rd', 11 -> '11th'."""- suffix = "th" if 10 <= n % 100 <= 20 else {1: "st", 2: "nd", 3: "rd"}.get(n % 10, "th")- return f"{n}{suffix}"--@click.command("risk")@click.argument("revspec", required=False, default=None)@click.option(@@ -505,7 +519,22 @@ def _ordinal(n: int) -> str:help="With --target: PR mode. The response leads with a directive naming ""structural review candidates, missing co-changes/tests, and what to run.",)-@format_option()+ "--fail-above-percentile",+ "fail_above",+ type=click.FloatRange(0, 100),+ default=None,+ help="CI gate: exit 1 when the change ranks above this percentile of recent "+ "commits (risk_percentile, never the 0-10 score); exit 2 when it cannot be "+ "ranked. Without REVSPEC it scores the CI change against its target branch.",+)+@format_option(+ choices=CI_FORMATS,+ help="Output format. ``markdown`` and ``github`` are not for --target, and "+ "without REVSPEC they score the CI change against its target branch; "+ "``github`` writes annotations to stdout and the markdown summary to "+ "$GITHUB_STEP_SUMMARY when set.",+)@full_option()def risk_command(revspec: str | None,@@ -515,6 +544,7 @@ def risk_command(exclude: tuple[str, ...],targets: tuple[str, ...],changed_files: tuple[str, ...],+ fail_above: float | None,fmt: str,full: bool,) -> None:@@ -522,10 +552,17 @@ def risk_command(With no --target this scores REVSPEC (a commit, or a ``base..head``range) from its diff. Omit REVSPEC to score your uncommitted work, or- HEAD when the tree is clean. With --target it reports what the repo's- history says about the named files, the same as the get_risk MCP tool.+ HEAD when the tree is clean; with --fail-above-percentile, or a+ markdown or github format, it scores the CI change instead (the pull+ request's target branch...HEAD). With+ --target it reports what the repo's history says about the named files,+ the same as the get_risk MCP tool."""if targets:+ if fmt not in ("table", "json"):+ raise click.UsageError("--target supports --format table or json.")+ if fail_above is not None:+ raise click.UsageError("--fail-above-percentile gates a change, not --target files.")_target_risk(repo_path, targets, changed_files, fmt, full)returnif changed_files:@@ -535,54 +572,104 @@ def risk_command(# payload, and silently ignoring the flag would hand a script that asked for# JSON a rich table and exit 0.fmt = _ta.resolve_format_for(fmt, full)+ subject = _resolve_subject(repo_path, revspec, fmt, fail_above)extensions = tuple(e.strip() for e in ext.split(",")) if ext else ()- status = err_console if fmt != "table" else console-- if baseline and fmt == "table":+ result = _score(subject, extensions, exclude, baseline)+ failed = fail_above is not None and result.percentile > fail_above+ if fmt == "table":+ _print_change(subject, result, failed)+ else:+ _emit_machine(subject, result)+ if failed:+ raise click.exceptions.Exit(EXIT_GATE_FAILED)+++@dataclass(frozen=True)+class _Subject:+ """The change a REVSPEC run scores, and how it reports it."""++ repo_path: str+ revspec: str | None+ fmt: str+ fail_above: float | None+++def _resolve_subject(+ repo_path: str, revspec: str | None, fmt: str, fail_above: float | None+) -> _Subject:+ """The change to score: *revspec*, else the CI change for a gate or CI format."""+ if revspec is None and (fail_above is not None or fmt in ("markdown", "github")):+ # A CI checkout of a pull request is a merge commit, so HEAD alone is+ # the wrong subject: the change is what the branch did since the target.+ try:+ revspec = ci_revspec(str(repo_root(repo_path)), None)+ except CannotEvaluateError as exc:+ cannot_evaluate(fmt, exc.code, str(exc))+ return _Subject(repo_path, revspec, fmt, fail_above)+++def _score(+ subject: _Subject, extensions: tuple[str, ...], exclude: tuple[str, ...], baseline: int+) -> ChangeRiskResult:+ """Score the change, or exit 2 when it cannot be read or, gated, ranked."""+ status = ci_notices(subject.fmt)+ if baseline and subject.fmt == "table":status.print(f"[dim]Sampling up to {baseline} recent commits…[/dim]")try:result = score_live_change(- repo_path,- revspec,+ subject.repo_path,+ subject.revspec,extensions=extensions,exclude_patterns=exclude,baseline=baseline,)except Exception as exc:- # Surface git errors (bad revspec, not a repo) as a clean CLI message.- raise click.ClickException(- f"Could not read change {revspec or 'HEAD'!r} in {repo_path}: {exc}"- ) from exc-- features = result.features- risk = result.risk- percentile = result.percentile- priority = result.priority- request_excludes = result.request_excludes+ # Git errors (bad revspec, not a repo, missing history) cannot be scored.+ cannot_evaluate(+ subject.fmt,+ "change_unreadable",+ f"Could not read change {subject.revspec or 'HEAD'!r} in {subject.repo_path}: {exc}",+ )+ if subject.fail_above is not None and result.percentile is None:+ from repowise.core.analysis.change_risk.render import unranked_reason- if features.nf == 0:+ cannot_evaluate(subject.fmt, "no_baseline", unranked_reason(result, baseline))+ if result.features.nf == 0:status.print(- f"[yellow]No counted file changes in {features.ref!r} "+ f"[yellow]No counted file changes in {result.features.ref!r} "f"(check the revspec, --ext, or exclusion filters).[/yellow]")+ return result+++def _print_change(subject: _Subject, result: ChangeRiskResult, failed: bool) -> None:+ """The table rendering of a scored change."""+ _print_priority(result, subject.fail_above, failed)+ _print_fix_history_and_shape(result)+ groups = _independent_changes_for(subject.repo_path, result, subject.revspec)+ if groups:+ _print_independent_changes(groups)- if fmt == "json":- payload = change_risk_payload(result, scales=True)- groups = _independent_changes_for(repo_path, result, revspec)- if groups:- payload["independent_changes"] = groups- click.echo(json.dumps(payload, indent=2))- return- # Lead with the benchmarked population-relative authority. Without a usable- # baseline, label the offline absolute band explicitly as the fallback.- if percentile is not None and priority is not None:+def _print_priority(result: ChangeRiskResult, fail_above: float | None, failed: bool) -> None:+ """Lead with the benchmarked population-relative authority.++ Without a usable baseline, label the offline absolute band explicitly as+ the fallback.+ """+ risk, priority = result.risk, result.priority+ if result.percentile is not None and priority is not None:console.print(f"\n[bold]Benchmarked review priority[/bold]: "f"{review_priority_classification(priority)} · "- f"{_ordinal(round(percentile))} percentile of recent commits by size and spread"+ f"{percentile_text(result, fail_above)} percentile of recent commits by size and spread")console.print(f" [dim]{_PRIORITY_LEAD[priority]}.[/dim]")+ if fail_above is not None:+ from repowise.core.analysis.change_risk.render import gate_text++ color = "red" if failed else "green"+ console.print(f" [{color}]Gate: {gate_text(result, fail_above)}.[/{color}]")else:color = {"high": "red", "moderate": "yellow", "low": "green"}[risk.level]console.print(@@ -590,7 +677,18 @@ def risk_command("(absolute per-commit band — no repo baseline to rank against)")- # Fix history remains separate evidence about where the change lands.++def _print_fix_history_and_shape(result: ChangeRiskResult) -> None:+ """Where the change lands (its fix record), then the shape of its diff."""+ _print_fix_headline(result)+ _print_change_facts(result)+ _print_hot_files(result)+ _print_diff_shape(result)+++def _print_fix_headline(result: ChangeRiskResult) -> None:+ """Fix history remains separate evidence about where the change lands."""+ features = result.featuresif not result.fix_history_available:console.print(f"\n[bold]Change risk[/bold] for [cyan]{features.ref}[/cyan]: "@@ -611,6 +709,11 @@ def risk_command(f"\n[bold]Change risk[/bold] for [cyan]{features.ref}[/cyan]: ""[green]no bug-fix history in the files it touches[/green]")+++def _print_change_facts(result: ChangeRiskResult) -> None:+ """What was scored: the tree, the subject, the filters and the diff's features."""+ features, request_excludes = result.features, result.request_excludesif result.working_tree:console.print(" [dim]Scoring your uncommitted changes, not the last commit.[/dim]")if features.subject:@@ -624,7 +727,10 @@ def risk_command(f"{'unknown' if features.exp is None else features.exp}"+ (" [magenta](fix)[/magenta]" if features.is_fix else ""))- # The fix record, named in the headline above and itemized here.+++def _print_hot_files(result: ChangeRiskResult) -> None:+ """The fix record, named in the headline above and itemized here."""if result.hot_files:hot = Table(show_header=True)hot.add_column("File")@@ -638,6 +744,10 @@ def risk_command("one from a year earlier counts a half.[/dim]")++def _print_diff_shape(result: ChangeRiskResult) -> None:+ """The supporting 0-10 diff-size score and its drivers."""+ risk = result.riskconsole.print(f" [dim]Diff-size score: {risk.score:.1f}/10 — how big and spread out the change is, "f"not where it lands. Corpus-anchored to a single commit.[/dim]"@@ -657,6 +767,30 @@ def risk_command()console.print(table)- groups = _independent_changes_for(repo_path, result, revspec)- if groups:- _print_independent_changes(groups)++def _emit_machine(subject: _Subject, result: ChangeRiskResult) -> None:+ """The json, markdown and github renderings of a scored change."""+ from repowise.core.analysis.change_risk import render++ fmt, fail_above = subject.fmt, subject.fail_above+ if fmt == "json":+ payload = change_risk_payload(result, scales=True)+ groups = _independent_changes_for(subject.repo_path, result, subject.revspec)+ if groups:+ payload["independent_changes"] = groups+ if fail_above is not None:+ payload["gate"] = {+ "fail_above_percentile": fail_above,+ # Unrounded, as the gate compares it: ``risk_percentile`` is+ # rounded to one decimal and can read as equal to the gate.+ "percentile": result.percentile,+ "status": render.percentile_gate(result, fail_above),+ }+ click.echo(json.dumps(payload, indent=2))+ elif fmt == "markdown":+ click.echo(render.render_markdown(result, fail_above), nl=False)+ else:+ for line in render.github_annotations(result, fail_above):+ click.echo(line)+ append_step_summary(render.render_markdown(result, fail_above))+ console.print(render.headline(result, fail_above, markdown=False), markup=False)diff --git a/packages/core/src/repowise/core/analysis/change_risk/render.py b/packages/core/src/repowise/core/analysis/change_risk/render.pynew file mode 100644index 000000000..3e62a521f--- /dev/null+++ b/packages/core/src/repowise/core/analysis/change_risk/render.py@@ -0,0 +1,163 @@+"""CI renderings of a change's risk: markdown, GitHub annotations, the gate.++The gate reads only ``risk_percentile``, the repo-relative rank the risk+authority names as primary (``risk_semantics.change_risk_authority``), never+the 0-10 diff-shape score. A change with no rank cannot be gated, which is the+caller's cue to exit "could not evaluate" with :func:`unranked_reason`.++Workflow-command and markdown mechanics come from :mod:`repowise.core.ci`,+shared with every other CI gate.+"""++from __future__ import annotations++from typing import Literal++from ...ci import github+from ...ci.markdown import ROW_LIMIT, cell, more_line, plural+from .service import _MIN_BASELINE, ChangeRiskResult++PercentileGate = Literal["pass", "fail", "no_data", "not_set"]+++def percentile_gate(result: ChangeRiskResult, threshold: float | None) -> PercentileGate:+ """``fail`` when the change ranks above *threshold*; unrounded, like every gate."""+ if threshold is None:+ return "not_set"+ if result.percentile is None:+ return "no_data"+ return "fail" if result.percentile > threshold else "pass"+++def unranked_reason(result: ChangeRiskResult, baseline: int) -> str:+ """Why the change has no percentile, and what would give it one."""+ if not baseline:+ return (+ "The change-risk gate ranks the change against recent commits, and "+ "--baseline 0 turns that off. Drop --baseline 0."+ )+ return (+ f"Only {plural(result.baseline_sample_size, 'recent commit')} could be sampled; "+ f"ranking a change needs at least {_MIN_BASELINE}. A shallow clone lacks the "+ "history: fetch it (fetch-depth: 0, or git fetch --unshallow)."+ )+++def ordinal(n: int) -> str:+ """1 -> '1st', 2 -> '2nd', 93 -> '93rd', 11 -> '11th'."""+ suffix = "th" if 10 <= n % 100 <= 20 else {1: "st", 2: "nd", 3: "rd"}.get(n % 10, "th")+ return f"{n}{suffix}"+++def _decimals(value: float) -> str:+ """*value* with the fewest decimals that keep it off a whole number (95.4, 95.001)."""+ for places in range(1, 10):+ text = f"{value:.{places}f}".rstrip("0").rstrip(".")+ if "." in text:+ return text+ return str(round(value))+++def _rank_text(value: float) -> str:+ """An ordinal for a whole number (``95th``), else its decimals (``95.4th``)."""+ return ordinal(round(value)) if value == round(value) else f"{_decimals(value)}th"+++def percentile_number(result: ChangeRiskResult, threshold: float | None = None) -> str | None:+ """The change's rank as a number, rounded unless rounding would contradict the gate."""+ pct = result.percentile+ if pct is None:+ return None+ shown = round(pct)+ # 95.4 rounds to 95, which reads as "not above the 95th percentile gate".+ if threshold is not None and (pct > threshold) != (shown > threshold):+ return _decimals(pct)+ return str(shown)+++def percentile_text(result: ChangeRiskResult, threshold: float | None = None) -> str:+ """:func:`percentile_number` as an ordinal (``97th``, ``95.4th``), or ``unranked``."""+ number = percentile_number(result, threshold)+ if number is None:+ return "unranked"+ return f"{number}th" if "." in number else ordinal(int(number))+++def headline(result: ChangeRiskResult, threshold: float | None, *, markdown: bool = True) -> str:+ """The verdict in words: which side of the gate, then how the change ranks."""+ bold = "**" if markdown else ""+ number = percentile_number(result, threshold)+ if number is None:+ # True for --baseline 0 and for a history of fewer than the minimum.+ return f"{bold}Change risk: unranked{bold} · too few recent commits to rank it"+ rank = f"larger and more spread out than {number}% of recent commits"+ gate = percentile_gate(result, threshold)+ if gate == "fail":+ return f"{bold}Above the {_rank_text(threshold)} percentile gate{bold}: {rank}"+ if gate == "pass":+ return f"{bold}Within the {_rank_text(threshold)} percentile gate{bold}: {rank}"+ return f"{bold}Change risk{bold}: {rank}"+++def gate_text(result: ChangeRiskResult, threshold: float | None) -> str:+ """``"above the 95th percentile gate"``, ``"within ..."``, or ``""`` ungated."""+ gate = percentile_gate(result, threshold)+ if gate == "fail":+ return f"above the {_rank_text(threshold)} percentile gate"+ if gate == "pass":+ return f"within the {_rank_text(threshold)} percentile gate"+ return ""+++def fix_history_text(result: ChangeRiskResult) -> str:+ """Where the change lands: the bug-fix record of the files it touches."""+ if not result.fix_history_available:+ return "fix history unavailable (the git history walk failed)"+ if not result.hot_files:+ return "no bug-fix history in the files it touches"+ where = (+ f" ({ordinal(round(result.fix_percentile))} percentile of recent commits "+ "by bug-fix history)"+ if result.fix_percentile is not None+ else ""+ )+ return f"touches files that have broken before{where}"+++def scope_line(result: ChangeRiskResult, *, markdown: bool = True) -> str:+ """The change, the sample it was ranked against, and its fix history."""+ ref = result.features.ref+ parts = [f"`{ref}`" if markdown else ref]+ if result.baseline_sample_size:+ parts.append(f"ranked against {plural(result.baseline_sample_size, 'recent commit')}")+ features = result.features+ parts.append(+ f"+{features.la} / -{features.ld} lines in {plural(features.nf, 'file')} "+ f"across {plural(features.nd, 'directory', 'directories')}"+ )+ parts.append(fix_history_text(result))+ return " · ".join(parts)+++def render_markdown(result: ChangeRiskResult, threshold: float | None = None) -> str:+ """Markdown for a CI step summary or merge-request report."""+ out = [headline(result, threshold), "", scope_line(result)]+ hot = result.hot_files+ if hot:+ out += ["", "| File | Changed lines | Recent bug fixes |", "|---|---|---|"]+ out += [+ f"| `{cell(path)}` | {churn} | {pressure:.1f} |"+ for path, churn, pressure in hot[:ROW_LIMIT]+ ]+ if len(hot) > ROW_LIMIT:+ out += ["", more_line(len(hot) - ROW_LIMIT, "files with bug-fix history")]+ out += ["", "Bug fixes are recency-weighted: one from a year earlier counts a half."]+ return "\n".join(out) + "\n"+++def github_annotations(result: ChangeRiskResult, threshold: float | None = None) -> list[str]:+ """An ``::error::`` when the gate fails, else a ``::notice::`` naming the rank."""+ text = headline(result, threshold, markdown=False)+ if percentile_gate(result, threshold) == "fail":+ return [github.error(text)]+ return [github.notice(text)]diff --git a/packages/core/src/repowise/core/analysis/change_risk/service.py b/packages/core/src/repowise/core/analysis/change_risk/service.pyindex d5ddc34eb..5d000035d 100644--- a/packages/core/src/repowise/core/analysis/change_risk/service.py+++ b/packages/core/src/repowise/core/analysis/change_risk/service.py@@ -118,6 +118,26 @@ def range_anchor(repo_path: str, base: str, head: str) -> str:return merge_base or base+def history_ref(repo_path: str, revspec: str | None, *, working_tree: bool = False) -> str:+ """The ref a change's fix history is read at: strictly before the change.++ Uncommitted work reads at ``HEAD``; a range at its merge-base, not at+ ``base``'s tip (with three dots the diff starts there, so base's later+ commits are not this change's ground); a commit at its parent, so it is+ never credited with fixes that only landed because of it. A root commit's+ parent does not resolve, which yields an empty record: the honest answer+ for the first commit in a repository.+ """+ if working_tree:+ return "HEAD"+ target = revspec or "HEAD"+ parts = split_revspec(target)+ if parts is not None:+ base, _, head = parts+ return range_anchor(repo_path, base, head)+ return f"{target}^"++def normalize_extensions(extensions: tuple[str, ...]) -> tuple[str, ...]:"""Add a leading dot to requested suffixes, matching the CLI contract."""return tuple(ext if ext.startswith(".") else f".{ext}" for ext in extensions)@@ -156,10 +176,8 @@ def score_live_change(# A tree dirty only in paths the filters drop is not a change this command# can score, so fall through to HEAD rather than answer "empty change".working_tree = uncommitted is not None and uncommitted.nf > 0- # Ref whose history the fix record is read from. Strictly *before* the- # change being scored, so a commit is never credited with fixes that only- # landed because of it.- history_ref = "HEAD"+ # Ref whose history the fix record is read from (see ``history_ref``).+ history = history_ref(repo_path, target, working_tree=working_tree)if working_tree:features = uncommittedanchor, excluded_ref = "HEAD", ""@@ -173,23 +191,17 @@ def score_live_change(exclude_patterns=effective_excludes,sep=sep,)- # Fix history is read at the fork point, not at ``base``'s tip: with- # three-dot syntax the diff starts at the merge-base, so base's later- # commits are not part of this change's ground.- anchor = range_anchor(repo_path, base, head)+ # The baseline shares the fix history's anchor: the range's merge-base.+ anchor = historyexcluded_ref = ""- history_ref = anchorelse:features = extract_commit_features(repo_path, target, extensions=extensions, exclude_patterns=effective_excludes)anchor, excluded_ref = _commit_anchor(repo_path, target)- # A root commit has no parent; its own ref then yields an empty record,- # which is the honest answer for the first commit in a repository.- history_ref = f"{target}^"try:- pressure: dict[str, float] | None = fix_pressure(repo_path, history_ref)+ pressure: dict[str, float] | None = fix_pressure(repo_path, history)except FixHistoryUnavailableError:pressure = Nonediff --git a/packages/core/src/repowise/core/analysis/health/coverage/discovery.py b/packages/core/src/repowise/core/analysis/health/coverage/discovery.pyindex f6ea2e0ec..23a336cdd 100644--- a/packages/core/src/repowise/core/analysis/health/coverage/discovery.py+++ b/packages/core/src/repowise/core/analysis/health/coverage/discovery.py@@ -138,6 +138,8 @@ class CoverageConfig:# message; read-only surfaces evaluate the valid entries.gates: tuple[PathGate, ...] = ()gate_errors: tuple[str, ...] = ()+ # The stricter gate over risky files only (``coverage check --fail-under-risky``).+ fail_under_risky: float | None = None@classmethoddef from_repo_config(cls, repo_config: dict | None) -> CoverageConfig:@@ -168,6 +170,7 @@ class CoverageConfig:min_coverable_lines=_line_count(block.get("min_coverable_lines")),gates=gates,gate_errors=gate_errors,+ fail_under_risky=_percent(block.get("fail_under_risky")),)def reports(self, repo_root: Path) -> dict[Path, str | None]:diff --git a/packages/core/src/repowise/core/analysis/patch_coverage/__init__.py b/packages/core/src/repowise/core/analysis/patch_coverage/__init__.pyindex 3abc51772..babd206dd 100644--- a/packages/core/src/repowise/core/analysis/patch_coverage/__init__.py+++ b/packages/core/src/repowise/core/analysis/patch_coverage/__init__.py@@ -23,19 +23,36 @@ from .render import (path_gate_row,path_gate_verdict,render_markdown,+ risk_basis_line,+ risk_words,+ risky_line,scope_line,)-from .stored import stored_patch_coverage+from .risk import (+ FileRisk,+ GitFixHistory,+ IndexFacts,+ assess_risks,+ attach_risk,+ read_git_fix_history,+ risk_unreadable,+)+from .stored import read_index_facts, stored_patch_coverage__all__ = ["RANGE_LIMIT","STATUS_TEXT","FilePatchCoverage",+ "FileRisk","FileStatus","GateStatus",+ "GitFixHistory",+ "IndexFacts","PatchCoverage","PatchScope","PathGateResult",+ "assess_risks",+ "attach_risk","attention_rows","compute_patch_coverage","fmt_pct",@@ -45,7 +62,13 @@ __all__ = ["patch_coverage_from_resolved","path_gate_row","path_gate_verdict",+ "read_git_fix_history",+ "read_index_facts","render_markdown",+ "risk_basis_line",+ "risk_unreadable",+ "risk_words",+ "risky_line","scope_line","stored_patch_coverage",]diff --git a/packages/core/src/repowise/core/analysis/patch_coverage/compute.py b/packages/core/src/repowise/core/analysis/patch_coverage/compute.pyindex 23e4447fc..bacecd574 100644--- a/packages/core/src/repowise/core/analysis/patch_coverage/compute.py+++ b/packages/core/src/repowise/core/analysis/patch_coverage/compute.py@@ -41,6 +41,7 @@ from ..health.coverage.model import FileCoverageif TYPE_CHECKING:from ..health.coverage.discovery import PathGate, ResolvedCoverage+ from .risk import FileRiskFileStatus = Literal["measured", "not_in_report", "no_line_data", "no_coverable_changes"]GateStatus = Literal["pass", "fail", "no_data", "not_set", "too_small"]@@ -57,6 +58,8 @@ class FilePatchCoverage:covered_line_count: int = 0# Inclusive (start, end) runs of changed, executable, unexecuted lines.uncovered_ranges: tuple[tuple[int, int], ...] = ()+ # What history says about the file (``risk.attach_risk``); ``None`` unread.+ risk: FileRisk | None = None@propertydef patch_coverage_pct(self) -> float | None:@@ -75,6 +78,7 @@ class FilePatchCoverage:"covered_line_count": self.covered_line_count,"patch_coverage_pct": _round(self.patch_coverage_pct),"uncovered_ranges": [list(r) for r in self.uncovered_ranges],+ "risk": self.risk.to_dict() if self.risk is not None else None,}@@ -190,6 +194,8 @@ class PatchCoverage:# Path-scoped gates, in config order. A failing one that is not# informational fails ``gate``; ``flat_gate`` ignores them.path_gates: tuple[PathGateResult, ...] = ()+ # A stricter gate over the risky files only (``risk.attach_risk``).+ risky_threshold: float | None = None@propertydef changed_file_count(self) -> int:@@ -222,16 +228,67 @@ class PatchCoverage:@propertydef gate(self) -> GateStatus:- """``flat_gate``, or ``fail`` when a path-scoped gate fails the change."""- return "fail" if self.failing_path_gates else self.flat_gate+ """``flat_gate``, or ``fail`` when a path-scoped or the risky-file gate fails.++ With no flat threshold the risky-file gate's status is the verdict.+ """+ if self.failing_path_gates or self.risky_gate == "fail":+ return "fail"+ flat = self.flat_gate+ return self.risky_gate if flat == "not_set" else flat@propertydef failing_path_gates(self) -> list[PathGateResult]:return [g for g in self.path_gates if g.fails_change]+ @property+ def risky_files(self) -> list[FilePatchCoverage]:+ """Measured files whose risk marks them risky."""+ return [f for f in self.with_status("measured") if f.risk is not None and f.risk.risky]++ @property+ def risky_covered_line_count(self) -> int:+ return sum(f.covered_line_count for f in self.risky_files)++ @property+ def risky_coverable_line_count(self) -> int:+ return sum(f.coverable_line_count for f in self.risky_files)++ @property+ def risky_pct(self) -> float | None:+ return _pct(self.risky_covered_line_count, self.risky_coverable_line_count)++ @property+ def risky_gate(self) -> GateStatus:+ """``risky_threshold`` over the risky files' lines, by the one gate rule.++ No risky file is ``no_data``. Like a path gate it takes the whole+ change's small-change tolerance: a small change is exempt, a small+ risky slice of a big one is not.+ """+ return _gate(+ self.risky_threshold,+ self.risky_covered_line_count,+ self.risky_coverable_line_count,+ self.small_change,+ )+def with_status(self, status: FileStatus) -> list[FilePatchCoverage]:return [f for f in self.files if f.status == status]+ def _risky_dict(self) -> dict[str, Any] | None:+ # Null when no row carries risk: "not assessed" is not "nothing risky".+ if all(f.risk is None for f in self.files):+ return None+ return {+ "file_count": len(self.risky_files),+ "covered_line_count": self.risky_covered_line_count,+ "coverable_line_count": self.risky_coverable_line_count,+ "patch_coverage_pct": _round(self.risky_pct),+ "threshold": self.risky_threshold,+ "gate": self.risky_gate,+ }+def to_dict(self) -> dict[str, Any]:counts = {status: len(self.with_status(status)) for status in get_args(FileStatus)}counts["out_of_scope"] = self.out_of_scope_count@@ -246,6 +303,7 @@ class PatchCoverage:"files": [f.to_dict() for f in self.files],"scope": self.scope.to_dict(),"path_gates": [g.to_dict() for g in self.path_gates],+ "risky": self._risky_dict(),}diff --git a/packages/core/src/repowise/core/analysis/patch_coverage/render.py b/packages/core/src/repowise/core/analysis/patch_coverage/render.pyindex ecd62f477..81c2dda8a 100644--- a/packages/core/src/repowise/core/analysis/patch_coverage/render.py+++ b/packages/core/src/repowise/core/analysis/patch_coverage/render.py@@ -18,6 +18,7 @@ from collections.abc import Sequencefrom ...ci import githubfrom ...ci.markdown import ROW_LIMIT, cell, details, more_line, pluralfrom .compute import FilePatchCoverage, PatchCoverage, PathGateResult+from .risk import FileRisk#: Uncovered ranges shown per file before "+N more".RANGE_LIMIT = 8@@ -56,19 +57,22 @@ def _flat_headline(pc: PatchCoverage, *, markdown: bool) -> str:f"{bold}Patch coverage {fmt_pct(pct)}{bold} ({pc.covered_line_count} of "f"{pc.coverable_line_count} changed executable lines covered)")- gate = pc.flat_gate+ return text + _gate_suffix(pc.flat_gate, pc.threshold, pc.min_coverable_lines, "gate")+++def _gate_suffix(gate: str, threshold: float | None, min_lines: int | None, noun: str) -> str:+ """`` · meets the 80.0% gate`` and its kin, or ``""`` for an unset or unjudged gate."""if gate == "fail":- text += f" · below the {fmt_pct(pc.threshold)} gate"- elif gate == "pass":- text += f" · meets the {fmt_pct(pc.threshold)} gate"- elif gate == "too_small":- n = pc.min_coverable_lines- lines = "line" if n == 1 else "lines"- text += (- f" · below the {fmt_pct(pc.threshold)} gate, not applied: fewer than {n} "+ return f" · below the {fmt_pct(threshold)} {noun}"+ if gate == "pass":+ return f" · meets the {fmt_pct(threshold)} {noun}"+ if gate == "too_small":+ lines = "line" if min_lines == 1 else "lines"+ return (+ f" · below the {fmt_pct(threshold)} {noun}, not applied: fewer than {min_lines} "f"changed executable {lines} (min_coverable_lines)")- return text+ return ""def path_gate_verdict(g: PathGateResult) -> str:@@ -125,6 +129,80 @@ def _no_data_words(g: PathGateResult) -> str:return _PATH_GATE_TEXT["no_data"] + unmeasured+def risky_line(pc: PatchCoverage, *, markdown: bool = True) -> str:+ """The verdict over risky files, or ``""`` when there is nothing to say."""+ if all(f.risk is None for f in pc.files):+ return ""+ bold = "**" if markdown else ""+ pct = pc.risky_pct+ if pct is None:+ if pc.risky_threshold is None:+ return ""+ return (+ "No changed executable line is in a file history marks as risky. "+ f"The {fmt_pct(pc.risky_threshold)} risky-file gate was not applied."+ )+ text = (+ f"{bold}Risky files {fmt_pct(pct)}{bold} ({pc.risky_covered_line_count} of "+ f"{pc.risky_coverable_line_count} changed executable lines covered in "+ f"{plural(len(pc.risky_files), 'risky file')})"+ )+ return text + _gate_suffix(+ pc.risky_gate, pc.risky_threshold, pc.min_coverable_lines, "risky-file gate"+ )+++def risk_order_key(f: FilePatchCoverage) -> tuple:+ """Risky first, then fix pressure, then dependents, then uncovered lines."""+ risk = f.risk or FileRisk()+ return (+ not risk.risky,+ -(risk.fix_pressure or 0.0),+ -(risk.dependents or 0),+ -f.uncovered_line_count,+ f.file_path,+ )+++def risk_words(risk: FileRisk | None) -> str:+ """``"hotspot, bug-fix weight 3.2, 14 dependents"``, ``"none known"`` or ``"unknown"``."""+ if risk is None or risk.basis == "unavailable":+ return "unknown"+ parts = list(risk.reasons)+ if risk.fix_pressure:+ parts.append(f"bug-fix weight {risk.fix_pressure:.1f}")+ if risk.dependents:+ parts.append(plural(risk.dependents, "dependent"))+ return ", ".join(parts) or "none known"+++def risk_basis_line(pc: PatchCoverage) -> str:+ """Where the risk came from, when that is not the index for every row; else ``""``."""+ bases = [f.risk.basis for f in pc.files if f.risk is not None]+ unknown = bases.count("unavailable")+ parts = [+ _git_only_text(bases.count("git"), len(bases)),+ "git fix history could not be read" if "index" in bases else "",+ f"risk could not be read for {plural(unknown, 'file')}" if unknown else "",+ ]+ return " · ".join(part for part in parts if part)+++def _git_only_text(git_only: int, total: int) -> str:+ """The basis clause for rows the index has no data for, or ``""``."""+ if not git_only:+ return ""+ if git_only == total:+ return (+ "Risk is from git bug-fix history alone; an index adds hotspot, bug-magnet "+ "and dependent counts"+ )+ return (+ f"Risk for {git_only} of {plural(total, 'file')} is from git bug-fix "+ "history alone: the index has no row for them yet"+ )++def scope_line(pc: PatchCoverage, *, markdown: bool = True) -> str:"""The diff, the reports and how many changed files were measured."""scope = pc.scope@@ -154,75 +232,128 @@ def scope_line(pc: PatchCoverage, *, markdown: bool = True) -> str:def attention_rows(pc: PatchCoverage) -> list[FilePatchCoverage]:- """Files a reader should look at: uncovered changes first, then unmeasurable ones."""+ """Files a reader should look at: uncovered changes first, then unmeasurable ones.++ Within each group the riskiest file leads (:func:`risk_order_key`).+ """gaps = sorted(- (f for f in pc.with_status("measured") if f.uncovered_line_count),- key=lambda f: (-f.uncovered_line_count, f.file_path),+ (f for f in pc.with_status("measured") if f.uncovered_line_count), key=risk_order_key)- return gaps + pc.with_status("not_in_report") + pc.with_status("no_line_data")+ unmeasured = sorted(+ pc.with_status("not_in_report") + pc.with_status("no_line_data"),+ key=lambda f: (f.status != "not_in_report", *risk_order_key(f)),+ )+ return gaps + unmeasureddef render_markdown(pc: PatchCoverage) -> str:"""Markdown for a CI step summary or PR comment."""- out = [headline(pc), "", scope_line(pc)]- if pc.path_gates:- out += ["", "| Path-scoped gate | Verdict | Covered changed lines | Threshold |"]- out += ["|---|---|---|---|"]- # Gates failing the change first, so the cap never hides one.- for g in sorted(pc.path_gates, key=lambda g: not g.fails_change)[:ROW_LIMIT]:- name, verdict, covered, threshold = path_gate_row(g)- out.append(f"| `{cell(name)}` | {verdict} | {covered} | {threshold} |")- if len(pc.path_gates) > ROW_LIMIT:- out += ["", more_line(len(pc.path_gates) - ROW_LIMIT, "path-scoped gates")]+ out = [headline(pc)]+ if risky := risky_line(pc):+ out += ["", risky]+ out += ["", scope_line(pc)]+ if basis := risk_basis_line(pc):+ out += ["", basis + "."]+ out += _path_gate_table(pc)+ out += _gap_table(pc)+ out += _details(pc.with_status("not_in_report"), "not in the coverage report")+ out += _details(pc.with_status("no_line_data"), "in a report without line data")+ return "\n".join(out) + "\n"+++def _path_gate_table(pc: PatchCoverage) -> list[str]:+ if not pc.path_gates:+ return []+ out = ["", "| Path-scoped gate | Verdict | Covered changed lines | Threshold |", "|---|---|---|---|"]+ # Gates failing the change first, so the cap never hides one.+ for g in sorted(pc.path_gates, key=lambda g: not g.fails_change)[:ROW_LIMIT]:+ name, verdict, covered, threshold = path_gate_row(g)+ out.append(f"| `{cell(name)}` | {verdict} | {covered} | {threshold} |")+ if len(pc.path_gates) > ROW_LIMIT:+ out += ["", more_line(len(pc.path_gates) - ROW_LIMIT, "path-scoped gates")]+ return out+++def _gap_table(pc: PatchCoverage) -> list[str]:+ """Files with uncovered changed lines, riskiest first; a Risk column once risk is read."""gaps = [f for f in attention_rows(pc) if f.status == "measured"]- if gaps:- out += ["", "| File | Uncovered changed lines | Covered |", "|---|---|---|"]+ if not gaps:+ return []+ if any(f.risk is not None for f in pc.files):+ out = ["", "| File | Risk | Uncovered changed lines | Covered |", "|---|---|---|---|"]+ out += [+ f"| `{cell(f.file_path)}` | {cell(risk_words(f.risk))} | "+ f"{format_ranges(f.uncovered_ranges, RANGE_LIMIT)} | "+ f"{f.covered_line_count} of {f.coverable_line_count} |"+ for f in gaps[:ROW_LIMIT]+ ]+ else:+ out = ["", "| File | Uncovered changed lines | Covered |", "|---|---|---|"]out += [f"| `{cell(f.file_path)}` | {format_ranges(f.uncovered_ranges, RANGE_LIMIT)} | "f"{f.covered_line_count} of {f.coverable_line_count} |"for f in gaps[:ROW_LIMIT]]- if len(gaps) > ROW_LIMIT:- out += ["", more_line(len(gaps) - ROW_LIMIT, "files with uncovered changed lines")]- out += _details(pc.with_status("not_in_report"), "not in the coverage report")- out += _details(pc.with_status("no_line_data"), "in a report without line data")- return "\n".join(out) + "\n"+ if len(gaps) > ROW_LIMIT:+ out += ["", more_line(len(gaps) - ROW_LIMIT, "files with uncovered changed lines")]+ return outdef github_annotations(pc: PatchCoverage) -> list[str]:"""GitHub Actions workflow commands for a change's patch coverage.A failed gate is an error, and so is each failing path-scoped gate that is- not informational; one exempted by the small-change tolerance, and an- informational gate below its threshold, is a notice, so neither is silent. The largest uncovered ranges are- marked, capped at what GitHub displays, with a notice counting the rest.+ not informational and a failing risky-file gate; one exempted by the+ small-change tolerance, and an informational gate below its threshold, is+ a notice, so neither is silent. Uncovered ranges are marked riskiest file+ first, then largest range, capped at what GitHub displays, with a notice+ counting the rest."""ranges = sorted(- ((f.file_path, a, b) for f in pc.with_status("measured") for a, b in f.uncovered_ranges),- key=lambda r: (r[1] - r[2], r[0], r[1]),+ ((f, a, b) for f in pc.with_status("measured") for a, b in f.uncovered_ranges),+ key=lambda r: (*risk_order_key(r[0])[:3], r[1] - r[2], r[0].file_path, r[1]),)- warnings = [- github.annotation(- "warning",- f"Changed {_span(a, b)} not covered by tests",- file=path,- line=a,- end_line=b,- title="Uncovered change",- )- for path, a, b in ranges+ warnings = [_range_annotation(f, a, b) for f, a, b in ranges]+ verdict = [+ *_gate_annotations(pc.flat_gate, _flat_headline(pc, markdown=False)),+ *_path_gate_annotations(pc),+ *_gate_annotations(pc.risky_gate, risky_line(pc, markdown=False)),]- verdict = []- if pc.flat_gate == "fail":- verdict = [github.error(_flat_headline(pc, markdown=False))]- elif pc.flat_gate == "too_small":- verdict = [github.notice(_flat_headline(pc, markdown=False))]+ return verdict + github.cap_annotations(warnings, noun="uncovered changed ranges")+++def _gate_annotations(gate: str, text: str) -> list[str]:+ """An error for a failed gate, a notice for one the tolerance exempted."""+ if gate == "fail":+ return [github.error(text)]+ if gate == "too_small":+ return [github.notice(text)]+ return []+++def _path_gate_annotations(pc: PatchCoverage) -> list[str]:+ out = []for g in pc.path_gates:if g.fails_change:- verdict.append(github.error(f"Fails: {path_gate_verdict(g)}."))+ out.append(github.error(f"Fails: {path_gate_verdict(g)}."))elif g.informational and g.gate == "fail":- verdict.append(github.notice(f"Informational: {path_gate_verdict(g)}."))- return verdict + github.cap_annotations(warnings, noun="uncovered changed ranges")+ out.append(github.notice(f"Informational: {path_gate_verdict(g)}."))+ return out+++def _range_annotation(f: FilePatchCoverage, a: int, b: int) -> str:+ risky = f.risk is not None and f.risk.risky+ message = f"Changed {_span(a, b)} not covered by tests"+ if risky:+ message += f" ({risk_words(f.risk)})"+ return github.annotation(+ "warning",+ message,+ file=f.file_path,+ line=a,+ end_line=b,+ title="Uncovered change in a risky file" if risky else "Uncovered change",+ )def format_ranges(ranges: Sequence[tuple[int, int]], limit: int | None = None) -> str:diff --git a/packages/core/src/repowise/core/analysis/patch_coverage/risk.py b/packages/core/src/repowise/core/analysis/patch_coverage/risk.pynew file mode 100644index 000000000..7c249f918--- /dev/null+++ b/packages/core/src/repowise/core/analysis/patch_coverage/risk.py@@ -0,0 +1,173 @@+"""Risk of the files a change's uncovered lines sit in.++An uncovered line in a file that keeps breaking matters more than one in a+file nobody depends on, so every patch-coverage row carries the risk of its+file and the rows are read riskiest first.++"Risky" reuses rules the repository already has rather than inventing new+thresholds. With index data for the file it is the index's own flags+(``is_hotspot`` or ``bug_magnet``, the ones the untested-hotspot and+bug-magnet findings read). Without it (no index, or a file the change adds)+it falls back to git: among the tracked files with any bug-fix history, the+file's recency-decayed fix pressure is in the top quartile, the 0.75 cut the+hotspot rule uses.++The assessment is pure; :func:`read_git_fix_history` does the git IO and+``stored.read_index_facts`` the index read. Renderings live in ``render``.+"""++from __future__ import annotations++import bisect+import subprocess+from collections.abc import Iterable, Mapping+from dataclasses import dataclass, replace+from typing import TYPE_CHECKING, Any, Literal++from ..change_risk.fix_history import FixHistoryUnavailableError, fix_pressure+from ..change_risk.service import history_ref++if TYPE_CHECKING:+ from .compute import PatchCoverage++#: Where a row's risk came from. ``index`` is the index without git history+#: (the walk failed); ``unavailable`` is neither.+RiskBasis = Literal["git", "index", "git_and_index", "unavailable"]++#: The percent rank a file's fix pressure must reach to count as risky on git+#: alone. The same top-quartile cut ``is_hotspot`` applies to churn.+TOP_QUARTILE = 0.75++TOP_QUARTILE_REASON = "top quartile of files with bug-fix history"+++@dataclass(frozen=True)+class FileRisk:+ """What history says about one changed file."""++ #: Recency-decayed bug-fix count from git; ``None`` when git was not read.+ fix_pressure: float | None = None+ #: Files that import this one, from the index graph.+ dependents: int | None = None+ hotspot: bool | None = None+ bug_magnet: bool | None = None+ basis: RiskBasis = "unavailable"+ risky: bool = False+ reasons: tuple[str, ...] = ()++ def to_dict(self) -> dict[str, Any]:+ return {+ "fix_pressure": None if self.fix_pressure is None else round(self.fix_pressure, 2),+ "dependents": self.dependents,+ "hotspot": self.hotspot,+ "bug_magnet": self.bug_magnet,+ "basis": self.basis,+ "risky": self.risky,+ "reasons": list(self.reasons),+ }+++@dataclass(frozen=True)+class IndexFacts:+ """One file's index row: its git_metadata flags and import fan-in."""++ hotspot: bool+ bug_magnet: bool+ dependents: int | None = None+++@dataclass(frozen=True)+class GitFixHistory:+ """Per-file fix pressure, and the population a file ranks against."""++ pressure: Mapping[str, float]+ #: One pressure per tracked file with any bug-fix history, sorted ascending.+ population: tuple[float, ...]+++def _in_top_quartile(value: float, population: tuple[float, ...]) -> bool:+ # Percent rank as SQL computes it for ``is_hotspot``: the share of the+ # others strictly below this value. The population is files with any fix+ # history, so "has been fixed once" alone never qualifies in a repository+ # where most files have. Fewer than two such files rank nothing (SQL's+ # percent_rank of a lone row is 0). Ceiling: ranks against files tracked+ # now, so a file deleted since keeps no weight; fine for a per-change read.+ if value <= 0 or len(population) < 2:+ return False+ below = bisect.bisect_left(population, value)+ return below / (len(population) - 1) >= TOP_QUARTILE+++def assess_file_risk(+ path: str, git: GitFixHistory | None, index: IndexFacts | None+) -> FileRisk:+ """One file's :class:`FileRisk` from whatever was read."""+ pressure = None if git is None else git.pressure.get(path, 0.0)+ if index is not None:+ reasons = tuple(+ word+ for flag, word in ((index.hotspot, "hotspot"), (index.bug_magnet, "bug magnet"))+ if flag+ )+ return FileRisk(+ fix_pressure=pressure,+ dependents=index.dependents,+ hotspot=index.hotspot,+ bug_magnet=index.bug_magnet,+ basis="index" if git is None else "git_and_index",+ risky=bool(reasons),+ reasons=reasons,+ )+ if git is None:+ return FileRisk()+ risky = _in_top_quartile(pressure or 0.0, git.population)+ return FileRisk(+ fix_pressure=pressure,+ basis="git",+ risky=risky,+ reasons=(TOP_QUARTILE_REASON,) if risky else (),+ )+++def assess_risks(+ paths: Iterable[str], git: GitFixHistory | None, index: Mapping[str, IndexFacts]+) -> dict[str, FileRisk]:+ """``{path: FileRisk}`` for every path."""+ return {path: assess_file_risk(path, git, index.get(path)) for path in paths}+++def attach_risk(+ pc: PatchCoverage,+ risks: Mapping[str, FileRisk],+ *,+ risky_threshold: float | None = None,+) -> PatchCoverage:+ """*pc* with each row's risk set, and the risky-files gate threshold."""+ files = tuple(replace(f, risk=risks.get(f.file_path)) for f in pc.files)+ return replace(pc, files=files, risky_threshold=risky_threshold)+++def risk_unreadable(pc: PatchCoverage) -> list[str]:+ """Measured rows whose risk could not be read: the rows the risky gate reads."""+ return [+ f.file_path+ for f in pc.with_status("measured")+ if f.risk is None or f.risk.basis == "unavailable"+ ]+++def read_git_fix_history(+ repo_path: str, revspec: str | None, *, working_tree: bool = False+) -> GitFixHistory | None:+ """The change's fix history and its population, or ``None`` when git cannot say."""+ from ... import git_refs++ try:+ pressure = fix_pressure(+ repo_path, history_ref(repo_path, revspec, working_tree=working_tree)+ )+ except (FixHistoryUnavailableError, subprocess.SubprocessError, OSError):+ return None+ tracked = git_refs.tracked_paths(repo_path)+ fixed = (pressure.get(p, 0.0) for p in tracked)+ return GitFixHistory(pressure, tuple(sorted(v for v in fixed if v > 0)))diff --git a/packages/core/src/repowise/core/analysis/patch_coverage/stored.py b/packages/core/src/repowise/core/analysis/patch_coverage/stored.pyindex 91c16d830..bc9b1e787 100644--- a/packages/core/src/repowise/core/analysis/patch_coverage/stored.py+++ b/packages/core/src/repowise/core/analysis/patch_coverage/stored.py@@ -14,6 +14,7 @@ from sqlalchemy.ext.asyncio import AsyncSessionfrom ..health.coverage.freshness import coverage_freshnessfrom .compute import PatchCoverage, PatchScope, compute_patch_coverage+from .risk import IndexFactsif TYPE_CHECKING:from ..health.coverage.discovery import CoverageConfig@@ -79,3 +80,30 @@ async def stored_patch_coverage(config_errors=cfg.gate_errors,),)+++async def read_index_facts(+ session: AsyncSession, repository_id: str, paths: Iterable[str]+) -> dict[str, IndexFacts]:+ """``{path: IndexFacts}`` for the paths the index has a git row for.++ A path with no row (a file the change adds) is absent, so its risk falls+ back to git.+ """+ from repowise.core.ingestion.models import FILE_DEPENDENCY_EDGE_TYPES+ from repowise.core.persistence.crud import get_git_metadata_bulk, get_node_degree_counts_bulk++ meta = await get_git_metadata_bulk(session, repository_id, sorted(set(paths)))+ if not meta:+ return {}+ degrees = await get_node_degree_counts_bulk(+ session, repository_id, list(meta), edge_types=sorted(FILE_DEPENDENCY_EDGE_TYPES)+ )+ return {+ path: IndexFacts(+ hotspot=bool(row.is_hotspot),+ bug_magnet=bool(row.bug_magnet),+ dependents=degrees[path]["in_degree"] if path in degrees else None,+ )+ for path, row in meta.items()+ }diff --git a/packages/core/src/repowise/core/git_refs.py b/packages/core/src/repowise/core/git_refs.pyindex 0b1dcd25c..5f7d5c117 100644--- a/packages/core/src/repowise/core/git_refs.py+++ b/packages/core/src/repowise/core/git_refs.py@@ -23,6 +23,7 @@ __all__ = ["current_branch","default_base","files_by_ref",+ "is_shallow","list_branches","refs_containing","refs_merged_into",@@ -74,6 +75,11 @@ def tracked_paths(repo_path: str) -> frozenset[str]:return frozenset(p for p in _read(repo_path, ["ls-files", "-z"]).split("\0") if p)+def is_shallow(repo_path: str) -> bool:+ """Whether the clone is shallow; ``False`` when git cannot answer."""+ return _read(repo_path, ["rev-parse", "--is-shallow-repository"]) == "true"++def current_branch(repo_path: str) -> str | None:"""The checked-out branch, or ``None`` when detached or unreadable."""name = _read(repo_path, ["rev-parse", "--abbrev-ref", "HEAD"])diff --git a/packages/server/src/repowise/server/mcp_server/tool_change_risk.py b/packages/server/src/repowise/server/mcp_server/tool_change_risk.pyindex 1db4d6cda..d41acb9ba 100644--- a/packages/server/src/repowise/server/mcp_server/tool_change_risk.py+++ b/packages/server/src/repowise/server/mcp_server/tool_change_risk.py@@ -3,6 +3,7 @@from __future__ import annotationsimport asyncio+import contextlibimport subprocessimport threadingimport time@@ -1113,16 +1114,24 @@ async def _patch_coverage_block(The same computation ``repowise coverage check`` gates on, read from thecoverage the index stores. ``None`` when there is no index, no storedcoverage, or no readable change: ``impacted_tests`` already says why.- ``files`` lists only the files that need attention, capped; the totals and- ``file_counts`` still count every file. ``coverage.ignore``,+ ``files`` lists only the files that need attention, riskiest first, capped;+ the totals and ``file_counts`` still count every file. ``coverage.ignore``,``coverage.min_coverable_lines`` and the path-scoped ``coverage.gates``apply as they do in the CLI gate; path gates read ``no_data`` on stale- coverage or invalid config, which ``scope.config_errors`` names.+ coverage or invalid config, which ``scope.config_errors`` names. Each row+ carries its file's risk, from the index and the checkout's git history."""from repowise.core import git_refsfrom repowise.core.analysis.change_risk.features import revspec_headfrom repowise.core.analysis.health.coverage import configured_coverage- from repowise.core.analysis.patch_coverage import attention_rows, stored_patch_coverage+ from repowise.core.analysis.patch_coverage import (+ assess_risks,+ attach_risk,+ attention_rows,+ read_git_fix_history,+ read_index_facts,+ stored_patch_coverage,+ )from repowise.core.persistence.database import get_sessionsession_factory = getattr(ctx, "session_factory", None)@@ -1132,20 +1141,33 @@ async def _patch_coverage_block(head_commit = Noneif not result.working_tree:head_commit = git_refs.resolve(str(ctx.path), revspec_head(revspec)) or None+ index: dict = {}try:async with get_session(session_factory) as session:+ repo_id = (await _get_repo(session)).idpatch = await stored_patch_coverage(session,- (await _get_repo(session)).id,+ repo_id,changed,label=result.features.ref,head_commit=head_commit,config=configured_coverage(ctx.path),)+ # An unreadable index row leaves risk to git alone.+ if patch is not None:+ with contextlib.suppress(SQLAlchemyError):+ index = await read_index_facts(+ session, repo_id, [f.file_path for f in patch.files]+ )except (LookupError, SQLAlchemyError):return Noneif patch is None:return None+ # Git after the session closes: no subprocess while a connection is held.+ git = await asyncio.to_thread(+ read_git_fix_history, str(ctx.path), revspec, working_tree=result.working_tree+ )+ patch = attach_risk(patch, assess_risks([f.file_path for f in patch.files], git, index))block = patch.to_dict()rows = [f.to_dict() for f in attention_rows(patch)]if len(rows) > _IMPACTED_TESTS_LIMIT:diff --git a/packages/server/src/repowise/server/routers/code_health/coverage_routes.py b/packages/server/src/repowise/server/routers/code_health/coverage_routes.pyindex 87371b5d9..23dceb6b4 100644--- a/packages/server/src/repowise/server/routers/code_health/coverage_routes.py+++ b/packages/server/src/repowise/server/routers/code_health/coverage_routes.py@@ -380,10 +380,19 @@ async def health_coverage_patch(coverage has been ingested, which is not the same as 0%. Honours``coverage.ignore`` and judges the path-scoped gates in ``coverage.gates``(with ``coverage.min_coverable_lines``) on current coverage and valid- config; the whole-change threshold stays with the CLI gate.+ config; the whole-change threshold stays with the CLI gate. Each file row+ carries its risk, from the index and the checkout's git history."""+ from sqlalchemy.exc import SQLAlchemyError+from repowise.core.analysis.health.coverage import configured_coverage- from repowise.core.analysis.patch_coverage import stored_patch_coverage+ from repowise.core.analysis.patch_coverage import (+ assess_risks,+ attach_risk,+ read_git_fix_history,+ read_index_facts,+ stored_patch_coverage,+ )changed, label, head_sha = await asyncio.to_thread(_read_change, repo.local_path, base, head)patch = await stored_patch_coverage(@@ -394,4 +403,13 @@ async def health_coverage_patch(head_commit=head_sha or None,config=configured_coverage(repo.local_path),)- return PatchCoverageResponse.model_validate(patch.to_dict()) if patch is not None else None+ if patch is None:+ return None+ paths = [f.file_path for f in patch.files]+ try:+ index = await read_index_facts(session, repo.id, paths)+ except SQLAlchemyError:+ index = {}+ git = await asyncio.to_thread(read_git_fix_history, repo.local_path, f"{base}...{head}")+ patch = attach_risk(patch, assess_risks(paths, git, index))+ return PatchCoverageResponse.model_validate(patch.to_dict())diff --git a/packages/server/src/repowise/server/schemas/__init__.py b/packages/server/src/repowise/server/schemas/__init__.pyindex db7a4f3ab..a5b97b683 100644--- a/packages/server/src/repowise/server/schemas/__init__.py+++ b/packages/server/src/repowise/server/schemas/__init__.py@@ -220,7 +220,9 @@ if TYPE_CHECKING:from .patch_coverage import (PatchCoverageFile,PatchCoverageFileCounts,+ PatchCoverageFileRisk,PatchCoverageResponse,+ PatchCoverageRisky,PatchCoverageScope,)from .refactoring import (@@ -497,7 +499,9 @@ _EXPORTS: dict[str, tuple[str, ...]] = {"patch_coverage": ("PatchCoverageFile","PatchCoverageFileCounts",+ "PatchCoverageFileRisk","PatchCoverageResponse",+ "PatchCoverageRisky","PatchCoverageScope",),"refactoring": (@@ -757,7 +761,9 @@ __all__ = ["Paginated","PatchCoverageFile","PatchCoverageFileCounts",+ "PatchCoverageFileRisk","PatchCoverageResponse",+ "PatchCoverageRisky","PatchCoverageScope","PopulationBreakdown","ProviderEntry",diff --git a/packages/server/src/repowise/server/schemas/patch_coverage.py b/packages/server/src/repowise/server/schemas/patch_coverage.pyindex 4491c23fe..270e22193 100644--- a/packages/server/src/repowise/server/schemas/patch_coverage.py+++ b/packages/server/src/repowise/server/schemas/patch_coverage.py@@ -10,6 +10,7 @@ from pydantic import BaseModel, ConfigDictfrom repowise.core.analysis.health.coverage.freshness import FreshnessStatusfrom repowise.core.analysis.patch_coverage.compute import FileStatus, GateStatus+from repowise.core.analysis.patch_coverage.risk import RiskBasisclass _Strict(BaseModel):@@ -17,6 +18,21 @@ class _Strict(BaseModel):model_config = ConfigDict(extra="forbid")+class PatchCoverageFileRisk(_Strict):+ #: Recency-decayed bug-fix count from git; null when git history was not read.+ fix_pressure: float | None+ #: Files importing this one, from the index graph; null without index data.+ dependents: int | None+ hotspot: bool | None+ bug_magnet: bool | None+ #: ``git`` for a file the index has no row for (one the change adds).+ basis: RiskBasis+ #: Index rows: hotspot or bug magnet. Git only: top quartile of files with+ #: bug-fix history, by fix pressure.+ risky: bool+ reasons: list[str]++class PatchCoverageFile(_Strict):file_path: strstatus: FileStatus@@ -27,6 +43,8 @@ class PatchCoverageFile(_Strict):patch_coverage_pct: float | None#: Inclusive ``[start, end]`` runs of changed, executable, unexecuted lines.uncovered_ranges: list[list[int]]+ #: Null when the file's risk was not assessed.+ risk: PatchCoverageFileRisk | Noneclass PatchCoverageFileCounts(_Strict):@@ -77,6 +95,19 @@ class PatchCoveragePathGate(_Strict):gate: GateStatus+class PatchCoverageRisky(_Strict):+ """Patch coverage over the measured files history marks as risky."""++ file_count: int+ covered_line_count: int+ coverable_line_count: int+ patch_coverage_pct: float | None+ threshold: float | None+ #: ``no_data`` when no risky file has a changed executable line;+ #: ``too_small`` under the whole change's small-change tolerance.+ gate: GateStatus++class PatchCoverageResponse(_Strict):patch_coverage_pct: float | Nonecovered_line_count: int@@ -85,9 +116,13 @@ class PatchCoverageResponse(_Strict):#: Small-change tolerance: below this many changed executable lines a#: missed threshold reads ``too_small`` and does not fail.min_coverable_lines: int | None+ #: Fails when the flat gate, a path-scoped gate that is not informational,+ #: or the risky-file gate fails.gate: GateStatusfile_counts: PatchCoverageFileCountsfiles: list[PatchCoverageFile]scope: PatchCoverageScope#: ``gate`` reads ``fail`` when any one here fails and is not informational.path_gates: list[PatchCoveragePathGate]+ #: Null when no file's risk was assessed.+ risky: PatchCoverageRisky | Nonediff --git a/packages/types/src/generated/http.ts b/packages/types/src/generated/http.tsindex fa0f98538..f4d9e3cf2 100644--- a/packages/types/src/generated/http.ts+++ b/packages/types/src/generated/http.ts@@ -2200,6 +2200,7 @@ export interface PatchCoverageFile {covered_line_count: number;patch_coverage_pct: number | null;uncovered_ranges: number[][];+ risk: PatchCoverageFileRisk | null;}export interface PatchCoverageFileCounts {@@ -2210,6 +2211,16 @@ export interface PatchCoverageFileCounts {out_of_scope: number;}+export interface PatchCoverageFileRisk {+ fix_pressure: number | null;+ dependents: number | null;+ hotspot: boolean | null;+ bug_magnet: boolean | null;+ basis: "git" | "index" | "git_and_index" | "unavailable";+ risky: boolean;+ reasons: string[];+}+/** One path-scoped gate from ``coverage.gates``, judged on the change. */export interface PatchCoveragePathGate {name: string;@@ -2235,6 +2246,17 @@ export interface PatchCoverageResponse {files: PatchCoverageFile[];scope: PatchCoverageScope;path_gates: PatchCoveragePathGate[];+ risky: PatchCoverageRisky | null;+}++/** Patch coverage over the measured files history marks as risky. */+export interface PatchCoverageRisky {+ file_count: number;+ covered_line_count: number;+ coverable_line_count: number;+ patch_coverage_pct: number | null;+ threshold: number | null;+ gate: "pass" | "fail" | "no_data" | "not_set" | "too_small";}export interface PatchCoverageScope {diff --git a/packages/ui/__tests__/health/patch-coverage-summary.test.tsx b/packages/ui/__tests__/health/patch-coverage-summary.test.tsxindex c0a48e64d..e723f0d72 100644--- a/packages/ui/__tests__/health/patch-coverage-summary.test.tsx+++ b/packages/ui/__tests__/health/patch-coverage-summary.test.tsx@@ -8,6 +8,7 @@ import {PatchCoverageSummary,floorPct,formatLineRanges,+ riskWords,} from "../../src/health/patch-coverage-summary.js";function coverage(fileCount: number): PatchCoverageResponse {@@ -33,6 +34,7 @@ function coverage(fileCount: number): PatchCoverageResponse {covered_line_count: 1,patch_coverage_pct: 50,uncovered_ranges: [[i + 1, i + 1]],+ risk: null,})),scope: {label: "coverage.xml",@@ -47,6 +49,7 @@ function coverage(fileCount: number): PatchCoverageResponse {config_errors: [],},path_gates: [],+ risky: null,};}@@ -69,6 +72,48 @@ function pathGate(};}+function withRisk(): PatchCoverageResponse {+ const base = coverage(2);+ const [plain, hot] = base.files;+ return {+ ...base,+ files: [+ {+ ...plain!,+ risk: {+ fix_pressure: 0,+ dependents: null,+ hotspot: null,+ bug_magnet: null,+ basis: "git",+ risky: false,+ reasons: [],+ },+ },+ {+ ...hot!,+ risk: {+ fix_pressure: 3.24,+ dependents: 14,+ hotspot: true,+ bug_magnet: false,+ basis: "git_and_index",+ risky: true,+ reasons: ["hotspot"],+ },+ },+ ],+ risky: {+ file_count: 1,+ covered_line_count: 1,+ coverable_line_count: 2,+ patch_coverage_pct: 50,+ threshold: null,+ gate: "not_set",+ },+ };+}+describe("patch coverage formatting", () => {it("floors to one decimal so a near miss never reads as full", () => {expect(floorPct(99.99)).toBe("99.9");@@ -87,15 +132,24 @@ describe("patch coverage formatting", () => {});});+/** The file paths a rendered list shows, in order. */+function listedPaths(container: HTMLElement, list = "li"): (string | null)[] {+ return [...container.querySelectorAll(`${list} .font-mono.flex-1`)].map(+ (el) => el.textContent,+ );+}+describe("PatchCoverageSummary", () => {it("lists ten files with gaps and counts the rest", () => {- render(<PatchCoverageSummary coverage={coverage(12)} />);- expect(screen.getByText("src/f9.ts")).toBeTruthy();- expect(screen.queryByText("src/f10.ts")).toBeNull();+ const { container } = render(<PatchCoverageSummary coverage={coverage(12)} />);+ // Equal risk and gap size fall back to path order, as the CLI's table does.+ const paths = listedPaths(container);+ expect(paths).toContain("src/f10.ts");+ expect(paths).not.toContain("src/f9.ts");expect(screen.getByText("and 2 more")).toBeTruthy();});- it("stays quiet about freshness when coverage is current", () => {+ it("states the denominator and stays quiet when coverage is current", () => {render(<PatchCoverageSummary coverage={coverage(1)} />);expect(screen.getByText("1 of 2 changed executable lines covered")).toBeTruthy();expect(screen.queryByText(/measured at/)).toBeNull();@@ -120,6 +174,7 @@ describe("PatchCoverageSummary", () => {[3, 3],[7, 9],],+ risk: null,},{file_path: "src/new.ts",@@ -129,6 +184,7 @@ describe("PatchCoverageSummary", () => {covered_line_count: 0,patch_coverage_pct: null,uncovered_ranges: [],+ risk: null,},],scope: { ...base.scope, freshness: "stale" },@@ -203,9 +259,9 @@ describe("PatchCoverageSummary", () => {expect(screen.queryByText(/invalid entr/)).toBeNull();});- it("renders a response from a server older than path-scoped gates", () => {+ it("renders a response from a server older than path-scoped gates and risk", () => {const base = coverage(1);- const { path_gates: _gates, ...older } = base;+ const { path_gates: _gates, risky: _risky, ...older } = base;const { config_errors: _errors, ...olderScope } = base.scope;render(<PatchCoverageSummary@@ -215,4 +271,73 @@ describe("PatchCoverageSummary", () => {expect(screen.getByText("1 of 2 changed executable lines covered")).toBeTruthy();expect(screen.queryByRole("list", { name: "Path-scoped gates" })).toBeNull();});++ it("lists the risky file first and names its risk in words", () => {+ const { container } = render(<PatchCoverageSummary coverage={withRisk()} />);+ expect(listedPaths(container)).toEqual(["src/f1.ts", "src/f0.ts"]);+ expect(screen.getByText("hotspot, bug-fix weight 3.2, 14 dependents")).toBeTruthy();+ // "none known" is not worth a label on a row.+ expect(screen.queryByText("none known")).toBeNull();+ expect(+ screen.getByText(/Risky files 50\.0% · 1 of 2 changed executable lines covered/),+ ).toBeTruthy();+ // One row had no index data, so the basis is disclosed.+ expect(+ screen.getByText(/Risk for 1 of 2 files is from git bug-fix history alone/),+ ).toBeTruthy();+ });++ it("orders unmeasured files as the core does: not in report first, then by risk", () => {+ const base = withRisk();+ const [plain, hot] = base.files;+ const unmeasured = (+ path: string,+ status: "not_in_report" | "no_line_data",+ file: typeof plain,+ ) => ({+ ...file!,+ file_path: path,+ status,+ coverable_line_count: 0,+ covered_line_count: 0,+ patch_coverage_pct: null,+ uncovered_ranges: [],+ });+ const { container } = render(+ <PatchCoverageSummary+ coverage={{+ ...base,+ files: [+ unmeasured("a_nodata_hot.ts", "no_line_data", hot),+ unmeasured("b_new_plain.ts", "not_in_report", plain),+ unmeasured("c_new_hot.ts", "not_in_report", hot),+ ],+ }}+ />,+ );+ expect(listedPaths(container, "details li")).toEqual([+ "c_new_hot.ts",+ "b_new_plain.ts",+ "a_nodata_hot.ts",+ ]);+ expect(screen.getAllByText("not in report")).toHaveLength(2);+ });+});++describe("riskWords", () => {+ it("reads unassessed risk as nothing, unreadable as unknown, and names the weight", () => {+ const risk = {+ fix_pressure: 1,+ dependents: 1,+ hotspot: null,+ bug_magnet: null,+ basis: "git" as const,+ risky: false,+ reasons: [],+ };+ expect(riskWords(null)).toBeNull();+ expect(riskWords({ ...risk, basis: "unavailable" })).toBe("unknown");+ expect(riskWords(risk)).toBe("bug-fix weight 1.0, 1 dependent");+ expect(riskWords({ ...risk, fix_pressure: 0, dependents: null })).toBe("none known");+ });});diff --git a/packages/ui/src/health/patch-coverage-summary.tsx b/packages/ui/src/health/patch-coverage-summary.tsxindex 640e42b85..270705d65 100644--- a/packages/ui/src/health/patch-coverage-summary.tsx+++ b/packages/ui/src/health/patch-coverage-summary.tsx@@ -4,15 +4,21 @@** A verdict sentence, not a figure with a band. There is no threshold here to* colour against, and a gate belongs to CI (`repowise coverage check`), so the- * only marked state is stale evidence: coverage measured at another commit- * describes other code, and the line numbers below it may not line up. The- * exception is path-scoped gates (`coverage.gates`), which carry their own- * thresholds: each is a dot plus a word.+ * marked states are few: stale or fragmentary evidence (coverage measured at+ * another commit describes other code, and the line numbers below it may not+ * line up), a risky file, whose risk words carry the warning colour, and+ * path-scoped gates (`coverage.gates`), which carry their own thresholds:+ * each is a dot plus a word.+ *+ * Each file row names its file's risk in words, and the rows read riskiest+ * first, in the order `repowise coverage check` uses. A line says where the+ * risk came from when not every file had index data.*/import type { ReactNode } from "react";import type {PatchCoverageFile,+ PatchCoverageFileRisk,PatchCoveragePathGate,PatchCoverageResponse,} from "@repowise-dev/types/generated/http";@@ -45,6 +51,56 @@ function plural(n: number, word: string, many?: string): string {return `${n} ${n === 1 ? word : (many ?? `${word}s`)}`;}+/** A file's risk in words, as the CLI's Risk column reads; `null` when not assessed. */+export function riskWords(+ risk: PatchCoverageFileRisk | null | undefined,+): string | null {+ if (risk == null) return null;+ if (risk.basis === "unavailable") return "unknown";+ const parts = [...risk.reasons];+ if (risk.fix_pressure) parts.push(`bug-fix weight ${risk.fix_pressure.toFixed(1)}`);+ if (risk.dependents) parts.push(plural(risk.dependents, "dependent"));+ return parts.length ? parts.join(", ") : "none known";+}++/** Where the risk came from, when not every row had index data; the CLI's basis line. */+export function riskBasisLine(files: PatchCoverageFile[]): string | null {+ const bases = files.flatMap((f) => (f.risk ? [f.risk.basis] : []));+ const count = (basis: string) => bases.filter((b) => b === basis).length;+ const unknown = count("unavailable");+ const parts = [+ gitOnlyText(count("git"), bases.length),+ bases.includes("index") ? "git fix history could not be read" : "",+ unknown ? `risk could not be read for ${plural(unknown, "file")}` : "",+ ].filter(Boolean);+ return parts.length ? parts.join(" · ") : null;+}++function gitOnlyText(gitOnly: number, total: number): string {+ if (!gitOnly) return "";+ if (gitOnly === total) {+ return "Risk is from git bug-fix history alone; an index adds hotspot, bug-magnet and dependent counts";+ }+ return `Risk for ${gitOnly} of ${plural(total, "file")} is from git bug-fix history alone: the index has no row for them yet`;+}++/** Risky first, then fix pressure, dependents, uncovered lines: the core's order. */+export function byRisk(a: PatchCoverageFile, b: PatchCoverageFile): number {+ const key = (f: PatchCoverageFile): number[] => [+ f.risk?.risky ? 0 : 1,+ -(f.risk?.fix_pressure ?? 0),+ -(f.risk?.dependents ?? 0),+ -(f.coverable_line_count - f.covered_line_count),+ ];+ const ka = key(a);+ const kb = key(b);+ for (let i = 0; i < ka.length; i++) {+ const diff = (ka[i] ?? 0) - (kb[i] ?? 0);+ if (diff !== 0) return diff;+ }+ return a.file_path < b.file_path ? -1 : a.file_path > b.file_path ? 1 : 0;+}+type Tone = "error" | "warning" | "success" | "neutral";const DOT: Record<Tone, string> = {@@ -64,92 +120,43 @@ const GATE_STATUS: Record<PatchCoveragePathGate["gate"], { text: string; tone: Ttoo_small: { text: "too few changed lines to judge", tone: "neutral" },};+type OpenFile = ((path: string) => void) | undefined;+export function PatchCoverageSummary({coverage,onOpenFile,}: PatchCoverageSummaryProps) {- const uncovered = coverage.files.filter(- (f) => f.status === "measured" && f.uncovered_ranges.length > 0,- );- const unmeasured = coverage.files.filter(- (f) => f.status === "not_in_report" || f.status === "no_line_data",- );- const measuredAt = coverage.scope.measured_commit?.slice(0, 7) ?? null;-return (<div className="flex flex-col gap-1.5"><Headline coverage={coverage} />-- {coverage.scope.freshness === "stale" && (- <WarningNote>- <span className="sr-only">Stale: </span>- Coverage was measured at{" "}- {measuredAt ? <code className="font-mono">{measuredAt}</code> : "another commit"}- , not at this change's head- </WarningNote>- )}-- {coverage.scope.mapping_partial && (- <WarningNote>- Most report paths did not match this repository, so this covers a fragment- </WarningNote>- )}-- {coverage.scope.ignored_file_count > 0 && (- <p className="text-xs text-[var(--color-text-tertiary)]">- {plural(coverage.scope.ignored_file_count, "changed file")} ignored by{" "}- <code className="font-mono">coverage.ignore</code>- </p>- )}-+ <RiskyLine risky={coverage.risky} />+ <ScopeNotes coverage={coverage} />{/* Guarded: a server older than path-scoped gates sends neither field. */}<PathGatesgates={coverage.path_gates ?? []}configErrors={coverage.scope.config_errors ?? []}/>-- {uncovered.length > 0 && (- <ul className="mt-1 flex flex-col gap-0.5">- {uncovered.slice(0, MAX_FILES).map((f) => (- <FileRow- key={f.file_path}- file={f}- trailing={`lines ${formatLineRanges(f.uncovered_ranges)}`}- onOpenFile={onOpenFile}- />- ))}- {uncovered.length > MAX_FILES && (- <li className="px-1.5 pt-1 text-xs text-[var(--color-text-tertiary)]">- and {uncovered.length - MAX_FILES} more- </li>- )}- </ul>- )}-- {unmeasured.length > 0 && (- <details className="group mt-1">- <summary className="cursor-pointer text-xs text-[var(--color-text-tertiary)]">- {plural(unmeasured.length, "changed file")} the coverage report does- not measure- </summary>- <ul className="mt-1 flex flex-col gap-0.5">- {unmeasured.map((f) => (- <FileRow- key={f.file_path}- file={f}- trailing={- f.status === "not_in_report" ? "not in report" : "no line data"- }- onOpenFile={onOpenFile}- />- ))}- </ul>- </details>- )}+ <UncoveredList files={uncoveredFiles(coverage.files)} onOpenFile={onOpenFile} />+ <UnmeasuredList files={unmeasuredFiles(coverage.files)} onOpenFile={onOpenFile} /></div>);}+/** Measured files with uncovered changed lines, riskiest first. */+function uncoveredFiles(files: PatchCoverageFile[]): PatchCoverageFile[] {+ return files+ .filter((f) => f.status === "measured" && f.uncovered_ranges.length > 0)+ .sort(byRisk);+}++/** Not-in-report files before those without line data, then by risk: the core's order. */+function unmeasuredFiles(files: PatchCoverageFile[]): PatchCoverageFile[] {+ const rank = (f: PatchCoverageFile) => Number(f.status !== "not_in_report");+ return files+ .filter((f) => f.status === "not_in_report" || f.status === "no_line_data")+ .sort((a, b) => rank(a) - rank(b) || byRisk(a, b));+}+function Headline({ coverage }: { coverage: PatchCoverageResponse }) {const pct = coverage.patch_coverage_pct;return (@@ -178,6 +185,45 @@ function Headline({ coverage }: { coverage: PatchCoverageResponse }) {);}+/** Only when a risky file has changed executable lines: nothing else to report. */+function RiskyLine({ risky }: { risky: PatchCoverageResponse["risky"] | undefined }) {+ if (risky == null || risky.patch_coverage_pct == null) return null;+ return (+ <p className="text-xs tabular-nums text-[var(--color-text-secondary)]">+ Risky files {floorPct(risky.patch_coverage_pct)}% · {risky.covered_line_count}{" "}+ of {plural(risky.coverable_line_count, "changed executable line")} covered+ in {plural(risky.file_count, "risky file")}+ </p>+ );+}++/** What to read the figures with: stale or partial evidence, ignored files, risk basis. */+function ScopeNotes({ coverage }: { coverage: PatchCoverageResponse }) {+ const { scope } = coverage;+ const basis = riskBasisLine(coverage.files);+ return (+ <>+ {scope.freshness === "stale" && (+ <StaleNote measuredAt={scope.measured_commit?.slice(0, 7) ?? null} />+ )}+ {scope.mapping_partial && (+ <WarningNote>+ Most report paths did not match this repository, so this covers a fragment+ </WarningNote>+ )}+ {scope.ignored_file_count > 0 && (+ <p className="text-xs text-[var(--color-text-tertiary)]">+ {plural(scope.ignored_file_count, "changed file")} ignored by{" "}+ <code className="font-mono">coverage.ignore</code>+ </p>+ )}+ {basis != null && (+ <p className="text-xs text-[var(--color-text-tertiary)]">{basis}.</p>+ )}+ </>+ );+}+/** An amber dot plus a sentence: evidence to read with care. */function WarningNote({ children }: { children: ReactNode }) {return (@@ -188,6 +234,17 @@ function WarningNote({ children }: { children: ReactNode }) {);}+function StaleNote({ measuredAt }: { measuredAt: string | null }) {+ return (+ <WarningNote>+ <span className="sr-only">Stale: </span>+ Coverage was measured at{" "}+ {measuredAt ? <code className="font-mono">{measuredAt}</code> : "another commit"}+ , not at this change's head+ </WarningNote>+ );+}+/** Path-scoped gates (`coverage.gates`): why none is judged, then each one. */function PathGates({gates,@@ -264,6 +321,60 @@ function PathGateRow({ gate }: { gate: PatchCoveragePathGate }) {);}+function UncoveredList({+ files,+ onOpenFile,+}: {+ files: PatchCoverageFile[];+ onOpenFile: OpenFile;+}) {+ if (!files.length) return null;+ return (+ <ul className="mt-1 flex flex-col gap-0.5">+ {files.slice(0, MAX_FILES).map((f) => (+ <FileRow+ key={f.file_path}+ file={f}+ trailing={`lines ${formatLineRanges(f.uncovered_ranges)}`}+ onOpenFile={onOpenFile}+ />+ ))}+ {files.length > MAX_FILES && (+ <li className="px-1.5 pt-1 text-xs text-[var(--color-text-tertiary)]">+ and {files.length - MAX_FILES} more+ </li>+ )}+ </ul>+ );+}++function UnmeasuredList({+ files,+ onOpenFile,+}: {+ files: PatchCoverageFile[];+ onOpenFile: OpenFile;+}) {+ if (!files.length) return null;+ return (+ <details className="group mt-1">+ <summary className="cursor-pointer text-xs text-[var(--color-text-tertiary)]">+ {plural(files.length, "changed file")} the coverage report does not measure+ </summary>+ <ul className="mt-1 flex flex-col gap-0.5">+ {files.map((f) => (+ <FileRow+ key={f.file_path}+ file={f}+ trailing={f.status === "not_in_report" ? "not in report" : "no line data"}+ onOpenFile={onOpenFile}+ />+ ))}+ </ul>+ </details>+ );+}+function FileRow({file,trailing,@@ -271,13 +382,14 @@ function FileRow({}: {file: PatchCoverageFile;trailing: string;- onOpenFile: ((path: string) => void) | undefined;+ onOpenFile: OpenFile;}) {const body = (<><span className="min-w-0 flex-1 truncate font-mono text-xs text-[var(--color-text-primary)]">{file.file_path}</span>+ <RiskLabel risk={file.risk} /><span className="shrink-0 font-mono text-xs tabular-nums text-[var(--color-text-tertiary)]">{trailing}</span>@@ -303,3 +415,17 @@ function FileRow({</li>);}++/** A row's risk in words; nothing for "none known", which is not worth a label. */+function RiskLabel({ risk }: { risk: PatchCoverageFile["risk"] | undefined }) {+ const words = riskWords(risk);+ if (words == null || words === "none known") return null;+ const risky = risk?.risky === true;+ const tone = risky ? "text-[var(--color-warning)]" : "text-[var(--color-text-tertiary)]";+ return (+ <span className={`shrink-0 text-[10px] ${tone}`}>+ {risky && <span className="sr-only">Risky: </span>}+ {words}+ </span>+ );+}diff --git a/packages/vscode/webview/src/views/risk/App.test.tsx b/packages/vscode/webview/src/views/risk/App.test.tsxindex a1492496d..fd4353e7c 100644--- a/packages/vscode/webview/src/views/risk/App.test.tsx+++ b/packages/vscode/webview/src/views/risk/App.test.tsx@@ -90,6 +90,15 @@ const PATCH_COVERAGE: NonNullable<RiskRangeReport["patchCoverage"]> = {[3, 3],[7, 9],],+ risk: {+ fix_pressure: 2,+ dependents: 5,+ hotspot: true,+ bug_magnet: false,+ basis: "git_and_index",+ risky: true,+ reasons: ["hotspot"],+ },},{file_path: "src/new.ts",@@ -99,8 +108,17 @@ const PATCH_COVERAGE: NonNullable<RiskRangeReport["patchCoverage"]> = {covered_line_count: 0,patch_coverage_pct: null,uncovered_ranges: [],+ risk: null,},],+ risky: {+ file_count: 1,+ covered_line_count: 2,+ coverable_line_count: 3,+ patch_coverage_pct: 66.66,+ threshold: null,+ gate: "not_set",+ },scope: {label: "coverage.xml",source_formats: ["cobertura"],@@ -469,7 +487,9 @@ describe("risk App", () => {expect(screen.queryByText(/test recommendation/)).toBeNull();});- it("reports patch coverage of the range with a denominator and its gaps", async () => {+ // The summary's own wording, ordering and risk labels are pinned by its+ // component test in packages/ui; this only checks the view wires it up.+ it("shows the range's patch coverage and opens a listed file", async () => {const riskRange = vi.fn().mockResolvedValue({ ...REPORT, patchCoverage: PATCH_COVERAGE });@@ -483,7 +503,6 @@ describe("risk App", () => {/>,);- // The panel renders the shared summary, whose own tests own the wording.expect(await screen.findByText("66.6%")).toBeTruthy();expect(screen.getByRole("list", { name: "Path-scoped gates" })).toBeTruthy();diff --git a/tests/unit/analysis/test_patch_coverage.py b/tests/unit/analysis/test_patch_coverage.pyindex 96f8e866f..b74515af1 100644--- a/tests/unit/analysis/test_patch_coverage.py+++ b/tests/unit/analysis/test_patch_coverage.py@@ -2,15 +2,24 @@from __future__ import annotations+from dataclasses import replace+from repowise.core.analysis.changed_lines import line_rangesfrom repowise.core.analysis.health.coverage import PathGate, file_coveragefrom repowise.core.analysis.patch_coverage import (+ FileRisk,+ GitFixHistory,+ IndexFacts,PatchScope,+ assess_risks,+ attach_risk,+ attention_rows,compute_patch_coverage,fmt_pct,github_annotations,headline,render_markdown,+ risky_line,)@@ -250,6 +259,194 @@ def test_exact_threshold_passes_and_reads_exactly() -> None:assert pc.to_dict()["patch_coverage_pct"] == 57.0+# ---------------------------------------------------------------------------+# Risk: which files are risky, the order rows read in, the risky-file gate+# ---------------------------------------------------------------------------+++def _git_history(pressure: dict[str, float]) -> GitFixHistory:+ # As read_git_fix_history builds it: the files with any fix history.+ return GitFixHistory(pressure, tuple(sorted(v for v in pressure.values() if v > 0)))+++def test_git_only_risk_is_the_top_quartile_of_files_with_fix_history() -> None:+ # Many files fixed once or twice, one fixed a lot: only that one is risky,+ # however many never-fixed files the repository also has.+ pressure = {f"f{i}.py": 1.0 + i / 10 for i in range(8)} | {"hot.py": 6.0}+ risks = assess_risks(["hot.py", "f0.py", "f7.py", "cold.py"], _git_history(pressure), {})++ assert risks["hot.py"].risky+ assert risks["hot.py"].reasons == ("top quartile of files with bug-fix history",)+ # f7 ranks 7 of 8 others (0.875): top quartile. f0 has a fix but ranks last.+ assert risks["f7.py"].risky+ assert not risks["f0.py"].risky+ # Zero pressure is never risky.+ assert not risks["cold.py"].risky+ assert {r.basis for r in risks.values()} == {"git"}+ assert risks["cold.py"].dependents is None and risks["cold.py"].hotspot is None+++def test_one_file_with_fix_history_ranks_nothing() -> None:+ risks = assess_risks(["a.py"], _git_history({"a.py": 3.0}), {})++ assert not risks["a.py"].risky+++def test_history_ref_reads_before_the_change() -> None:+ from repowise.core.analysis.change_risk.service import history_ref++ assert history_ref(".", "abc123") == "abc123^"+ assert history_ref(".", None) == "HEAD^"+ assert history_ref(".", None, working_tree=True) == "HEAD"+ assert history_ref(".", "main...HEAD", working_tree=True) == "HEAD"+++def test_index_rows_use_the_index_flags_and_a_new_file_falls_back_to_git() -> None:+ git = _git_history({"hot.py": 9.0, "a.py": 1.0, "new.py": 0.0})+ index = {+ "hot.py": IndexFacts(hotspot=False, bug_magnet=False, dependents=3),+ "flagged.py": IndexFacts(hotspot=True, bug_magnet=True, dependents=14),+ }+ risks = assess_risks(["hot.py", "flagged.py", "new.py"], git, index)++ # With index data the index decides, even against a high git pressure.+ assert not risks["hot.py"].risky and risks["hot.py"].basis == "git_and_index"+ assert risks["flagged.py"].reasons == ("hotspot", "bug magnet")+ assert risks["new.py"].basis == "git"+ # Git unreadable: index rows keep their flags, the rest are unavailable.+ no_git = assess_risks(["flagged.py", "new.py"], None, index)+ assert no_git["flagged.py"].basis == "index" and no_git["flagged.py"].risky+ assert no_git["new.py"] == FileRisk()+++def _risk(risky: bool, pressure: float = 0.0, dependents: int | None = None) -> FileRisk:+ return FileRisk(+ fix_pressure=pressure,+ dependents=dependents,+ basis="git",+ risky=risky,+ reasons=("hotspot",) if risky else (),+ )+++def _three_files():+ coverage = {+ **_cov("big.py", covered=[], coverable=[1, 2, 3, 4, 5]),+ **_cov("fixed.py", covered=[], coverable=[1, 2]),+ **_cov("risky.py", covered=[1], coverable=[1, 2]),+ }+ changed = {"big.py": {1, 2, 3, 4, 5}, "fixed.py": {1, 2}, "risky.py": {1, 2}}+ risks = {+ "big.py": _risk(False),+ "fixed.py": _risk(False, pressure=2.0),+ "risky.py": _risk(True, pressure=1.0, dependents=4),+ }+ return compute_patch_coverage(changed, coverage), risks+++def test_rows_and_annotations_read_riskiest_first() -> None:+ pc, risks = _three_files()+ pc = attach_risk(pc, risks)++ assert [f.file_path for f in attention_rows(pc)] == ["risky.py", "fixed.py", "big.py"]+ warnings = [line for line in github_annotations(pc) if line.startswith("::warning")]+ assert "file=risky.py" in warnings[0]+ assert "title=Uncovered change in a risky file" in warnings[0]+ assert "(hotspot, bug-fix weight 1.0, 4 dependents)" in warnings[0]+ assert "title=Uncovered change::" in warnings[1]+++def test_markdown_names_each_files_risk_in_words() -> None:+ pc, risks = _three_files()+ md = render_markdown(attach_risk(pc, risks))++ assert "| File | Risk | Uncovered changed lines | Covered |" in md+ assert "| `risky.py` | hotspot, bug-fix weight 1.0, 4 dependents | 2 | 1 of 2 |" in md+ assert "| `big.py` | none known | 1-5 | 0 of 5 |" in md+ assert "Risk is from git bug-fix history alone" in md+++def test_unreadable_risk_reads_unknown_and_the_basis_says_so() -> None:+ pc, risks = _three_files()+ md = render_markdown(attach_risk(pc, {**risks, "big.py": FileRisk()}))++ assert "| `big.py` | unknown | 1-5 | 0 of 5 |" in md+ assert "risk could not be read for 1 file" in md+++def test_the_risky_gate_fails_on_risky_files_alone() -> None:+ pc, risks = _three_files()+ pc = replace(pc, threshold=10)++ strict = attach_risk(pc, risks, risky_threshold=80)+ assert strict.flat_gate == "pass" # 1 of 9 lines is 11.1%, above 10%+ assert strict.risky_gate == "fail" # 1 of 2 risky lines is 50%+ assert strict.gate == "fail"+ assert strict.to_dict()["risky"] == {+ "file_count": 1,+ "covered_line_count": 1,+ "coverable_line_count": 2,+ "patch_coverage_pct": 50.0,+ "threshold": 80,+ "gate": "fail",+ }+ assert "meets the 10.0% gate" in headline(strict)+ assert "below the 80.0% risky-file gate" in risky_line(strict)+ errors = [line for line in github_annotations(strict) if line.startswith("::error")]+ assert len(errors) == 1 and "risky-file gate" in errors[0]++ assert attach_risk(pc, risks, risky_threshold=50).gate == "pass"+ # Only the risky gate set: its verdict is the verdict.+ assert attach_risk(replace(pc, threshold=None), risks, risky_threshold=50).gate == "pass"+++def test_no_risky_file_is_no_data_not_a_failure() -> None:+ pc, risks = _three_files()+ calm = {path: _risk(False) for path in risks}++ gated = attach_risk(replace(pc, threshold=10), calm, risky_threshold=90)+ assert gated.risky_gate == "no_data"+ assert gated.gate == "pass"+ assert "risky-file gate was not applied" in risky_line(gated)+++def test_the_risky_gate_joins_path_gates_and_the_small_change_tolerance() -> None:+ pc, risks = _three_files() # 9 changed executable lines, 2 of them risky+ passing_gate = [PathGate("all", ("*.py",), 10)]+ gated = compute_patch_coverage(+ {f.file_path: set(range(1, f.coverable_line_count + 1)) for f in pc.files},+ {+ **_cov("big.py", covered=[], coverable=[1, 2, 3, 4, 5]),+ **_cov("fixed.py", covered=[], coverable=[1, 2]),+ **_cov("risky.py", covered=[1], coverable=[1, 2]),+ },+ gates=passing_gate,+ )++ risky_fail = attach_risk(gated, risks, risky_threshold=80)+ assert [g.gate for g in risky_fail.path_gates] == ["pass"]+ assert (risky_fail.flat_gate, risky_fail.risky_gate, risky_fail.gate) == (+ "not_set",+ "fail",+ "fail",+ )+ # Under the whole change's tolerance the risky gate is exempt, as path gates are.+ small = attach_risk(replace(gated, min_coverable_lines=20), risks, risky_threshold=80)+ assert (small.risky_gate, small.gate) == ("too_small", "too_small")+ assert "risky-file gate, not applied" in risky_line(small)+ notices = [line for line in github_annotations(small) if line.startswith("::notice")]+ assert any("Risky files" in line for line in notices)+++def test_without_risk_the_flat_gate_and_shape_are_unchanged() -> None:+ pc, _ = _three_files()+ d = pc.to_dict()++ assert d["risky"] is None+ assert all(f["risk"] is None for f in d["files"])+ assert "| File | Uncovered changed lines | Covered |" in render_markdown(pc)++def test_serialized_percentage_is_floored_not_rounded() -> None:coverable = list(range(1, 40001))pc = compute_patch_coverage(diff --git a/tests/unit/cli/test_coverage_check_cmd.py b/tests/unit/cli/test_coverage_check_cmd.pyindex b7b1eeacb..515ffb965 100644--- a/tests/unit/cli/test_coverage_check_cmd.py+++ b/tests/unit/cli/test_coverage_check_cmd.py@@ -323,6 +323,120 @@ def test_coverage_ignore_drops_changed_files_and_report_entries(repo) -> None:1,0,)+def fixed_repo(tmp_path):+ """``main`` fixed src/app.py twice and src/other.py once; ``feat`` edits app lines 2-3."""+ _git(tmp_path, "init", "-q", "-b", "main")+ _git(tmp_path, "config", "user.email", "[email protected]")+ _git(tmp_path, "config", "user.name", "t")+ (tmp_path / "src").mkdir()+ for name in ("app", "util", "other"):+ (tmp_path / "src" / f"{name}.py").write_text("a = 1\nb = 2\nc = 3\n", encoding="utf-8")+ _git(tmp_path, "add", "-A")+ _git(tmp_path, "commit", "-qm", "init")+ for value in ("0", "-1"):+ (tmp_path / "src" / "app.py").write_text(f"a = {value}\nb = 2\nc = 3\n", encoding="utf-8")+ _git(tmp_path, "commit", "-qam", "fix: crash on start")+ (tmp_path / "src" / "other.py").write_text("a = 0\nb = 2\nc = 3\n", encoding="utf-8")+ _git(tmp_path, "commit", "-qam", "fix: off by one")+ _git(tmp_path, "switch", "-qc", "feat")+ (tmp_path / "src" / "app.py").write_text("a = 0\nb = 22\nc = 33\n", encoding="utf-8")+ _git(tmp_path, "commit", "-qam", "feat: new values")+ return tmp_path+++def test_rows_carry_git_risk_without_an_index(fixed_repo) -> None:+ report = _lcov(fixed_repo, {2: 1, 3: 0})+ result = _run(fixed_repo, "main...feat", "--report", report, "--format", "json")++ assert result.exit_code == 0, result.output+ data = json.loads(result.stdout)+ (row,) = data["files"]+ assert row["risk"]["basis"] == "git"+ assert row["risk"]["risky"] is True+ assert row["risk"]["reasons"] == ["top quartile of files with bug-fix history"]+ assert data["risky"]["patch_coverage_pct"] == 50.0+ assert data["risky"]["gate"] == "not_set"+++def test_fail_under_risky_gates_the_risky_files(fixed_repo) -> None:+ report = _lcov(fixed_repo, {2: 1, 3: 0})++ passed = _run(fixed_repo, "main...feat", "--report", report, "--fail-under-risky", "40")+ assert passed.exit_code == 0, passed.output+ failed = _run(fixed_repo, "main...feat", "--report", report, "--fail-under-risky", "80")+ assert failed.exit_code == 1+ assert "below the 80.0% risky-file gate" in failed.output+ # From config too, validated like fail_under.+ (fixed_repo / ".repowise").mkdir()+ config = fixed_repo / ".repowise" / "config.yaml"+ config.write_text("coverage:\n fail_under_risky: 80\n", encoding="utf-8")+ assert _run(fixed_repo, "main...feat", "--report", report).exit_code == 1+ config.write_text("coverage:\n fail_under_risky: high\n", encoding="utf-8")+ assert _run(fixed_repo, "main...feat", "--report", report).exit_code == 2+++def test_fail_under_risky_without_readable_risk_exits_2(fixed_repo, monkeypatch) -> None:+ import repowise.core.analysis.patch_coverage as pc_module++ monkeypatch.setattr(pc_module, "read_git_fix_history", lambda *_a, **_k: None)+ report = _lcov(fixed_repo, {2: 1, 3: 0})++ # Without the risky gate an unreadable risk is only a gap in the table.+ assert _run(fixed_repo, "main...feat", "--report", report).exit_code == 0+ gated = _run(fixed_repo, "main...feat", "--report", report, "--fail-under-risky", "50")+ assert gated.exit_code == 2+ assert "risky-file gate cannot run" in gated.output+ # A failing flat gate is not hidden behind the risky gate's setup problem.+ both = _run(+ fixed_repo, "main...feat", "--report", report,+ "--fail-under", "80", "--fail-under-risky", "50", "--format", "json",+ )+ assert both.exit_code == 1, both.output+ assert json.loads(both.stdout)["gate"] == "fail"+++def test_fail_under_risky_on_a_shallow_clone_exits_2(fixed_repo, tmp_path_factory) -> None:+ shallow = tmp_path_factory.mktemp("shallow") / "repo"+ _git(+ fixed_repo.parent, "clone", "-q", "--depth", "2", "--no-single-branch",+ fixed_repo.as_uri(), str(shallow),+ )+ report = _lcov(shallow, {2: 1, 3: 0})++ result = _run(shallow, "origin/main...HEAD", "--report", report, "--fail-under-risky", "50")+ assert result.exit_code == 2, result.output+ assert "fetch full history" in result.output.lower()+++def test_an_unreadable_index_still_gates_on_git_risk(fixed_repo, monkeypatch) -> None:+ from contextlib import asynccontextmanager++ from sqlalchemy.exc import OperationalError++ import repowise.cli.commands.coverage_check_cmd as cmd+ import repowise.core.analysis.patch_coverage as pc_module++ @asynccontextmanager+ async def _opened(_root):+ yield object(), "r"++ async def _raise(*_a, **_k):+ raise OperationalError("select", {}, Exception("disk I/O error"))++ monkeypatch.setattr(cmd, "has_db_store", lambda _root: True)+ monkeypatch.setattr(cmd, "repo_index_session", _opened)+ monkeypatch.setattr(pc_module, "read_index_facts", _raise)+ report = _lcov(fixed_repo, {2: 1, 3: 0})++ result = _run(+ fixed_repo, "main...feat", "--report", report, "--fail-under-risky", "80",+ "--format", "json",+ )+ assert result.exit_code == 1, result.output+ data = json.loads(result.stdout)+ assert data["files"][0]["risk"]["basis"] == "git"+ assert data["risky"]["gate"] == "fail"def test_stored_coverage_is_not_gated_when_stale_or_without_line_data() -> None:diff --git a/tests/unit/cli/test_risk_cmd.py b/tests/unit/cli/test_risk_cmd.pyindex 3b0633627..401114660 100644--- a/tests/unit/cli/test_risk_cmd.py+++ b/tests/unit/cli/test_risk_cmd.py@@ -116,3 +116,178 @@ def test_risk_help_describes_repeatable_exclude() -> None:assert result.exit_code == 0assert "-x, --exclude PATTERN" in result.outputassert "Repeatable" in result.output+++# ---------------------------------------------------------------------------+# --fail-above-percentile: the CI gate on risk_percentile+# ---------------------------------------------------------------------------+++def _ranked_repo(tmp_path: Path) -> Path:+ """Enough history to rank HEAD against: a seed and nine one-file commits."""+ repo = tmp_path / "repo"+ repo.mkdir()+ _git(["init", "-q", "-b", "main"], repo)+ _commit(repo, {"README.md": "# seed\n"}, "chore: seed")+ for index in range(9):+ _commit(repo, {f"src/f{index}.py": f"value = {index}\n"}, f"feat: add {index}")+ return repo+++def _gate(repo: Path, *args: str, env: dict[str, str] | None = None):+ from repowise.core.ci.base import CI_BASE_VARS++ return CliRunner(env={**dict.fromkeys(CI_BASE_VARS, ""), **(env or {})}).invoke(+ risk_command, ["--path", str(repo), *args]+ )+++def test_gate_passes_at_or_below_the_percentile_and_fails_above(tmp_path: Path) -> None:+ repo = _ranked_repo(tmp_path)++ passed = _gate(repo, "HEAD", "--fail-above-percentile", "100", "--format", "json")+ assert passed.exit_code == 0, passed.output+ payload = json.loads(passed.stdout)+ gate = payload["gate"]+ assert (gate["fail_above_percentile"], gate["status"]) == (100.0, "pass")+ # Unrounded, beside the rounded risk_percentile.+ assert round(gate["percentile"], 1) == payload["risk_percentile"]++ failed = _gate(repo, "HEAD", "--fail-above-percentile", "0", "--format", "json")+ assert failed.exit_code == 1, failed.output+ assert json.loads(failed.stdout)["gate"]["status"] == "fail"+++def test_json_has_no_gate_block_without_the_flag(tmp_path: Path) -> None:+ result = _gate(_ranked_repo(tmp_path), "HEAD", "--format", "json")++ assert result.exit_code == 0, result.output+ assert "gate" not in json.loads(result.stdout)+++def test_an_unranked_change_cannot_be_gated(tmp_path: Path) -> None:+ repo = _ranked_repo(tmp_path)++ disabled = _gate(repo, "HEAD", "--baseline", "0", "--fail-above-percentile", "90")+ assert disabled.exit_code == 2+ assert "--baseline 0" in disabled.output++ young = tmp_path / "young"+ young.mkdir()+ _git(["init", "-q"], young)+ _commit(young, {"a.py": "a = 1\n"}, "feat: a")+ _commit(young, {"b.py": "b = 1\n"}, "feat: b")+ few = _gate(young, "HEAD", "--fail-above-percentile", "90", "--format", "json")+ assert few.exit_code == 2+ assert json.loads(few.stdout)["error"] == "no_baseline"+++def test_an_unreadable_change_is_exit_2_for_every_revspec_run(tmp_path: Path) -> None:+ repo = _ranked_repo(tmp_path)++ for args in (["no-such-ref"], ["no-such-ref", "--fail-above-percentile", "90"]):+ result = _gate(repo, *args, "--baseline", "0")+ assert result.exit_code == 2, result.output+ assert "Could not read change" in result.output+++def test_the_gate_scores_the_ci_change_when_revspec_is_omitted(tmp_path: Path) -> None:+ repo = _ranked_repo(tmp_path)+ _git(["update-ref", "refs/remotes/origin/main", "HEAD~1"], repo)+ # A dirty tree would be the subject without the flag; the gate ignores it.+ (repo / "src" / "f0.py").write_text("value = 100\n", encoding="utf-8")++ gated = _gate(+ repo, "--fail-above-percentile", "100", "--format", "json",+ env={"GITHUB_BASE_REF": "main"},+ )+ assert gated.exit_code == 0, gated.output+ assert json.loads(gated.stdout)["ref"] == "origin/main...HEAD"++ ungated = _gate(repo, "--baseline", "0", "--format", "json")+ assert json.loads(ungated.stdout)["working_tree"] is True+++def test_the_gate_without_a_target_branch_exits_2(tmp_path: Path) -> None:+ repo = _ranked_repo(tmp_path)+ _git(["checkout", "-q", "--detach"], repo)+ _git(["branch", "-D", "main"], repo)++ result = _gate(repo, "--fail-above-percentile", "90")+ assert result.exit_code == 2+ assert "Pass REVSPEC" in result.output+++def test_markdown_and_github_lead_with_the_verdict(tmp_path: Path) -> None:+ repo = _ranked_repo(tmp_path)+ summary = tmp_path / "summary.md"++ md = _gate(repo, "HEAD", "--fail-above-percentile", "0", "--format", "markdown")+ assert md.exit_code == 1+ first, _, scope = md.stdout.splitlines()[:3]+ assert first.startswith("**Above the 0th percentile gate**: larger and more spread out than ")+ assert first.endswith("% of recent commits")+ assert "absolute" not in md.stdout+ assert "across 1 directory" in scope++ gh = _gate(+ repo, "HEAD", "--fail-above-percentile", "0", "--format", "github",+ env={"GITHUB_STEP_SUMMARY": str(summary)},+ )+ assert gh.exit_code == 1+ assert gh.stdout.startswith("::error::Above the 0th percentile gate: ")+ assert summary.read_text(encoding="utf-8").startswith("**Above the 0th percentile gate**")+++def test_ci_formats_score_the_ci_change_without_the_gate(tmp_path: Path) -> None:+ repo = _ranked_repo(tmp_path)+ _git(["update-ref", "refs/remotes/origin/main", "HEAD~1"], repo)++ md = _gate(repo, "--format", "markdown", env={"GITHUB_BASE_REF": "main"})+ assert md.exit_code == 0, md.output+ assert md.stdout.startswith("**Change risk**: larger and more spread out than ")+ assert "`origin/main...HEAD`" in md.stdout+++def _scored(percentile: float | None):+ from types import SimpleNamespace++ return SimpleNamespace(percentile=percentile)+++def test_percentile_text_never_hides_which_side_of_the_gate() -> None:+ from repowise.core.analysis.change_risk.render import (+ _rank_text,+ headline,+ percentile_gate,+ percentile_text,+ )++ assert _rank_text(95.0) == "95th"+ assert _rank_text(95.4) == "95.4th"+ assert _rank_text(95.001) == "95.001th"+ assert "." not in _rank_text(95.0).removesuffix("th")+ # Rounds as usual when rounding agrees with the gate.+ assert percentile_text(_scored(96.7), 95) == "97th"+ assert percentile_text(_scored(21.0)) == "21st"+ # Just above an integer gate: rounding to 95 would read as "not above".+ assert percentile_text(_scored(95.4), 95) == "95.4th"+ assert percentile_text(_scored(95.0004), 95) == "95.0004th"+ assert percentile_gate(_scored(95.0004), 95) == "fail"+ assert percentile_text(_scored(None)) == "unranked"+ unranked = headline(_scored(None), 95, markdown=False)+ assert unranked == "Change risk: unranked · too few recent commits to rank it"+ assert headline(_scored(40.0), 95, markdown=False) == (+ "Within the 95th percentile gate: larger and more spread out than 40% of recent commits"+ )+++def test_target_mode_refuses_ci_formats_and_the_gate(tmp_path: Path) -> None:+ repo = _ranked_repo(tmp_path)++ fmt = _gate(repo, "--target", "src/f0.py", "--format", "markdown")+ assert fmt.exit_code == 2+ assert "--target supports --format table or json" in fmt.output+ gate = _gate(repo, "--target", "src/f0.py", "--fail-above-percentile", "90")+ assert gate.exit_code == 2+ assert "gates a change" in gate.outputdiff --git a/tests/unit/server/mcp/test_tool_change_risk.py b/tests/unit/server/mcp/test_tool_change_risk.pyindex 3e5d593f7..3b835eea4 100644--- a/tests/unit/server/mcp/test_tool_change_risk.py+++ b/tests/unit/server/mcp/test_tool_change_risk.py@@ -441,6 +441,46 @@ async def test_patch_coverage_is_the_gate_computation_on_stored_coverage(assert partial["gate"] == "not_set"+async def test_patch_coverage_rows_carry_their_files_risk(monkeypatch, factory, session, tmp_path):+ from repowise.core import git_refs+ from repowise.core.analysis.health.coverage import file_coverage+ from repowise.core.persistence.crud import (+ save_coverage_files,+ upsert_git_metadata,+ upsert_repository,+ )++ repo = await upsert_repository(session, name="r", local_path=str(tmp_path))+ await save_coverage_files(+ session,+ repo.id,+ [file_coverage("a.py", [], [1]), file_coverage("hot.py", [], [1])],+ source_format="lcov",+ ingested_commit_sha="abc",+ )+ await upsert_git_metadata(session, repository_id=repo.id, file_path="hot.py", is_hotspot=True)+ await session.commit()++ async def _repo(_session, *_a, **_k):+ return repo++ monkeypatch.setattr(tool, "_get_repo", _repo)+ monkeypatch.setattr(git_refs, "resolve", lambda _p, _rev: "abc")+ ctx = SimpleNamespace(session_factory=factory, path=tmp_path)++ block = await tool._patch_coverage_block(+ ctx, {"a.py": {1}, "hot.py": {1}}, None, "HEAD", _scored(), _collector(tmp_path)+ )++ # tmp_path is no git repository: the index answers where it has a row.+ hot, plain = block["files"]+ assert hot["file_path"] == "hot.py"+ assert hot["risk"]["basis"] == "index" and hot["risk"]["reasons"] == ["hotspot"]+ assert plain["risk"]["basis"] == "unavailable"+ assert block["risky"]["file_count"] == 1++@pytest.mark.asyncioasync def test_patch_coverage_is_absent_without_a_readable_change_or_index(monkeypatch, factory, tmp_pathdiff --git a/tests/unit/server/test_patch_coverage_schema.py b/tests/unit/server/test_patch_coverage_schema.pyindex 7a782a54e..09355a473 100644--- a/tests/unit/server/test_patch_coverage_schema.py+++ b/tests/unit/server/test_patch_coverage_schema.py@@ -3,7 +3,12 @@from __future__ import annotationsfrom repowise.core.analysis.health.coverage import PathGate, file_coverage-from repowise.core.analysis.patch_coverage import PatchScope, compute_patch_coverage+from repowise.core.analysis.patch_coverage import (+ FileRisk,+ PatchScope,+ attach_risk,+ compute_patch_coverage,+)from repowise.server.schemas.patch_coverage import PatchCoverageResponse@@ -54,3 +59,14 @@ def test_every_to_dict_key_round_trips_through_the_response_model() -> None:assert (wire["gate"], wire["scope"]["ignored_file_count"]) == ("too_small", 1)assert [g["gate"] for g in wire["path_gates"]] == ["too_small", "not_set"]assert PatchCoverageResponse.model_validate(wire).model_dump() == wire++ # With risk attached, each row and the risky-file summary round-trip too.+ risks = {+ "a.py": FileRisk(2.5, 14, True, False, "git_and_index", True, ("hotspot",)),+ "new.py": FileRisk(fix_pressure=0.0, basis="git"),+ }+ risked = attach_risk(pc, risks, risky_threshold=90).to_dict()+ # 1 of 3 risky lines misses 90%, and the change is under min_coverable_lines.+ assert risked["risky"]["gate"] == "too_small"+ assert risked["files"][0]["risk"]["basis"] == "git_and_index"+ assert PatchCoverageResponse.model_validate(risked).model_dump() == riskeddiff --git a/tests/unit/server/test_risk_range.py b/tests/unit/server/test_risk_range.pyindex c1fb77b96..369545dfc 100644--- a/tests/unit/server/test_risk_range.py+++ b/tests/unit/server/test_risk_range.py@@ -269,6 +269,9 @@ async def test_patch_coverage_reads_stored_coverage(assert data["files"][0]["uncovered_ranges"] == [[3, 3]]assert data["scope"]["freshness"] == "current"assert data["scope"]["label"] == f"{base}...HEAD"+ # The index has no git row for a file the change adds, so git alone answers.+ assert data["files"][0]["risk"]["basis"] == "git"+ assert data["risky"]["file_count"] == 0assert (await client.get(url, params={"base": "nope"})).status_code == 400# The repository's coverage config reaches the stored-coverage surface as@@ -294,3 +297,38 @@ async def test_patch_coverage_reads_stored_coverage(stale = await _with_config(one_gate)assert stale["scope"]["freshness"] == "stale"assert [g["gate"] for g in stale["path_gates"]] == ["no_data"]+++async def test_patch_coverage_rows_carry_index_risk(+ client: AsyncClient, session, git_repo: Path, tmp_path: Path+) -> None:+ from repowise.core.analysis.health.coverage import file_coverage+ from repowise.core.persistence.crud import save_coverage_files, upsert_git_metadata++ base = subprocess.run(+ ["git", "rev-parse", "HEAD"], cwd=git_repo, check=True, capture_output=True, text=True+ ).stdout.strip()+ head = _commit(git_repo, {"src/a.py": "x = 1\ny = 2\nz = 3\n"}, "feat: add a")+ repo = await _register(client, tmp_path)+ await save_coverage_files(+ session,+ repo["id"],+ [file_coverage("src/a.py", [1, 2], [1, 2, 3])],+ source_format="lcov",+ ingested_commit_sha=head,+ )+ await upsert_git_metadata(+ session, repository_id=repo["id"], file_path="src/a.py", is_hotspot=True+ )+ await session.commit()++ data = (+ await client.get(f"/api/repos/{repo['id']}/health/coverage/patch", params={"base": base})+ ).json()++ risk = data["files"][0]["risk"]+ assert risk["basis"] == "git_and_index"+ assert risk["risky"] is True and risk["reasons"] == ["hotspot"]+ assert data["risky"]["file_count"] == 1+ assert data["risky"]["patch_coverage_pct"] == 66.66diff --git a/tests/unit/test_ci_action.py b/tests/unit/test_ci_action.pyindex c8024d09f..358c93664 100644--- a/tests/unit/test_ci_action.py+++ b/tests/unit/test_ci_action.py@@ -50,7 +50,16 @@ def test_every_variable_the_runner_reads_is_passed_by_the_action() -> None:def test_the_gitlab_template_parses() -> None:jobs = yaml.safe_load(GITLAB.read_text(encoding="utf-8"))- assert {"repowise-coverage", "repowise-doc-drift", "repowise-security"} <= set(jobs)+ assert {+ "repowise-coverage",+ "repowise-doc-drift",+ "repowise-security",+ "repowise-risk",+ } <= set(jobs)+ risk = " ".join(jobs["repowise-risk"]["script"])+ assert "--fail-above-percentile" in risk and "--format markdown" in risk+ assert "REPOWISE_RISK_FAIL_ABOVE_PERCENTILE" in jobs["repowise-risk"]["rules"][0]["if"]+ assert "--fail-under-risky" in " ".join(jobs["repowise-coverage"]["script"])def _gitlab_jobs() -> dict:@@ -176,6 +185,7 @@ def fake_repowise(tmp_path: Path) -> Path:' coverage) exit "${FAKE_COVERAGE:-0}" ;;\n'' doc-drift) exit "${FAKE_DOC_DRIFT:-0}" ;;\n'' security) exit "${FAKE_SECURITY:-0}" ;;\n'+ ' risk) exit "${FAKE_RISK:-0}" ;;\n'"esac\n",encoding="utf-8",newline="\n",@@ -215,9 +225,11 @@ def _run(tmp_path: Path, bin_dir: Path, **env: str) -> tuple[int, str, str]:"COVERAGE_REPORT": "","COVERAGE_FAIL_UNDER": "","COVERAGE_MIN_COVERABLE_LINES": "",+ "COVERAGE_FAIL_UNDER_RISKY": "","DOC_DRIFT_BASELINE": "","SECURITY_FAIL_ON": "high","SECURITY_BASELINE": "",+ "RISK_FAIL_ABOVE_PERCENTILE": "","SARIF": "false","SARIF_DIR": str(tmp_path / "sarif"),**env,@@ -267,6 +279,32 @@ def test_inputs_reach_the_commands(tmp_path, fake_repowise) -> None:)+def test_risk_inputs_reach_the_commands(tmp_path, fake_repowise) -> None:+ code, outputs, calls = _run(+ tmp_path,+ fake_repowise,+ CHECKS="coverage,risk",+ COVERAGE_FAIL_UNDER_RISKY="90",+ RISK_FAIL_ABOVE_PERCENTILE="95",+ FAKE_RISK="1",+ )+ assert code == 1+ assert outputs.split() == ["coverage=0", "risk=1"]+ coverage, risk = calls.splitlines()+ assert coverage == "coverage check --format github --fail-under-risky 90"+ # Gated, the CLI reads the target branch itself.+ assert risk == "risk --format github --fail-above-percentile 95"+++def test_ungated_risk_reports_without_a_percentile(tmp_path, fake_repowise) -> None:+ # The CLI reads the target branch itself for --format github.+ code, _, calls = _run(tmp_path, fake_repowise, CHECKS="risk", GITHUB_BASE_REF="main")+ assert code == 0+ assert calls.strip() == "risk --format github"+ _, _, based = _run(tmp_path, fake_repowise, CHECKS="risk", BASE="v1...HEAD")+ assert based.splitlines()[-1] == "risk v1...HEAD --format github"++def test_an_unknown_check_is_refused(tmp_path, fake_repowise) -> None:code, _, calls = _run(tmp_path, fake_repowise, CHECKS="coverage,lint")assert code == 2