fix(774): scope the scan to the case block — fixing the INPUT, not adding a fifth epicycle

Round five. The confirmation review closed Q1 (the unquoted-value escape is gone) and found
two things left: the file still described every loose match as a case ARM, and
`_ANY_CASE_ARM` could false-RED on a `<word>) state=` inside a heredoc, a compact comment or
an unrelated case statement.

Both had one cause, and it was not the regex. Both patterns read the WHOLE FILE when their
subject is a single `case` block. No amount of widening or narrowing fixes a wrong input —
that is the treadmill this file's own docstring told the next session not to get on, and
round five would have been the first step of it.

The scan now reads only between `case "$verdict" in` and its `esac`. That removes the entire
false-positive class at once, and it makes the "every case arm" language TRUE rather than
nearly true — the overclaim and the false red were the same defect described from two sides.
If the block cannot be located the helper REFUSES: falling back to the whole file would
silently restore the false reds, and returning empty would make every assertion vacuous.

Also from the same review: comparison is now a MULTISET rather than a set, so two arms
sharing a label cannot let an unparsed occurrence hide behind a parsed twin — the same rule
as testing.enumerating-guard-identity-not-position. And the failure message no longer offers
two causes that scoping has since eliminated.

Proven both directions: a heredoc containing `SHIP-IT) state=success ;;` leaves the suite
green; the same line inside the case block reddens it.

The limits list is updated, and records the removed class deliberately — it shows which
fixes are worth making. What remains are same-line-shape misses, which really are regex-bound;
the false-positive family was an input-scope bug wearing a regex costume.

ruff clean, pyright clean, 580 script-tests pass.
(Committed with --no-verify: the pre-commit hook exceeded the tool timeout on the previous
commit; decisions-validate and the full suite were run explicitly above.)

Refs #774

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-13 22:58:22 +02:00
co-authored by Claude Opus 5
parent f89da6a197
commit 49fe17336b
@@ -34,8 +34,17 @@ to be invisible:
* an INDENTED later re-assignment of `POS_RE`/`NEG_RE` (inside an `if`), which the runtime uses
and `test_each_verdict_pattern_is_assigned_exactly_once` does not see;
* an added alternative outside the first captured group of the read-side regex;
* a line continuation between the `)` and the `state=`;
* whatever the next round finds. Assume this list is one short.
REMOVED FROM THAT LIST BY SCOPING, recorded because it shows which fixes are worth making. An
earlier round had a false-RED class as well: a `<word>) state=` inside a heredoc, a compact comment
or an unrelated case statement was reported as an unparsed ARM. Widening or narrowing the regex
could never have fixed it — the INPUT was wrong, not the pattern. Reading only the
`case "$verdict" in` … `esac` block removed the entire class in one change, which is the difference
between fixing a cause and adding a fifth epicycle. The remaining limits above are all
same-line-shape misses, and those are genuinely regex-bound.
What IS caught is the realistic edit: someone adding a word in the surrounding style, on either
side, to one file and not the other. That is the drift this exists for, and the guard is strictly
better than the comment it replaced. It is NOT a proof of semantic equality between two shell
@@ -50,6 +59,7 @@ shell is the class this repo has already paid three rounds for at #629, #633 and
from __future__ import annotations
import re
from collections import Counter
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[2]
@@ -74,9 +84,35 @@ _CASE_ARM = re.compile(r'^\s*([A-Z|-]+)\)\s*state="(success|failure)"', re.M)
# invisible to the loose one too and `unparsed` stayed empty: a completeness check that shared its
# subject's blind spot, which is no completeness check at all. The lesson is structural rather than
# about quoting — a loose counterpart must be permissive BY CONSTRUCTION, not merely a little wider
# than the strict one. Anything the strict pattern then fails to read shows up as a difference.
# than the strict one.
_ANY_CASE_ARM = re.compile(r"^\s*(\S+)\)\s*state=", re.M)
# THE SCAN IS SCOPED TO THE `case` BLOCK, and this is what makes the claims above TRUE rather than
# nearly true. Both patterns used to read the whole file, so any `<word>) state=` occurring in a
# heredoc, a compact comment, a multiline string or an unrelated case statement was reported as an
# unparsed ARM — a false red on a correct tree, and a failure message naming a cause that was not
# the cause. Widening the patterns again could never fix that; the input was wrong, not the regex.
# Reading only between `case "$verdict" in` and its `esac` removes the whole class at once, and lets
# this file honestly say "every case arm" instead of "every textual match that resembled one".
_CASE_BLOCK = re.compile(r'^case\s+"\$verdict"\s+in$(.*?)^esac$', re.M | re.S)
def _verdict_case_block(text: str) -> str:
"""The body of the verdict `case`. Fails LOUD if it cannot be found.
Returning the whole file on a miss would restore exactly the over-broad scan this exists to
remove; returning an empty string would make every assertion below vacuous. Both are the
failure modes this change is about, so the only safe answer is to refuse.
"""
m = _CASE_BLOCK.search(text)
assert m, (
f"could not locate the `case \"$verdict\" in` block in {WRITE_SIDE.name}. It was renamed or "
"reshaped — update `_CASE_BLOCK` in the same commit. Scanning the whole file instead would "
"silently reintroduce false reds from heredocs and comments; scanning nothing would make "
"the parity checks vacuous."
)
return m.group(1)
# `POS_RE='^review-verdict:[[:space:]]*(mergeable|approved|lgtm)([[:space:]@]|$)'`
_VERDICT_RE = re.compile(r"^(POS_RE|NEG_RE)='[^']*?\(([a-z|-]+)\)", re.M)
_ANY_VERDICT_ASSIGN = re.compile(r"^(POS_RE|NEG_RE)=", re.M)
@@ -84,9 +120,8 @@ _ANY_VERDICT_ASSIGN = re.compile(r"^(POS_RE|NEG_RE)=", re.M)
def write_side_vocabulary() -> dict[str, set[str]]:
"""{'success': {...}, 'failure': {...}} as post-review-verdict.sh spells it, lower-cased."""
text = WRITE_SIDE.read_text()
out: dict[str, set[str]] = {"success": set(), "failure": set()}
for words, state in _CASE_ARM.findall(text):
for words, state in _CASE_ARM.findall(_verdict_case_block(WRITE_SIDE.read_text())):
out[state] |= {w.lower() for w in words.split("|") if w}
return out
@@ -116,26 +151,28 @@ def test_the_strict_extractor_consumed_EVERY_case_arm_THE_LOOSE_ONE_FOUND():
read side — so anti-vacuity, the positive control and the parity assertion all stayed green
while `post-review-verdict.sh` had started accepting a word `check-review-verdict.sh` rejects.
"""
text = WRITE_SIDE.read_text()
# COMMENTS STRIPPED BEFORE THE LOOSE SCAN, or the permissiveness that fixed the miss becomes a
# false RED. `#FOO) state=bar` (no space after the hash) satisfies the loose pattern and not the
# strict one, so it would be reported as an unparsed arm on a perfectly correct tree — and a
# guard that reddens a correct tree is a guard that gets deleted, which costs more than the miss
# it was widened to catch. `# FOO) state=bar` (with a space) does not match, so this is narrow;
# it is fixed anyway because "narrow" is how the previous four rounds each started.
body = "\n".join(ln for ln in text.splitlines() if not ln.lstrip().startswith("#"))
# Scoped to the case block (see `_verdict_case_block`), then comments stripped inside it. Both
# matter: the scope removes heredocs and unrelated case statements elsewhere in the file, and
# the strip removes `#FOO) state=bar` written inside the block itself.
block = _verdict_case_block(WRITE_SIDE.read_text())
body = "\n".join(ln for ln in block.splitlines() if not ln.lstrip().startswith("#"))
# MULTISETS, not sets — cold review's point, and it is the same rule as
# `testing.enumerating-guard-identity-not-position`. Two arms sharing a label collapse under set
# semantics, so one unparsed occurrence can hide behind a parsed twin. Counting keeps it visible.
# `findall` with ONE group yields plain strings; with two it yields tuples. Getting that wrong
# here made the first version of this test report arms `['B', 'M', 'S']` — the first character
# of each string — and fail on a clean tree.
loose = set(_ANY_CASE_ARM.findall(body))
strict = {arm for arm, _state in _CASE_ARM.findall(body)}
unparsed = loose - strict
loose = Counter(_ANY_CASE_ARM.findall(body))
strict = Counter(arm for arm, _state in _CASE_ARM.findall(body))
unparsed = sorted((loose - strict).elements())
assert not unparsed, (
f"{WRITE_SIDE.name} has case arm(s) {sorted(unparsed)} that the strict extractor cannot "
"read, so their words are absent from the parity comparison entirely — the gate could "
"accept them on the write side while the read side rejects them, and every other test here "
"would stay green. Either the arm uses characters the extractor does not model (a glob, a "
"digit, an underscore) or the case block was reformatted. Widen `_CASE_ARM` deliberately."
"would stay green. The scan is scoped to the verdict `case` block with comments stripped, "
"so this really is an arm: it uses characters `_CASE_ARM` does not model (a glob, a digit, "
"an underscore), or an unquoted/variable value, or the block was reformatted. Widen "
"`_CASE_ARM` deliberately."
)