Skip to content

feat: exit with 2 when deslag cannot run - #39

Merged
webern merged 2 commits into
mainfrom
m/exit-codes
Sep 26, 2026
Merged

webern merged 2 commits into
mainfrom
m/exit-codes

Conversation

@webern

@webern webern commented Sep 26, 2026 •

Copy link
Copy Markdown
Owner

Human Summary

Use exit 2 to signal that lints may not have been able to run, and reserve exit 1 for a successful run with lints that require attention.

Summary

deslag check exited 1 both when a file failed a lint and when deslag could not run at all: no config, a config it cannot parse, an invalid setting, a bad glob. A CI job or an agent reading the result could not tell "fix your Markdown" from "fix deslag's setup" without parsing the text.

deslag now has three exit codes, for every subcommand:

  • 0: the run finished and nothing failed.
  • 1: the run finished and a file failed a lint.
  • 2: deslag could not do what it was asked. This is any Err out of run() in src/main.rs, set once in main(), so a new subcommand gets it without any code of its own. It matches clap, which already exits 2 on bad arguments.

The case runner in tests/cases.rs needed to know which code a case expects. A case can now have a <case>.exit file beside its .stderr file, holding the code. The four cases with an invalid setting hold 2. A case without the file keeps the old rule: an empty .stderr means exit 0, any other means exit 1. The code is written down rather than guessed from the text because make fix-test-output rewrites the text but never a .exit file, so a change that turns a lint failure into an error fails the test instead of being accepted along with the new output. The doc comment at the top of tests/cases.rs explains this.

every_config_deslag_accepts_fits_the_schema in tests/instructions.rs guessed which cases have a rejected config by checking whether the .stderr starts with deslag:. It now skips cases with a .exit file instead.

The tests that expected 1 from an error now expect 2. Every other code(&output), 1 is a lint failure and stays 1.

