|
| 1 | +# ADR-14: A changed statement in a partially-taken branch blocks SAFE |
| 2 | + |
| 3 | +> Status: **Accepted** |
| 4 | +> Date: 2026-09-01 |
| 5 | +> Deciders: @omsherikar |
| 6 | +> Closes: #117. Follow-up to ADR-11 (`11-safe-coverage-rule.md`). |
| 7 | +
|
| 8 | +## Context |
| 9 | + |
| 10 | +`SAFE` rests on a statement-level coverage rule (ADR-11): every coverable changed |
| 11 | +statement must have executed. But statement execution does not mean the changed |
| 12 | +*logic* was exercised. A changed `if`/`elif` whose header ran but whose branch a |
| 13 | +test never entered counts as "covered," so the change earns `SAFE` with a branch |
| 14 | +nobody tested. |
| 15 | + |
| 16 | +A changed conditional is one of the most common shapes of a real regression, and |
| 17 | +it is exactly where a statement-level answer is weakest: the header executed, so |
| 18 | +the statement is covered, while the behaviour that changed sits in a branch no |
| 19 | +test took. |
| 20 | + |
| 21 | +Real example, measured. `playground/legacy-python/dispatch.py`: |
| 22 | + |
| 23 | +```python |
| 24 | + if isinstance(shape, Circle): |
| 25 | + return math.pi * shape.radius * shape.radius |
| 26 | + elif isinstance(shape, Square): |
| 27 | + return shape.side * shape.side |
| 28 | + elif isinstance(shape, Rectangle): # line 32 — executed |
| 29 | + return shape.width * shape.height |
| 30 | + raise TypeError("unknown shape") # line 34 — arc 32→34 never taken |
| 31 | +``` |
| 32 | + |
| 33 | +`coverage.py --branch` reports `missing_branches: [[32, 34]]`: the arc from the |
| 34 | +final `elif` to the `raise` was never taken, because no test passed an unknown |
| 35 | +shape. Statement coverage of line 32 is complete. Change line 32 and it earns |
| 36 | +`SAFE` today, with the error path untested. |
| 37 | + |
| 38 | +ADR-11 recorded this as an explicit follow-up. #117 required the decision be made |
| 39 | +on **measured** cost and flip rate, not asserted, because `SAFE` had already |
| 40 | +tightened twice in the 0.4.x line and over-tightening it into unreachability |
| 41 | +would make the verdict ignored rather than honest. |
| 42 | + |
| 43 | +## Measurements |
| 44 | + |
| 45 | +Both taken on this repository's `playground/` before the decision. |
| 46 | + |
| 47 | +**Cost (AC1).** On `large-python` (1,201 files, 600 tests), a real suite: |
| 48 | + |
| 49 | +| run | wall-clock | |
| 50 | +| --- | --- | |
| 51 | +| `coverage run` (statement) | 10.48s, 10.04s | |
| 52 | +| `coverage run --branch` | 9.93s, 9.91s | |
| 53 | + |
| 54 | +`--branch` added **no measurable runtime cost** — the branch runs were marginally |
| 55 | +faster, within noise. The JSON report grew ~1.5x (3.66 MB → 5.60 MB), a parse |
| 56 | +cost, not a run cost. The issue's "measurably slower" concern is not borne out. |
| 57 | + |
| 58 | +**Flip surface (AC2).** Branch gating only flips a `SAFE` when a *changed* |
| 59 | +statement is itself a partially-taken branch. Density of that shape: |
| 60 | + |
| 61 | +| repo | statements | partial branches | share | |
| 62 | +| --- | --- | --- | --- | |
| 63 | +| `large-python` (straight-line) | 30,600 | 0 | 0% — branch gating is a no-op | |
| 64 | +| `legacy-python` (real control flow) | 73 | 1 | 1.4% | |
| 65 | + |
| 66 | +This is the load-bearing finding. ADR-11 flips a `SAFE` for **any** diff touching |
| 67 | +an uncovered statement in a partially-covered file — a broad surface. Branch |
| 68 | +gating flips only a diff whose changed lines are **conditionals with an untaken |
| 69 | +arc** — a strict, narrow subset, and precisely the false-SAFE case. Straight-line |
| 70 | +changes and fully-exercised conditionals are untouched. Stacking this on ADR-11 |
| 71 | +therefore adds a small, targeted flip, not a second broad tightening. |
| 72 | + |
| 73 | +## Decision |
| 74 | + |
| 75 | +**A changed statement that `coverage.py` reports as a partially-taken branch |
| 76 | +blocks `SAFE`, flooring the verdict to `UNPROVEN`.** |
| 77 | + |
| 78 | +Formally: with branch data available, a changed statement disqualifies `SAFE` if |
| 79 | +it is the source line of an arc in the file's `missing_branches`. This is added |
| 80 | +to the ADR-11 conjunct; both must hold for `SAFE`. |
| 81 | + |
| 82 | +`coverage run` gains `--branch`. The JSON parse reads `missing_branches` per file |
| 83 | +alongside the existing `executed_lines` / `missing_lines`. |
| 84 | + |
| 85 | +Two boundaries keep the direction fail-safe: |
| 86 | + |
| 87 | +1. **Unavailable branch data never grants `SAFE`.** An older `coverage.py`, a |
| 88 | + parse that yields no `missing_branches`, or a measurement failure falls back |
| 89 | + to the ADR-11 statement rule — it never uses absent branch data as evidence of |
| 90 | + a covered branch. Missing information floors, it does not clear. |
| 91 | +2. **The rule only tightens.** It can move `SAFE` → `UNPROVEN` and nothing else. |
| 92 | + No path it introduces can move `UNSAFE` or `UNPROVEN` → `SAFE`. The branch gap |
| 93 | + is enforced in two places: `attributeChangedLines` folds it into |
| 94 | + `changedLinesCovered` (the ADR-11 conjunct), and `fuseVerdict`'s `SAFE` gate |
| 95 | + independently asserts `partialBranches` is empty. The second is defence in |
| 96 | + depth: it makes a future producer of `partialBranches` fail safe by |
| 97 | + construction rather than by remembering to floor `changedLinesCovered`. |
| 98 | + |
| 99 | +`reportVersion` stays `1`: `missing_branches` is additive to the coverage sub- |
| 100 | +object, no field is renamed, removed or retyped. The **semantics** of `verdict` |
| 101 | +tighten, which `engineVersion` (ADR-13) already exists to disambiguate for a |
| 102 | +consumer storing fleet history. |
| 103 | + |
| 104 | +Version: **patch**, shipping in 0.4.x, per the repository's earned-minor policy. |
| 105 | +The changelog leads with what `SAFE` now means, since the version number does not |
| 106 | +carry it. |
| 107 | + |
| 108 | +## Alternatives considered |
| 109 | + |
| 110 | +### Alternative A: disclose, don't block |
| 111 | + |
| 112 | +Report partial branches as an advisory (like `changedStatements` before ADR-11) |
| 113 | +and leave the verdict `SAFE`. |
| 114 | + |
| 115 | +Rejected. This is the exact shape ADR-11 rejected: a verdict that claims proof it |
| 116 | +does not have, with the shortfall disclosed beside it. Every automated consumer |
| 117 | +gates on `verdict`, not on a branch advisory. A changed conditional with an |
| 118 | +untested branch is a textbook false `SAFE`; disclosing it is not a substitute for |
| 119 | +declining to certify it. |
| 120 | + |
| 121 | +### Alternative B: gate on the partial-branch COUNT (ratio threshold) |
| 122 | + |
| 123 | +Block only when partial branches exceed some fraction of changed statements. |
| 124 | + |
| 125 | +Rejected for the reason ADR-11 rejected a coverage ratio: an arbitrary constant |
| 126 | +at the centre of the honesty of the verdict, indefensible at every value, and |
| 127 | +inexplicable in a sentence. One untested branch on a changed line is one untested |
| 128 | +branch. |
| 129 | + |
| 130 | +### Alternative C: leave it at statement level |
| 131 | + |
| 132 | +The status quo. Rejected: it is the open false-SAFE surface #117 exists to close, |
| 133 | +and the measured cost of closing it is ~zero. |
| 134 | + |
| 135 | +## Consequences |
| 136 | + |
| 137 | +- **Positive.** The most common regression shape — a changed conditional — can no |
| 138 | + longer earn `SAFE` with a branch no test entered. `SAFE` moves closer to |
| 139 | + meaning what its reason says. |
| 140 | +- **Positive.** Fail-safe only. Every verdict this changes moves `SAFE` → |
| 141 | + `UNPROVEN`; measured flip surface is narrow and surgical. |
| 142 | +- **Positive.** No runtime cost (measured). The usability objection the issue |
| 143 | + raised does not materialise: it cannot make `SAFE` unreachable for straight-line |
| 144 | + code (0% affected) and bites conditional changes only where a branch is |
| 145 | + genuinely untested. |
| 146 | +- **Negative.** A changed conditional in a weakly-tested repo now reads |
| 147 | + `UNPROVEN` where it read `SAFE`. That is an honest report of an untested branch, |
| 148 | + but it must be explained by the reason string, not left as a bare downgrade. |
| 149 | +- **Neutral.** Report ~1.5x larger. Parse cost only; no user-visible effect. |
| 150 | +- **Neutral.** Exit codes unchanged — `SAFE` and `UNPROVEN` both exit 0. |
| 151 | + |
| 152 | +## Compliance |
| 153 | + |
| 154 | +- **Red-first (AC3):** a changed `if`/`elif` whose one branch never executes, with |
| 155 | + a green suite and complete statement coverage, must return `UNPROVEN`. Proven |
| 156 | + failing against `main` (where it returns `SAFE`) before the fix. |
| 157 | +- **Negative (AC4):** a changed conditional whose branches are all exercised must |
| 158 | + still reach `SAFE`. Proven passing, so the rule does not over-block. |
| 159 | +- **Fallback:** a run without branch data (branch parse empty) falls back to the |
| 160 | + ADR-11 rule and does not crash or wrongly grant `SAFE`. |
| 161 | +- **ADR-11's cross-file statement test must keep passing unmodified**, proving the |
| 162 | + statement conjunct was not traded away. |
| 163 | + |
| 164 | +## Open questions / follow-ups |
| 165 | + |
| 166 | +- [ ] Path and condition coverage (`a and b` where only `a` was evaluated). A |
| 167 | + strictly larger gap than branches; out of scope here. |
| 168 | +- [ ] Non-Python languages. Coverage is Python-only, so this rule never applies to |
| 169 | + a TypeScript diff, which already caps at `UNPROVEN`. |
| 170 | +- [ ] #116, mutation testing: coverage — statement or branch — proves a line |
| 171 | + *executed*, not that a test *asserts* on it. The next layer. |
0 commit comments