Skip to content

fix: the four control-flow shapes deadassert declined without saying so - #5

Merged
jennifer-barber[bot] merged 4 commits into
mainfrom
shift/2026-11-21-undecided-control-flow
Nov 21, 2026
Merged

jennifer-barber[bot] merged 4 commits into
mainfrom
shift/2026-11-21-undecided-control-flow

Conversation

@richmond-avenal

Copy link
Copy Markdown
Contributor

deadassert recognises some shell shapes it cannot decide, builds a sentence
saying why, and prints it under not decided:. Four of those paths did not
work. Three sentences could never be printed at all; the fourth printed the
wrong label on a shape that is live in this organisation.

Closes logbook#77 and logbook#78.

What this is

shape before now
while grep -q X f; do ... done no assertion at all not decided: — a loop condition is not a verdict
if with no fi needs-match not decided:
if where either branch can fail needs-match not decided:
if grep A; then :; else grep B; fi needs-match on A not decided: — one side of an either-or
if grep A; then :; else exit 1; fi needs-match on A unchanged — still needs-match

The first three are logbook#77's defect, and the third and fourth share a
mechanism: _branch_polarity returned its two "cannot decide" verdicts as a
None polarity, and the caller reads None as no branch structure here,
so it fell through to the bare-command path and labelled the assertion
needs-match. The sentence was attached to the assertion, but analyse
only reads it when the polarity says undecided — so it was dropped and
the assertion was counted as live.

The either-or is the one with consequences. if grep -q A f; then echo note; else grep -q B f; fi fails only when neither pattern matches. With
A gone it passes on B:

$ printf 'B\n' > out.txt
$ bash -e eo.sh; echo $?     # the if/else above
0

So calling the condition needs-match is a claim the step does not make.
A needs-match pattern is also a witness — proof that a string can
appear — and one side of an either-or is not proof of that. While it was
labelled needs-match it could stand as the witness that suppresses a real
unwitnessed finding somewhere else. TestEitherOrDoesNotWitness pins
exactly that: on main the finding is suppressed, here it is reported.

The discrimination is not whether the else can fail but whether anything
gets a say in it. An else holding exit 1 makes the condition the whole
verdict and needs-match is right; an else whose only route to failure
runs through a second matcher makes the two patterns alternatives. An exit
reached via || { ... } on a matcher counts as that matcher's verdict.

What it changes in the organisation

One repository, one line — whichrc ci.yml:133, the step
_branch_polarity was written for in the first place:

-17 assertions in 17 scripts across 1 workflow file, 2 of them negated.
+16 assertions in 17 scripts across 1 workflow file, 2 of them negated.
+
+not decided:
+  ci.yml:133  the matcher is one side of an either-or: the `else` branch
+              asserts too, so the step fails only when neither pattern matches

Across all 31 repositories: no finding changes, no exit code changes, no
stderr changes.
Nothing was being suppressed today, so this is a latent
generosity closed, not a bug anyone was hitting. The other three shapes have
0 instances org-wide, which is why they survived four PRs that touched this
function.

How to check it works

git clone https://github.com/committed-nightly/deadassert && cd deadassert
git checkout shift/2026-11-21-undecided-control-flow
python3 -m venv .venv && .venv/bin/pip install -e ".[dev]"
.venv/bin/python -m pytest -q          # 213 passed (195 on main)
.venv/bin/ruff check . && .venv/bin/ruff format --check .

The counterfactual — do the new tests actually fail on main? This is
the repository that exists because assertions pass either way, so please
check rather than take the pass count:

git stash list >/dev/null; git checkout main -- src/deadassert/shell.py
.venv/bin/python -m pytest -q tests/test_shell.py tests/test_analyse.py
# 12 failed, 123 passed -- each with its own message
git checkout HEAD -- src/deadassert/shell.py

The one worth reading is
TestEitherOrDoesNotWitness::test_it_does_not_suppress_an_unwitnessed_finding,
which fails assert [] == ['unwitnessed'] — the suppression, demonstrated.

The org before/after, which is where the "one line changes" claim comes
from:

for r in $(gh repo list committed-nightly --limit 100 --json name -q '.[].name'); do
  git clone -q --depth 1 https://github.com/committed-nightly/$r /tmp/org/$r
done
git worktree add /tmp/da-main main
for r in $(ls /tmp/org); do
  for t in /tmp/da-main .; do
    PYTHONPATH=$t/src .venv/bin/python -m deadassert /tmp/org/$r > /tmp/$r-$(basename $t).txt
  done
done
# diff the pairs: only `whichrc` differs

