A Validator Needs to Know What It Is Judging
One pre-commit check made old Markdown debt everybody's problem. Another mistook a shell command for documentation. Both needed a sharper boundary, in opposite directions.
Two pre-commit fixes landed within minutes of each other on November 18. One stopped rejecting so much. The other stopped accepting so much.
The first was a Markdown check that required language tags on opening backtick fences. The second guarded against recursive grep commands that could search their own growing output logs. Neither rule was the problem. The problem was deciding which text the rule applied to.
Old debt became a shared commit blocker
The Markdown check ran in strict mode over the files supplied by the hook. An opening fence without a language tag failed it, even if that line had been sitting in the file long before the current edit.
That is a defensible policy for a clean repository. Ours wasn’t clean. The repair’s initial audit counted 2,117 violating files among 73,546 tracked Markdown files. In a shared checkout, hook batches could make one session’s touched legacy file block other sessions’ commits too.
The incident crossed seven units in eleven minutes. Commit attempts consumed failure budgets without reducing the underlying debt. A formatting rule had become a coordination failure.
I could have made the check advisory, excluded journals, or cleaned up every old file. The first two would weaken the rule for new work. The third would turn an unrelated commit into a repository-wide cleanup.
Instead, the local gate now judges newly added lines of the staged diff.
| Edit | Previous strict gate | Changed-line gate |
|---|---|---|
| Fix a sentence below an old untagged opening fence | Reject | Accept |
| Add a new untagged opening fence | Reject | Reject |
Add a new opening fence tagged python |
Accept | Accept |
The implementation still parses the whole file to track whether a fence opens or closes a block. Only the reporting is filtered. Looking at diff lines alone would lose the state that gives those lines meaning: an untagged closing fence is perfectly normal.
There are limits worth stating. The validator reads working-tree contents, not staged blobs, so the line mapping assumes those versions match. A fence deletion can change how later lines are interpreted without adding a reportable line. With staged changes elsewhere, an unstaged new file has no staged lines to inspect; stage it before relying on this mode.
The local pass therefore does not certify that the whole file is clean. With a clean index, the CLI falls back to whole-file validation. Our CI uses that behavior in a separate changed-file step, not its general all-files hook run. That step is conditional: it uses selected paths from the PR diff, or from the last commit on scheduled/manual runs. It does not run on every push or cover every Markdown path. When selected, a file’s legacy debt can fail CI even though the local changed-line check allowed it. That is stricter coverage within that selection, not a full-repository audit.
For a deliberate whole-file check, omit the changed-lines option:
python3 scripts/precommit/validators/validate_markdown_codeblock_syntax.py \
--strict path/to/document.md
Quotation is not an execution boundary
The grep check had the opposite mistake. It exempted quoted grep text, allowing documentation to describe commands the check would otherwise reject. But a quoted string can also be a shell program.
| Text in a script | Meaning | Current exemption |
|---|---|---|
echo "grep -r pattern ." |
Print an example | Retained |
bash -c "grep -r pattern ." |
Execute the example | Removed |
sh -c 'grep -r pattern .' |
Execute the example | Removed |
Before the fix, the latter two passed through the documentation exemption. The outer quotes were treated as evidence that the inner command was inert. They actually supplied the program to the shell.
The repair keeps the quoted-text exception but refuses that exception when
the same line contains recognized execution contexts: several shells with
-c, os.system(...), or subprocess. calls. The regression tests check
both halves: shell-executed examples lose the exemption, while echoed
examples and docstrings retain it.
This remains a line-based heuristic. It does not trace a string assigned
on one line and executed on another. It does not prove that every
subprocess call invokes a shell, either. Other safe-pattern exceptions
still apply. Even the quote recognition is approximate: a quote earlier on
the same line can satisfy its pattern without actually enclosing the grep.
Combined shell flags such as bash -lc are not recognized by the -c
pattern. Passing this check is not a general shell-safety guarantee.
A proper parser or data-flow analysis could express a stronger boundary. For this repair, the narrower change closed the demonstrated exemption without pretending to solve arbitrary program execution.
The two questions I now ask
For the Markdown check: does this commit introduce the violation, or did it merely touch the file containing it?
For the grep check: is this string being displayed, or handed to something that can execute it?
Those questions move the gates in opposite directions. One shrinks the set of reportable violations; the other shrinks an exemption. Both make the verdict better match the claim the check can actually support.
That distinction matters when automation consumes the verdict. A passing changed-line check means no reportable new violation was found under its scope. A passing grep heuristic means its patterns did not flag the text. Neither means the document or program has been proved correct.
The useful check is the one whose boundary we can explain—and whose limitations survive being summarized as a green tick.