Skip to content

fix: an elif condition is a verdict, not a verdict over no branches - #6

Merged
jennifer-barber[bot] merged 3 commits into
mainfrom
shift/2026-11-25-elif-branch-walk
Nov 25, 2026
Merged

jennifer-barber[bot] merged 3 commits into
mainfrom
shift/2026-11-25-elif-branch-walk

Conversation

@richmond-avenal

Copy link
Copy Markdown
Contributor

Closes committed-nightly/logbook#82.

What this is

deadassert reported a working CI assertion as dead when the matcher was
in an elif condition. decorative is a finding kind, so the tool was telling
you to go and rewrite a guard that was load bearing:

$ deadassert elifs            # on main
ci.yml:8  decorative  'B'
    grep: cannot fail the step
    it chooses which branch runs, and neither branch can fail the step
      elif grep -q 'B' f; then

That elif's own then branch holds exit 1. Every word of the message is
false about the line.

_branch_polarity is handed the elif's own index, and depth is counted
from the if/fi tokens on each line. In elif grep ... the if is preceded
by el rather than a separator, so the depth regex never matched, depth
stayed 0, and if depth <= 0: break ended the walk on its first line — after
the condition had been stripped out as the subject. The function then fell
through to its last line: return "decorative", None. A verdict over no
branches at all.

Fixing that surfaced a second wrong answer in the same four lines, and it
is the one with a live polarity. For the condition above an elif, the
else-path failure sits behind the elif's matcher:

if grep -q 'A' f; then :; elif grep -q 'B' f; then exit 1; fi

exits 1 only when A misses and B matches. main reads 'A' as
needs-match — "the step fails when the pattern is absent" — which the step
does not claim. needs-match is live, so it counted and could stand as the
witness that suppresses someone else's unwitnessed finding. This is
logbook#78 with the second matcher one line further away, and it is now
declined with the same sentence.

Who wants it: anyone who writes a three-way check in CI. Nobody in this org
does yet — see "not live" below.

How to check it works

From a fresh clone:

git clone https://github.com/committed-nightly/deadassert && cd deadassert
git fetch origin shift/2026-11-25-elif-branch-walk
python3 -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"
pytest -q          # 223 passed (213 on main)
ruff check . && ruff format --check .

See the bug, then see it fixed. main first:

git checkout main && pip install -e . -q
mkdir -p /tmp/elifs/.github/workflows
printf '%s\n' "'on': push" "jobs:" "  j:" "    steps:" "      - run: |" \
  "          if grep -q 'A' f; then" "            echo a" \
  "          elif grep -q 'B' f; then" "            exit 1" "          fi" \
  > /tmp/elifs/.github/workflows/ci.yml
deadassert /tmp/elifs            # decorative 'B'  <- the false positive

then the branch:

git checkout shift/2026-11-25-elif-branch-walk && pip install -e . -q
deadassert /tmp/elifs

'B' is now read needs-no-match and reported unwitnessed — a correct
finding of a different kind, because nothing in that synthetic file
establishes B can ever appear. 'A' moves to not decided: with the
either-or sentence.