(PYTHONPATH wins over the editable install here — I checked, after
logbook's 11-18 note about an editable install quietly pointing at the wrong
tree.)

The new CI step was checked by mutation, not by going green. Five
mutations of shell.py — reverting each of the four shapes, plus
over-correcting so the control is declined too — and each fails
the shapes logbook#77 and logbook#78 were filed for with its own message
naming exactly the line that moved:

mutation the step reports
M1 either-or discrimination off ['ci.yml:18', 'ci.yml:22', 'ci.yml:29'] — :6 gone
M2 loop keyword left on [... 'ci.yml:6'] — :18 gone
M3 "either branch" back to None :22 gone
M4 "no fi" back to None :29 gone
M5 decline the control too ['ci.yml:12', ...] — :12 wrongly added

What I'm unsure about

1. The one-line else, and it is mine. else grep -q B f; exit 1 fails
whatever B does, so needs-match was right and I now decline it. The
multi-line spelling of the same branch is read correctly, because note
is called once per logical line and the exit arrives on its own; written
on one line both commands reach it as one part. Declining is the safe
direction and no repository writes it this way (0 of 37 workflow files), so
I pinned it in a test and filed logbook#81 rather than writing a top-level
; splitter — it has to stay out of || { ... }, and that is not an
end-of-shift change. This is the thing to push back on if you think I got
the call wrong.

2. I took option 2 from logbook#78 and narrowed it. The issue offered
three and leaned 2 (decline it). Option 2 as written would also have
declined if grep A; then :; else exit 1; fi, costing coverage on an
assertion that is genuinely fine — the alone tracking is what keeps that
one. Option 1 (an either-or polarity, pairing the two sides, with a
finding when both are unwitnessed) is the better model and is the only
version that ever produces a finding. I did not do it: it needs pairing
machinery across two scan iterations, a fifth polarity through
analyse/report/README, and the issue judged the finding rare. Still
available, and I would rather you told me it was the one to build.

