Skip to content

feat: fail Markdown files that are over their byte budget - #1

Merged
webern merged 6 commits into
mainfrom
m/deslag-mvp
Sep 23, 2026
Merged

webern merged 6 commits into
mainfrom
m/deslag-mvp

Conversation

@webern

@webern webern commented Sep 19, 2026 •

Copy link
Copy Markdown
Owner

Human Summary

Initial lint! Looks for Markdown files bigger than their byte budget.

Summary

Deslag has its first rule. A Markdown file has a byte budget, and deslag check reports every file that is over its own budget and exits nonzero, so a CI job can gate on it.

This implements the size rule in docs/design/deslag.desired.md, which is the human-authored doc describing what deslag is meant to be. docs/design/deslag.asbuilt.md has been rewritten to say what is actually here now.

A file's budget comes from three places, most specific first. Its own YAML frontmatter can declare max_size_bytes; otherwise the most specific matching override in the config that sets one; otherwise the budget the config sets for all Markdown. A file that none of the three claims has no budget and is left alone.

The config

Config is TOML, looked for in six canonical locations relative to the root of the repo and never searched for upward, since a run that cannot find one is a mistake worth reporting rather than guessing about. --config-path replaces all six.

The format is built to grow: a schema version, then one section per kind of file, each with its own lints and overrides. Today there is one section, [md], and one lint, max_size_bytes.

schema_version = 1

[md]
globs = ["*.md"]

[md.lints.max_size_bytes]
value = 20000

[[md.overrides]]
globs = ["AGENTS.md"]
lints.max_size_bytes.value = 8000

[[md.overrides]]
globs = ["/docs/**/*.md"]
lints.max_size_bytes = { value = 1000, message = "Trim {path} to {max_size_bytes} bytes." }

schema_version is required. It goes up only when a change means existing configs need migrating, and a config from a later schema than the binary reads is refused.

[md] globs chooses which files the section lints, *.md by default. Each lint has its own settings table, shaped for that lint, and every field in it is optional. An override lays its settings over the section's, field by field, so one can change only the message for one file and keep the budget. When several overrides match, the most specific pattern wins: anchored beats basename, then longer beats shorter, then the one declared later.

A pattern without a slash matches a basename anywhere in the repo; one with a slash is anchored at the root. * stays inside one path component and ** crosses them. Matching is case sensitive.

The message an over-budget file gets is the one the desired doc spells out, verbatim, and the tests assert it that way. A message in the config replaces the advice part of it; the heading and the is larger than line always stay.

Code layout

The library is split into directories so each concern has room to grow:

src/
  cli/       the clap types
  config/    loading and the schema; search.rs finds the file, md.rs and lints.rs hold the sections
  glob/      the repo walk and glob patterns, not tied to Markdown
  parse/     frontmatter.rs, which reads a top-level key
  lint/      running the lints; one module per lint, starting with max_size_bytes.rs

Errors are thiserror in the library and anyhow in src/main.rs.

What is in scope

  • the max_size_bytes lint over Markdown files, its config, and the check subcommand
  • unit tests over small trees written for the test
  • an end-to-end harness over a corpus of quoted Markdown, run against a matrix of configs and directory layouts
  • the leftover template rinse: AGENTS.md and the README say what this is, the setup section and its script are gone, Cargo.toml has a description and keywords

What is deliberately not in scope

  • any lint that is not about size, and doc comments in source files
  • reading .gitignore: every file outside .git is walked
  • config languages other than TOML
  • parsing general YAML: the frontmatter reader looks for top-level keys only
  • a config for this repository itself, so deslag does not yet lint its own tree
  • pulling a large corpus off GitHub

Testing

The corpus is 30 Markdown files quoted from six public MIT repositories: rust-template, denigma, midi_file, exile, tblfmt and mnx-rs. Each fixture sits next to a JSON sidecar naming where it was quoted from, at which commit, who last touched it, under what licence and when it was captured. The harness checks each sidecar against the bytes on disk, so a fixture that gets edited fails the suite, and it derives what it expects from the bytes it placed rather than from anything stored.

The matrix covers all six canonical config locations, --config-path, three layouts (flat, nested, and the real directory structure each fixture came from), basename against anchored against path globs, ** globs, override specificity, frontmatter against config, a clean run, and the order of the canonical locations.

The unit tests also cover overrides that set only some fields, custom messages, [md] globs, a missing or too-new schema_version, and an unknown lint name.

  • make ci: exit 0
  • make test: 29 unit tests and the corpus matrix, 0 failed