Note pip install -e . is not editable across a git checkout of the
source tree in the way you might assume
— it is, but the installed console
script resolves through the same tree, so do the two runs in that order and
don't keep two trees sharing one venv. That mistake cost a previous shift an
evening (#general, 11-09).

The CI step, which is the witness. It is green here and red on main:

# on this branch
python3 -c "
import yaml,sys
d=yaml.safe_load(open('.github/workflows/ci.yml'))
s=[x for j in d['jobs'].values() for x in j['steps'] if x.get('name')=='the shapes logbook#82 was filed for']
assert len(s)==1, len(s)
sys.stdout.write(s[0]['run'])" > /tmp/step82.sh
mkdir -p /tmp/w && cd /tmp/w && bash -e /tmp/step82.sh; echo "exit=$?"   # 0

Run the same script with main installed and it exits 1 at the assertion that
names the defect:

AssertionError: {('decorative', 'B'), ('decorative', 'D'), ('decorative', 'C')}

Three of that step's six assertions fail on main and pass here, one for each
half of the fix (("decorative","B") not in kinds, negated == 1, and the
either-or decline). The other three are controls that must pass on both.

How it was verified

Against bash -e, not against my reading of the code. Every expectation in
TestElifConditions is the exit status the shape really produces, taken for
each combination of which patterns are present in the file. The table is in the
class docstring. The oracle is in the commit message's terms: eight shapes,
including a negated elif, a nested if inside an elif body, and a
three-way chain.

By mutation, not by green CI. Five mutations, each failing with its own
message and each naming the part it broke:

mutation tests red
depth not seeded for an elif subject 6
gating term dropped from note() 1 — test_the_condition_above_it_is_gated_by_the_elif
subject line allowed to flip the branch 5
the no-branches guard removed 1 — the direct-call test
entered asked per line, not per token 6, incl. the shipped example and TestPolarity

Nothing moves org-wide. deadassert run over all 31 repositories, main
versus this branch: output byte-identical, exit codes included, 0 of 31 moved.
0 of 37 workflow files have a matcher in an elif, which is the same fact said
twice.

All 19 CI steps run locally, each job in its own copy of the repo with its
own venv, each run: block under bash -e.

What I'm unsure about

The entered invariant is unreachable through scan, and I kept it
anyway.
Once the seeding is right, scan only calls _branch_polarity on a
line matching ^(?:el)?if\s+, and both spellings now enter their block — so
the sentence can only be produced by calling the function directly, which is
how it is tested. I think it earns its three lines because it is precisely what
would have made this bug announce itself in not decided: rather than answer
wrongly, and logbook#77 is the same lesson one function up. But it is a guard
with no live caller and you may read that as dead code; I would not argue hard.
It also immediately caught itself being wrong (the one-line if), which is
either an argument for it or an argument about its author.

needs-no-match for an elif that is gated by the conditions above it. In
shape 1, 'B' only gets to run when 'A' missed, so 'B''s polarity is
conditional in a way the single word does not convey. I went with the module's
existing convention — polarity is what the status does to the step when the
command is reached
, which is already how a matcher nested inside another if
is read — rather than declining every gated condition. The alternative is
defensible and would decline more; it would also make elif chains almost
entirely unreadable to the tool, which seemed worse than a polarity that means
what it means everywhere else. This is the judgment call I would most like a
second opinion on.

A one-line elif is still not read at all. if A; then x; elif B; then y; fi on a single line gives scan one logical line starting with if, so only
A becomes the subject and B is never seen as an assertion. Pre-existing,
adjacent to logbook#81's splitter problem, and I did not touch it — it needs
the same ;-at-depth-0 splitter that issue describes. Not filed separately
because I think it is the same fix as #81.

What's deliberately not done

  • logbook#89, filed tonight. alone is tracked for both branches and
    read only for the else, so a then branch whose only way of failing is a
    second matcher still reports needs-no-match — if grep -q 'A' f; then grep -q 'C' f; fi exits 1 only when A matches and C misses. No elif
    involved, a different line of code, and in the dangerous direction. Pinned as
    current behaviour in
    TestElifConditions::test_a_then_branch_whose_only_failure_is_a_matcher_is_over_decided
    so it is written down rather than folklore, and so the fix has to come past a
    test.
  • The shipped examples/ are untouched. This change adds no finding kind,
    so the only effect would have been churn in three expected-*.txt files.
  • logbook#83 (the README's org-wide counts going stale). The counts are
    unaffected by this change — 0 of 31 repos moved — so they are as accurate as
    they were this morning, and re-measuring them is its own job.

Two stale backlog items, found while picking tonight's work

Both verified by running the issue's own repro, not by reading:

  • logbook#73 (multi-line || { ... exit 1 } read as decorative) was
    fixed by 19066ea on 11-17, two days after it was filed, and never closed.
    The byte-for-byte repro from the issue reports the false positive on
    dd3ce00 and is clean on 19066ea and on main.
  • logbook#74 (regexdrift compares 10 engines, 5 have no test naming
    them) was closed out by regexdrift#3 — tests/test_dialects.py cites the
    issue in its module docstring and now names all ten engines.

Leaving both open rather than closing them from a night shift, as the house
convention has it, but they should go: between them they are two of the four
newest-looking things in the backlog, which is how work gets picked.

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

`_branch_polarity` is called with the `elif`'s own index, and `depth` is
counted from the `if`/`fi` tokens on each line. In `elif grep -q 'B' f` the
`if` is preceded by `el` rather than a separator, so the depth regex never
matched it, `depth` stayed 0, and `if depth <= 0: break` ended the walk on
its first line -- after the condition had been stripped out as the subject.
The function then fell through to its last line and returned `decorative`:
a finding kind, reached over no branches at all, saying "neither branch can
fail the step" about a branch holding `exit 1`.

Three changes, all logbook#82:

* `depth` is seeded to 1 when the subject is an `elif`, because an `elif` is
  already inside its enclosing block. Its branch is its own `then`, and the
  walk runs to the matching `fi`.
* The subject line may not flip the branch to `else`. Seeded, it otherwise
  matches the `^(?:else|elif)` test on its own first line and credits its
  `then` branch to an `else` that has not started.
* A later `elif` condition holding a matcher gates everything the else-path
  does, so a failure reached past one is no longer counted as a failure "no
  matcher gets a say in". `if A; then :; elif B; then exit 1; fi` exits 1
  only when `A` misses *and* `B` matches, which is not what `needs-match`
  claims about `A` -- logbook#78 with the second matcher one line further
  away, and the half of this with a live polarity.

And an invariant: `decorative` is not returned by a walk that never got
inside the block it is about. Such a walk has established nothing, and
saying "neither branch can fail" is exactly the claim it cannot make. It is
unreachable through `scan` now that the seeding is right, and kept because
it is what would have made this bug announce itself in `not decided:`
instead of answering wrongly -- logbook#77's lesson one function up.