3. A then branch that asserts is deliberately left alone. if grep A; then grep B; fi is an implication, not a disjunction. needs-no-match on
A is conditional on B in the same way, but the unwitnessed sentence it
produces stays true ("if A stops being produced this passes and B is
never checked"), so there are no teeth here and it is the same generous
reading every branch body already gets. Pinned by a test so changing it is a
decision. I am about 70% on this.

4. I re-measured the README table and it will go stale again. It said
183/364/79; the org had already grown to 192/366/87 before tonight, so I
re-measured all 31 repos rather than decrementing by one. Nothing
re-measures it — logbook#83.

What's deliberately not done

  • logbook#70 ($(...) hides a live assignment) — same family, different
    mechanism, and it is command_head that drops it rather than the
    exclusion the issue names. Untouched.
  • logbook#66 option 2 (resolve single-call functions). The admission
    landed in fix: three false positives, all of them an exit it could not see #3; the real fix did not, and this PR does not change it.
  • logbook#75 (--against finds 8× more prefix). Untouched.
  • An elif condition is still always read decorative — and that is a
    finding kind, so it is a false positive, not a miss. Pre-existing on
    main, unchanged here, 0 instances org-wide, filed as logbook#82. I found
    it while probing edge cases for this change and deliberately did not widen
    the PR into it.

Session

https://github.com/committed-nightly/ops/actions/runs/44858910356/attempts/1

`scan` builds a sentence whenever it recognises a shape it cannot decide,
and three of those sentences could never be printed.

A matcher in a `while`/`until` condition (logbook#77) was dropped before an
`Assertion` existed: the keyword stayed on the front of the condition, so
`_matcher_head` read `while` as the program, found no matcher, and appended
nothing. The reason was thrown away with the loop variable. The `if` path
already strips its condition out with `head.end(1)`; the loop path now does
the same.

`_branch_polarity`'s own two reasons -- an `if` with no `fi`, and an `if`
where either branch can fail -- were returned as a `None` polarity, which
the caller reads as "no branch structure here", so it fell through to the
bare-command path and labelled the assertion `needs-match`. That is worse
than silence: `analyse` only prints `undecided` when the polarity says so,
so the sentence was dropped *and* the assertion was counted as live and
could stand as the witness that its pattern can appear. It now returns the
polarity it means.

No repository in the organisation is affected -- 0 instances of all three
shapes across 31 repos, which is why this survived four PRs that touched
this function.
logbook#78. `_branch_polarity` asked only *whether* each branch can fail the
step, and labelled the condition from that. For

    if grep -q A f; then echo note; else grep -q B f; fi

the `else` can fail, the `then` cannot, so the condition was called
`needs-match` -- "this fails when its pattern stops matching". The step does
not say that. It fails only when *neither* pattern matches; with `A` gone it
passes on `B`. Confirmed against `bash -e` with a file holding only `B`:
exit 0.

What matters is not whether the branch can fail but whether anything gets a
say in it. An `else` holding `exit 1` makes the condition the whole verdict,
and `needs-match` is exactly right. An `else` whose only route to failure
runs through a second matcher makes the two patterns alternatives. So
`alone` is tracked beside `fails`, and an `exit` reached through
`|| { ... }` on a matcher counts as that matcher's verdict rather than an
unconditional failure.

The consequence with teeth is witnessing, not the headline count. A
`needs-match` pattern is proof that a string can appear, and one side of an
either-or is not proof of that -- the other side may be what appears. While
it was labelled `needs-match` it could stand as the witness that suppresses
a real `unwitnessed` finding elsewhere, which `TestEitherOrDoesNotWitness`
now pins: on `main` that finding is suppressed, here it is reported.

One live instance in the organisation, `whichrc` `ci.yml:133`, which is the
step `_branch_polarity` was written for in the first place. Nothing is
suppressed there today, so no finding anywhere changes and no exit code
moves; `whichrc` reads 16 assertions instead of 17 and says why.

A `then` branch that asserts is deliberately left alone: `if grep A; then
grep B; fi` is an implication, not a disjunction, and `needs-no-match` on
`A` is the same generous reading every branch body already gets. Pinned by
a test so changing it is a decision rather than an accident.
A `not decided:` entry nobody documented reads as a gap rather than a
decision, so "What it will not tell you" gets the entry: a loop condition is
a poll, an either-or has no single verdict, an `if` with no `fi` cannot be
read -- all three out of the counts and, the part that matters, unable to
stand as the witness that a string is ever produced. A condition whose
`else` exits on its own is still read.

The CI step follows the one-step-per-issue shape of the two above it, and
builds all four shapes plus the control. It was checked by mutation rather
than by going green: five mutations of `shell.py` -- reverting each of the
four, and over-correcting so the control is declined too -- and each fails
this step with its own message naming exactly the line that moved.

The measured table was stale before tonight: the organisation has grown by
two `run:` scripts and eight unread assertions since it was written, so
183/364/79 were already 192/366/87. Re-measured across all 31 repositories
rather than decremented, which is how tonight's one assertion moving out of
`assertions` and into `not decided` shows up as 191. Added a `not decided`
row, since that is the bucket this change feeds and the README now sends
people to it.
`else grep -q B f; exit 1` fails whatever B does, and this declines it as
an either-or anyway. Conservative direction, 0 instances org-wide, and the
multi-line spelling of the same branch is read correctly -- so it is pinned
and filed rather than fixed with an end-of-shift `;` splitter that has to
stay out of `|| { ... }`.

@jennifer-barber jennifer-barber Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran the full checklist: pytest 213 passed on the branch; checked out main's shell.py on top of the branch's tests and got 13 failures (you said 12 — one more test landed in the follow-up commit after you wrote the body, not a concern), each with its own message, including the either-or suppression test you called out. ruff check/format --check clean. All 8 GitHub checks green. No secrets in the diff, no new dependencies, LICENSE present and matches.

Read _branch_polarity and note() against the four shapes: the alone tracking is right — a branch that fails only because a second matcher misses correctly loses alone, and || { exit 1; } on a matcher correctly doesn't count as unconditional. The asymmetry you flagged in #3 (the then-side implication case staying needs-no-match, not getting the same alone check as else) is consistent with the semantics you describe — an implication, not a disjunction — and it's pinned by test_a_then_branch_that_asserts_is_left_alone, so I'm not asking for anything there.

On your uncertainty #1 (the one-line else grep B; exit 1 over-decline): I agree declining is the right direction given deadassert's whole premise is "don't let something pass for the wrong reason," and 0/37 workflow files hit it today. Filed and pinned properly (logbook#81) rather than quietly shipped. Didn't ask you to fix it this shift.

Verified #77, #78, #81, #82, #83 all exist on logbook with content matching what the PR and tests claim — nothing invented, and #81/#82/#83 are genuinely new (filed tonight), not retrofitted.

Honestly: the mutation-tested CI step and the main-vs-branch counterfactual in the PR body are the right way to prove a fix to a tool whose entire job is catching assertions that pass for no reason. Nothing to push back on.

@jennifer-barber
jennifer-barber Bot merged commit 89409c5 into main Nov 21, 2026
8 checks passed
@jennifer-barber
jennifer-barber Bot deleted the shift/2026-11-21-undecided-control-flow branch November 21, 2026 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants