Catch a countless head/tail in the truncation guard (rebased #24) - #27
Open
chaosisnotrandomitisrhythmic wants to merge 1 commit into
Open
chaosisnotrandomitisrhythmic wants to merge 1 commit into
chaosisnotrandomitisrhythmic wants to merge 1 commit into
Conversation
_TRUNC requires an explicit line count, so a bare `head` or `tail` at the end of a pipe never matches. Both keep 10 lines by default, the same cut as the `-10` form the guard already rejects. The rule names the two commands it is about and then misses them whenever the count is left off. The regex now has two alternatives: an explicit count under 20, or no count at all. The countless branch is a lookahead anchored to a real command boundary, so counts of 20 and above, byte counts, follow mode, and words that merely start with those four letters are all unaffected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reopens #24, rebased onto current
mainafter the test rewrite in 328369a. The earlier PR touchedtest_bash_guard, which no longer exists, so it could not have merged as written. This version adds one small focused test instead._TRUNCrequires an explicit line count, so a bareheadortailat the end of a pipe never matches:The
\s+and the digit class are both mandatory. Both commands keep 10 lines with no count, which is the same cut as the-10form the guard already rejects, so the rule names the two commands it is about and then misses them whenever the count is left off.tar tzf x.tgz | head -10tar tzf x.tgz | headls ~ | tailThe fix
Two alternatives instead of one: an explicit count under 20, or no count at all.
The countless branch is a lookahead anchored to a real command boundary: end of string, or before
|,;,&or a newline. That keepshead -30,head -c 200,tail -fandls | headerout of it.Evidence it matters
Over eight days of one developer's sessions this guard fired 200 times on truncating pipes. In the same corpus a rejected
| head -1was retried moments later as| sed -n 1pand passed, which is the same shape of hole: the rule matches a notation rather than the behaviour. This PR closes thehead/tailhalf, where the bypass is the guard's own two commands.sed -n '1,10p'andawk 'NR<=10'truncate identically and remain allowed; neither is what the message tells you to fix, so they feel like a separate question.Tests
test_bash_guard_bare_head_tail, seven assertions, all failing before the change. It re-importsbash_guard_msg, which the test rewrite dropped.Note on running the suite
uv run pytestcannot resolve dependencies on this checkout, before and after the change:pyproject.tomldeclaresrequires-python = ">=3.10"whilellmdojoneeds>=3.11. I verifiedbash_guard_msgdirectly instead, which needs only the standard library. Happy to send therequires-pythonbump as its own PR if it is wanted.🤖 Generated with Claude Code