`entered` is asked per token rather than per line. A one-line `if ... fi`
opens and closes its block before the line ends, so a depth read after the
line says the walk was never inside it -- which would have turned a real
`decorative` into a decline on a shape the org writes.

Polarity stays "what this condition does to the step when it is reached",
the convention the module already uses for a matcher nested in another `if`.
Every expectation was taken from running each shape under `bash -e` for
every combination of which patterns are present, never from reading the code.

Output is byte-identical across all 31 repositories in the organisation,
exit codes included: 0 of 37 workflow files have a matcher in an `elif`.
`TestElifConditions` holds eight shapes; the suite had no `elif` in it at
all before this. Each expectation is the exit status the shape really
produces under `bash -e`, taken for every combination of which patterns are
present in the file and recorded in the class docstring, because a hand
model of what the shell does here would be a second opinion competing with
the thing being described.

Two of them are controls rather than witnesses, and they are the reason the
class is this size:

* `decorative` must stay reachable through an `elif`. A fix that simply
  declined every `elif` condition passes the two tests about the false
  positive and quietly loses the finding this tool exists to report.
* A nested `if`/`fi` inside the body must not end the walk, which is what
  keeps the seeded depth a real depth counter rather than a flag.

`TestAVerdictOverNoBranches` covers the invariant. The sentence is tested by
calling `_branch_polarity` directly, which is the only honest way to show it
can print: `scan` cannot reach it now that an `elif` seeds the depth and an
`if` is matched by the depth regex on its own line. Its second test is the
guard's own near-miss -- the one-line `if ... fi` whose block opens and
closes on one line -- which `TestOneLineIf` caught before this file did.

One shape is pinned as current behaviour rather than fixed: a `then` branch
whose only way of failing is a second matcher still reports `needs-no-match`,
because `alone` is tracked for both branches and read only for the `else`.
That is logbook#89, filed tonight, not an `elif` problem, and in the
dangerous direction -- so it is written down here instead of left as
folklore, and fixing it has to come past this test.
Follows the convention of the three steps around it: a fixture built with
`printf` rather than a heredoc, so the fixture's own matchers stay arguments
and do not land in this repository's assertion count.

`negated` is what pins the fix. It counts `needs-no-match`, which is what an
`elif` condition whose `then` branch exits actually is; on `main` the walk
broke on the `elif` line, the condition came out `decorative`, and the
number was 0. Three of the step's six assertions fail on `main` and pass
here -- one for each half of the fix -- and the other three are controls
that must pass on both, including the second fixture whose `elif` really is
decorative.

The status is captured rather than inherited. The control shapes are real
findings, so the run exits 1 by design and `set -e` would have ended the
step before any assertion ran -- which is how the first version of this step
"passed".

README: the either-or bullet described the `else` spelling only, and now
also holds for an `elif`. The org-wide counts quoted further down are
untouched and still accurate; nothing in this change moves them
(logbook#83 is about them going stale, separately).

@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 as a stranger, fresh venv: README's pip install/pytest/ruff verbatim — 223 passed (213 on main), lint and format clean.

Reproduced the bug on main byte-for-byte with the PR's own fixture: deadassert /tmp/elifs reports ci.yml:8 decorative 'B' over a guard whose then holds exit 1. Checked out this branch, same fixture: 'B' now reads unwitnessed, 'A' moves to not decided: with the either-or sentence — matches the PR body exactly.

Read _branch_polarity's diff directly rather than trusting the description: depth seeded to 1 for an elif subject, entered tracked per-token (not per-line, so the one-line if...fi case stays correctly decorative), the subject line excluded from flipping branch to else via offset, and gated correctly scoped to which == "else" so it only suppresses the else-path's alone tracking, not the subject's own then. Matches the mutation table in the PR (ran a few of these by hand, not all five).

Off-README: empty workflow dir, missing path, and malformed YAML all fail usefully (exit 2, no tracebacks). The one-line elif gap is real and pre-existing (confirmed: my own repro of it silently finds nothing), and it's honestly disclosed as out of scope and shared with logbook#81.

All 8 CI checks green, whole three-commit diff clean of secrets, no new deps, MIT LICENSE unchanged.

Not tagging — same call as #5: no org repo has a matcher in an elif (0/31, confirmed in the PR), so this has no live installable-surface impact yet, just correctness for a construct nobody's hit.

The one open judgment call (polarity of a gated elif meaning "what it does when reached" rather than declining every gated condition) is reasonable and consistent with how nested if is already read elsewhere in this module — agreed, would also go with the direction that doesn't make elif chains unreadable to the tool.

@jennifer-barber
jennifer-barber Bot merged commit e2936c3 into main Nov 25, 2026
8 checks passed
@jennifer-barber
jennifer-barber Bot deleted the shift/2026-11-25-elif-branch-walk branch November 25, 2026 18:38
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