The guide that deslag instructions prints, the README and docs/design/deslag.asbuilt.md now say what each code means. The as-built doc is 23 bytes smaller than on main: I stated the codes once, in the command line section, and cut two lists that would go stale (the check targets and the crate's include entries).

Testing

  • make ci: exit 0
  • Removed a .exit file, wrote a bad one, and put one beside a clean case: the runner failed each with a message saying what to do, and make fix-test-output wrote nothing.
  • By hand: a missing config, a bad argument and a bad instructions topic exit 2; --help exits 0.

References

deslag exited 1 both when a file failed a lint and when it could not
run at all, such as on a bad config. A CI job or an agent could not
tell "fix your Markdown" from "fix deslag's setup" without reading
the text.

Now any error out of run() in src/main.rs exits 2, whatever the
subcommand, which matches clap's exit code for bad arguments. A file
that fails a lint still exits 1, and a clean run exits 0.

The case runner in tests/cases.rs now reads the expected code from a
<case>.exit file when one sits beside the case, and the cases with an
invalid setting have one holding 2. Without the file, the old rule
applies. make fix-test-output never writes it, so a change that turns
a lint failure into an error fails the test instead of being accepted
with the new text.

The tests that expected 1 from an error now expect 2. The guide, the
README and the as-built doc say what each code means.
@webern webern added enhancement New feature or request area/cli Deals with the command line interface. labels Sep 26, 2026
@webern webern mentioned this pull request Sep 26, 2026
9 tasks done
@webern webern added the ai Primarily authored by AI (whether PR, Issue, Whatever) label Sep 26, 2026
@webern
webern merged commit e3cb25d into main Sep 26, 2026
2 checks passed
@webern
webern deleted the m/exit-codes branch September 26, 2026 13:48
webern added a commit that referenced this pull request Sep 26, 2026
## Human Summary

Seems like a pretty good feature that could go a long way toward solving
pet peeves.

## Summary

Adds `banned_phrases`, a lint that fails a Markdown file whose prose
holds a phrase the config lists. `ban` maps each phrase to the advice
its report shows, and `allow` exempts a longer phrase that holds a
banned one. deslag ships no phrases: with nothing in `ban`, the lint
checks nothing. `.agents/deslag.toml` is unchanged.

This config and report are the `broken` case in
`tests/cases/banned_phrases/`:

```toml
[md.lints.banned_phrases.ban]
"it's worth noting" = ""
"delve into" = "say what you found instead"
"in today's fast-paced world" = ""
"leverage" = "write `use`"
```

```
ERROR: deslag detected banned phrases!

README.md has 6 banned phrases.

This repo keeps these phrases out of its Markdown. Do what the advice with each one says, or reword the sentence; it often reads better with the phrase simply gone.

Do not hide a phrase in a code span or HTML, and do not change the config to get past this check. Only a human can tell you to do that, and I am a linter, not a human.

The phrases, and what to do about each:
  line 1: "Delve Into"; say what you found instead
  line 3: "In today's fast-paced world"; delete it
  line 3: "It's worth noting"; delete it
  line 4: "it's worth noting"; delete it
  line 6: "leverage"; write `use`
  line 11: "Leverage"; write `use`

deslag: 1 of 1 Markdown files with banned phrases.
```

The second match on line 3 runs onto line 4, and the one on line 4 is
written `**it's worth** noting`.

### How it matches

There are no regular expressions. A new `Token::split` in
`src/document/tokens.rs` splits a phrase with the same code that splits
a stretch of prose, so the config and the document agree on what a word
is. The phrase then matches the same tokens in one block, compared by
`Token::folded`: lower case, with a curly apostrophe made straight.
Whitespace, line breaks and formatting between tokens don't count. A
code span, HTML, an image, a URL or a footnote reference is a token no
phrase can hold, so it breaks a match. Phrases are indexed by their
first token, so each token of a file costs one lookup.

### Choices the issue left open

- **A phrase is plain text, not Markdown.** `Token::split` runs the
tokenizer on the raw string instead of parsing it with
`Document::markdown`. So `#` or `*` in a phrase stays a token instead of
turning into a heading or emphasis, and the phrase is compared with what
the file renders: `AT&amp;T rocks` in a file matches `AT&T rocks` in the
config.
- **Symbols match like words and marks.** The issue names word, number
and punctuation tokens. Without symbols (`+`, `>`, emoji), `C++ rules`
would pass validation and never match.
- **"No words" means no word and no number**, so `110%` is a valid
phrase.
- **Overlap is leftmost, then longest.** Scanning left to right, the
first match wins, and of matches that start at one token, the longest.
With `worth noting` and `noting that` banned, "Worth noting that"
reports `Worth noting` only.
- **`allow` is applied before overlap is settled.** A match inside an
allowed match is discarded first, so an allowed phrase hides only what
lies inside it. With `worth noting` and `noting that` banned and `well
worth noting` allowed, "well worth noting that" reports `noting that`.
- **A phrase in both `ban` and `allow` is an error within one table
only**, because `invalid()` runs per table. That lets an override allow
a phrase the section bans, and after the merge `allow` wins; the `allow`
case pins this. The check compares folded tokens, so `It's worth noting`
and `it's worth NOTING` count as one phrase.
- **An override replaces `ban` and `allow` whole**, as with
`banned_chars`.
- **The quote** is each matched token as written, with a space where
whitespace parts two of them and the markup between them left out.
Quoting the raw byte range gave `"it's** worth noting"`, and `"It's
worth > noting"` inside a block quote.
- **The advice is printed as written**, and an empty one prints `delete
it`. It is advice, not a replacement, so the report does not put it in a
`write ...` the way `banned_chars` does.
- **The guide's example config** turns the lint on with an empty table,
because its test requires every lint. The bullet tells the agent to ask
the human which phrases to ban.

### Corpus numbers

These are 22 phrases widely cited as LLM tells, run through the library
over `tests/corpus/{human,llm,mixed}` by a throwaway test that is not
committed:

`delve into`, `delves into`, `it's worth noting`, `it is worth noting`,
`it's important to note`, `in today's fast-paced world`, `in the realm
of`, `a testament to`, `tapestry`, `navigate the complexities`, `plays a
crucial role`, `seamlessly`, `harness the power of`, `unlock the power
of`, `game-changer`, `cutting-edge`, `ever-evolving`, `in conclusion`,
`embark on`, `let's dive in`, `deep dive`, `whether you're`

| label | files | flagged | rate | matches |
|---|---|---|---|---|
| human | 400 | 1 | 0.25% | 1 |
| llm | 400 | 5 | 1.25% | 7 |
| mixed | 400 | 13 | 3.25% | 20 |

The list barely fires on any label, so it does not separate them in any
useful way. Only 7 of the 22 phrases matched at all, mostly `seamlessly`
(7 files) and `whether you're` (5 files, one of them human). 19 flagged
files out of 1,200 is too few to tell the labels apart. The chatbot
phrases people cite are rare in the Markdown that agents write in
repositories.

For comparison, 12 single words that are also often cited
(`comprehensive`, `robust`, `seamless`, `leverage`, `crucial`,
`essential`, `ensure`, `enhance`, `streamline`, `utilize`, `powerful`,
`effortlessly`) flag human 55 (13.8%), llm 67 (16.8%) and mixed 142
(35.5%). They fire far more but hardly separate human from llm: `ensure`
alone flags 33 human files and 34 llm ones. `comprehensive` (4 / 14 / 25
files) and `leverage` (2 / 9 / 14) lean llm. Under both lists mixed
files are flagged most, and I did not correct for file length. None of
this makes a case for shipping a default list.

### Also in this PR

Fixes found while checking the README, in three commits of their own:

- **The README holds only what stays true.** It listed the lints,
spelled out each one's rules and quoted corpus counts, so it went stale
with every lint PR; one count was already wrong (the density defaults
fail 92 of the 400 agent-written files, not 84). It now says what deslag
is, how to install, set up and run it, and the config's general shape,
and points to `deslag instructions config-schema`, `deslag
instructions`, `deslag explain` and the as-built doc for the details.
This is also why the PR adds no README section for `banned_phrases`.
- **Install.** `cargo install deslag` fails: the crate is not on
crates.io. The README installs from a checkout, and the setup guide
tells the agent to ask the human how CI should install deslag instead of
writing `cargo install deslag --locked`. `deslag --help` and the crate
description said deslag only checks the byte budget; they now describe
the linter.
- **`deslag explain` on a file check skips.** `check` never reads a
git-ignored file or one inside `.git`, but `explain` printed its
settings as if `check` used them. It now prints `# ignored: yes, so
deslag check never reads it` and stops, as it does for a file `[md]`
does not select.
- **`every_lint_has_a_failing_case`** counted any case with a non-empty
`.stderr` file, so a case where deslag cannot run (exit 2) passed for a
case where the lint fails a file. It now counts only cases that exit 1.

## Testing

- [x] `make ci`: exit 0
- [x] `tests/phrases.rs`: case, curly apostrophes, soft and hard line
breaks, formatting, code spans, HTML, images, URLs, footnotes, two
blocks, blocks that are not read, `allow`, overlap, merging, each
validation error, and `Token::split` agreeing with `Document::markdown`
on sample text
- [x] `tests/cases/banned_phrases/`: `clean`, `broken`, `allow` (with an
override that allows a phrase the section bans), `custom_message`, and
`ban_and_allow` (an invalid config)
- [x] `deslag instructions config-schema` shows `BannedPhrases`
- [x] A throwaway corpus probe, not committed: every run of three prose
tokens in the corpus's prose blocks (319,722 of them), joined into a
phrase, split back into the same tokens and was found by the lint. No
tokenizer bug turned up.
- [x] `tests/explain.rs`: a file under a git-ignored directory, and one
inside `.git`, each say `# ignored: yes` and nothing more; without the
fix they print full settings
- [x] `tests/cases.rs`: a case with a `.exit` file holding 2 does not
count as failing a file; with the old rule it did
- [x] The README's example config, run with `deslag check` and `deslag
explain` in a scratch repo, and `cargo install --locked --path .` into a
scratch directory
- [x] `docs/design/deslag.asbuilt.md` is 16,379 bytes of its 16,384,
from 16,363 on `main`

## Notes

- The as-built doc's intro and its config paragraph now give examples
instead of listing every lint and every invalid setting, so a new lint
doesn't have to extend them.
- Rebased after #37 to #40 merged. `ban_and_allow` is a config error, so
it has a `.exit` file holding `2`, as #39 asks of such cases.
`BannedPhrases` derives `Serialize` like the other lints, so `deslag
explain` (#40) shows it, and explain's pinned output gains a `#
banned_phrases: off` line.

## References

- Closes #32
- Related to #34, which ruled out regular expressions over raw text;
this matches tokens
- Related to #39 and #40
- Related to #16, #18 (the README)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai Primarily authored by AI (whether PR, Issue, Whatever) area/cli Deals with the command line interface. enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exit with 2 when deslag cannot run

1 participant