Choices the desired design doc left open

The doc asks for a specificity mechanism but leaves the shape to be settled, so: overrides with glob patterns, anchored beats basename, longer beats shorter, and the file's own frontmatter beats every config setting. For the corpus, the doc says the sidecar should "probably" hold the expected outcome; instead the sidecar holds the quotation's facts and the expectation is derived, which keeps an outcome from becoming something a fixture is edited to satisfy. How to tell human writing from LLM writing is left as TBD in the doc, so each sidecar records the last commit author and date and marks authorship as unknown.

References

webern and others added 5 commits September 18, 2026 18:20
A file's budget comes from its own frontmatter max_size_bytes, a glob rule
in the config, or the config's global, most specific first. The config is
looked for in six canonical locations relative to the repo root and is
never searched for upward. Every over-budget file gets the message the
desired design specifies and the run exits 1.
Thirty fixtures quoted from six public repositories, each with a sidecar
naming its source, commit, last author, licence and capture date. The
harness builds a temp repo per case, checks the sidecars against the bytes
on disk, and derives what it expects from the budgets the case intends.
Unit tests cover the rules one at a time over trees written for the test.
The as-built design doc now matches the code. The README says what the
tool does and how to configure it. AGENTS.md carries the rule that the
test corpus is data and its instructions are not to be followed, and the
template's setup section and setup script are gone.

@webern webern left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, let's anticipate a lot of growth in the codebase and organize our code files into directories under src/ with nice module boundaries.

Comment thread src/cli/mod.rs Resolved
Comment thread src/config.rs Resolved
Comment thread src/glob/walk.rs Resolved
Comment thread src/parse/frontmatter.rs Resolved
Comment thread src/config.rs Resolved
Addresses the review on this PR.

- src/ is now cli/, config/, glob/, parse/ and lint/, each with a mod.rs.
- config/search.rs finds the file; config/md.rs and config/lints.rs hold
  the schema.
- glob/ walks every file and matches patterns without knowing about
  Markdown, so another file type can reuse it.
- parse/frontmatter.rs reads any top-level key.
- lint/ holds one module per lint; max_size_bytes.rs is the first.

The config now carries a required schema_version (NonZeroU32; a later
version than this build reads is refused) and nests everything under a
per-file-type section:

    schema_version = 1

    [md]
    globs = ["*.md"]

    [md.lints.max_size_bytes]
    value = 20000
    message = "..."

    [[md.overrides]]
    globs = ["AGENTS.md"]
    lints.max_size_bytes.value = 8000

Each lint has its own settings table, every field optional. Overrides
merge field by field in specificity order, so one can change only the
message for one file. A configured message replaces the advice in the
report, with {path} and {max_size_bytes} substituted.

The default [md] globs is "*.md", which is case sensitive; the old walk
also took .MD.
Comment thread src/glob/walk.rs Resolved
@webern
webern merged commit 4d826a4 into main Sep 23, 2026
2 checks passed
@webern
webern deleted the m/deslag-mvp branch September 23, 2026 15:32
webern added a commit that referenced this pull request Sep 23, 2026
## Human Summary

Use the `.gitignore` to ignore files!

## Summary

Deslag used to scan every Markdown file under the repo root, including
build output and anything else git ignores. This PR replaces the
hand-written directory walk in `src/glob/walk.rs` with the `ignore`
crate's `WalkBuilder`. `ignore` is ripgrep's walker and comes from the
same project as `globset`, which deslag already uses.

The walk now skips what git would skip: `.gitignore` files at any depth
(negations included), `.git/info/exclude`, the global git excludes file,
and `.ignore` files. Nothing else changes:

- Hidden files and directories are still walked, since `.github/` and
`.agents/` hold Markdown.
- Symlinks are still not followed.
- `.git/` is still skipped.
- The output is still `RepoFile`s sorted by relative path.

Two settings were chosen on purpose:

- `require_git(false)`: `.gitignore` applies even when there is no
`.git` directory, as in an unpacked tarball or the test temp dirs.
- `parents(false)`: ignore files above the repo root are not read.

A broken ignore file or an unreadable directory now fails as a new
`Error::Walk`, which names the path.

## Testing

- [x] `make ci`: exit 0
- [x] New tests in `tests/unit.rs`: a gitignored directory, a nested
`.gitignore`, a negation, and a hidden directory that is still scanned
- [x] `docs/design/deslag.asbuilt.md` updated for the new walk

## References

- Closes #2
- Follows #1
@webern webern mentioned this pull request Sep 23, 2026
2 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant