rotguard is a command-line tool for people who keep a markdown site. It checks the site's
outbound links for rot (dead links, redirects and soft-404s), reports each broken link with a
proposed fix, and with --apply writes the safe fixes back into the files. Start reading at
run and check_pages in src/lib.rs, then Verdict and check_all in src/check.rs.
Context: rotguard
┌──────────────┐
│ Site owner │
└──────┬───────┘
rotguard [site_dir] │ ▲ report on stdout, progress on stderr,
[--apply] [--strict] │ │ exit code 0 / 1 / 2
[--quiet] [--format] │ │
┌──────▼─┴─────────────┐ reads rotguard.toml and ┌──────────────────────┐
│ rotguard ├───────────────────────────►│ Site directory │
│ [Rust, one binary] │ content; --apply replaces │ rotguard.toml, │
│ │ files via a temp file │ content/**/*.md │
└──────┬──────────┬────┘ └──────────────────────┘
GET each external │ │ snapshot lookups (dead and suspect links only)
URL (HTTPS) ▼ ▼
┌──────────────────┐ ┌──────────────────────────┐
│ Linked websites │ │ Wayback Machine CDX API │
└──────────────────┘ └──────────────────────────┘
Maintainer ──git push vX.Y.Z──► release.yml ──publishes──► GitHub Releases ──► action.yml
- Linked websites: a timeout, TLS error, failed DNS lookup, 429 or 5xx makes a link
UNVERIFIABLE, never dead, after 2 retries (a 429 once, honouringRetry-Afterup to 30 s). A 401, 403 or 999 isBLOCKEDwhen a made-up URL beside it gets the same status, elseUNVERIFIABLE. Only a 404, 410 or a DNS answer that the name doesn't exist, on 2 tries, makes a linkDEAD, and a 404 or 410 only when the host's root doesn't answer 404 or 410 too. - Wayback Machine: 3 attempts per query. When it fails, has no capture or runs past the
deadline, the finding says
manual fix: archive unreachable,no archived copyordeadline, and nothing is written. - GitHub Actions:
action.ymlruns the binary on the site's runner, usually weekly, and the site's workflow (docs/github-actions.md) opens a pull request with the fixes. A failed build or a cache miss never changes what rotguard reports. - GitHub Releases:
release.ymlpublishes static Linux binaries andSHA256SUMSon avX.Y.Ztag, and the action downloads them. A published release is immutable.
Components: one crate, src/
main.rs ──Args──► lib.rs: run, check_pages, exit_code
│ 1 load │ 2 extract │ 3 check_all │ 4 lookup
▼ ▼ ▼ ▼
config.rs extract.rs check.rs archive.rs
Config Page, Link, Ctx Fetcher, Verdict CDX gate
│ 5 findings with Fix::Proposed
▼
apply.rs ──temp file + rename──► content files (the only writer)
│ 6 findings, Summary
▼
report.rs ──text/JSON/HTML/markdown──► stdout
progress.rs ──progress lines──► stderr
lib.rs owns the run. run(Args) -> ExitCode loads the config, reads the content files,
calls check_pages and decides the exit code in exit_code. It also owns the Finding, Fix
and ManualReason types every other module passes around. main.rs only parses arguments.
check.rs owns the network and the verdicts. Fetcher sends GET requests politely: 1 in
flight per host, spaced starts, at most 8 hosts at once, redirects followed by hand up to 10.
classify turns a fetch into a Verdict (Ok, Permanent, Temporary, Dead, Suspect,
Unverifiable, Blocked); a Permanent fix points at the URL before the first noise step of
the leading 301/308 run. check_all checks every distinct URL with retries, re-checks a first
Dead, runs the soft-404 and bot-wall probes and stops at the deadline. It never writes.
apply.rs is the only module that writes files, a rule a fitness check in .whittle/gates.toml
enforces. apply writes fixes only for Permanent and Dead findings with a proposed fix, and
only into markdown link destinations and quoted TOML front-matter strings. It never applies a
fix for a suspect, temporary, unverifiable or blocked link.
extract.rs owns link discovery. extract(path, text, cfg) -> Page returns each link with its
line, its byte span, the syntax it was written in (Ctx) and whether it is verbatim
(written so --apply can replace the span), plus the page date from front matter. It covers
markdown links, reference definitions, autolinks, raw-HTML href/src, bare URLs, and TOML
and YAML front matter.
config.rs loads rotguard.toml into Config and HostConfig and rejects unknown keys.
archive.rs finds the Wayback capture closest to a page date, with 1 request in flight at a
time. report.rs renders findings and the summary in 4 formats and has no say in the exit
code. progress.rs writes progress to stderr only, so stdout stays a clean report.
Outside src/, action.yml is a composite GitHub Action with 6 steps:
- Fetch: when the ref is a release tag (
v0,v0.1.0) on Linux X64 or ARM64, download the release binary, check its sha256 and install it where the build would put it, and skip steps 2, 3, 4 and 6. Any other ref or platform setsmode=buildand those steps run. - Pick a cache key from the OS, the stable
rustcversion andCargo.lock. - Restore the build cache.
- Build rotguard.
- Run
--apply --format markdownon the folder in thepathinput (default., the workspace root), always exiting 0 with the real code in theexit-codeoutput. - Save the cache on a miss.
.github/workflows/release.yml runs on a vX.Y.Z tag in 3 jobs: verify the tag against
Cargo.toml and CHANGELOG.md, build both musl targets, then publish the release and move the
v<major> tag (scripts/release-verify.sh, scripts/release-publish.sh).
Check (every run):
- Load the config; read every file matching
content, sorted by path; extract links. A config, glob or read error exits 2, as does a missing system CA bundle (reported before any request). - Drop internal links (host of
site_url) andignorematches; strip fragments; dedupe. check_allthe distinct URLs untildeadline_s. A URL still running then isUNVERIFIABLE(deadline).- Look up a snapshot for each dead or suspect link at its page's date (the run date when the page has none). Dead links go first, sorted by URL then date, then suspects, so a deadline cuts off suspect lookups before dead ones.
- Build 1 finding per occurrence: a permanent redirect proposes the URL before its first noise
step (without the
utm_*pairs the redirect added), dead and suspect propose the snapshot, temporary, unverifiable and blocked propose nothing. - With
--apply, apply the fixes. Print the report; exit 1 if a dead, permanent or suspect link is left unfixed (with--strict, also temporary, unverifiable and blocked), else 0. Blocked links are listed only with--strict; the summary always counts them.
Soft-404 and bot-wall probes: a 2xx answer is suspect when a non-root link ends at the site
root or its permanent fix would point there (a middle hop through / doesn't count), when its
title matches a not-found phrase, or when a probe URL with a random last segment under the same
parent path returns the same page. A link ending in / is probed as a sibling folder
(/a/b/ probes /a/<token>/); other paths probe /a/<token>. Probes are shared per origin,
parent path and trailing slash. A 404 or 410 probe skips the title phrase check; a failed probe
doesn't. The same probe decides bot walls: a final 401, 403 or 999 is Blocked when the probe
gets that status too. The root control (below) also serves the 404 check: a link that stays 404
or 410 after the recheck is Unverifiable when the root of its last hop's origin answers 404 or
410 too, because a wall answers every path alike. A root that fails or answers anything else
leaves the link Dead. A host with soft404 = false skips the soft-404 checks but keeps the
bot-wall probe and this 404 check.
For a 2xx link whose probe also answered 2xx, 3 comparisons run in order:
- The probe ends at the link's final URL. If the link wasn't redirected, the probe says nothing
(the title phrase check still runs). If it was, the link is
Suspect(probe: same final URL). - The titles match, the bodies are under 0.5 alike and both have at least 20 shingles: the probe says nothing, because the 2 pages differ.
- Otherwise a title match or a body at least 0.9 alike makes the link a candidate, and checks 1
to 4 decide it:
- A not-found title phrase makes it
Suspect, and beats the 2 results below. - The link's visible text asks for JavaScript (
enable javascript,javascript is required,requires javascript,javascript is disabled):Unverifiable(probe: page needs JavaScript). - rotguard GETs the host's root once per origin, the root control. If the root answers 2xx
and matches the probe on every signal the link matched (same title, body at least 0.5
alike), the host gives every URL the same page:
Unverifiable(probe: every URL gets the same page). - Otherwise the link is
Suspect(probe: same titleorprobe: similar body).
- A not-found title phrase makes it
Permanent fixes: a step of a 301/308 run is noise when it keeps the origin and every query
pair of the URL before it, and then adds a query pair, adds 1 locale-shaped path segment (en,
en-gb, pt-BR, zh-Hans, es-419) or changes nothing but a trailing slash while adding a
pair. A run whose first step is noise leaves the link Ok, as written. Otherwise the fix is the
URL before the first noise step, so /old to /new to /new?language=en_US is rewritten to
/new. A fix that would be the site root for a link that isn't is SUSPECT redirect to root.
Last, check_one drops the utm_* pairs the redirect added, keeping any the link had, and
sends 1 GET for the stripped URL, with no retry and no probe. The strip stands only when that
URL classifies Ok and its <title> matches the page the link ended on; any other result keeps
the link Ok, unchanged.
Apply (--apply, after the check): per file, re-read it and confirm each finding's span
still holds its original URL, then write <file>.rotguard.tmp and rename it over the file. A
fix that can't be written safely becomes manual fix: <reason>; a file that changed during the
run is left untouched (changed during run). An I/O error leaves that file as it was, reports
its fixes as manual fix: write failed and exits 2.
Weekly fix PR (docs/github-actions.md): the site's workflow runs the action. On exit 0
or 1 it opens or updates a PR whose body is the markdown report; on 1 or anything higher the job
fails afterwards. On exit 2 or higher no PR opens, because --apply may have written some files
and not others. On a tag ref (@v0) the action downloads the release binary instead of
building it; a failed download fails the job.
The binary keeps nothing between runs: no history and no link cache. It reads rotguard.toml
and the content files, and with --apply replaces content files through a temp file in the
same directory. Its output is stdout, stderr and the exit code.
A release holds 3 assets: rotguard-<X.Y.Z>-x86_64-unknown-linux-musl,
rotguard-<X.Y.Z>-aarch64-unknown-linux-musl and SHA256SUMS.
The action writes the report to $RUNNER_TEMP/rotguard-report.md and the job summary, and keeps
the compiled build in the site repo's Actions cache under
rotguard-<os>-<rustc>-<Cargo.lock hash>. A run from a release tag uses no cache.
- Writing the user's files. A stale span would corrupt text the user edited mid-run, and a
crash mid-write would leave half a file.
applyre-reads each file and checks every span still holds its URL, applies edits last span first, refuses overlapping spans, creates the temp file withcreate_new(so a planted symlink is never followed), syncs it and renames it. The unit tests insrc/apply.rscover each case. - Spans and
verbatimin extract. A wrong span orverbatimflag makes apply write into the wrong bytes. TOML strings, YAML, CRLF, a byte-order mark and escaped or entity-encoded destinations are where it goes wrong. YAML, HTML and escaped links are never verbatim; tests insrc/extract.rsandtests/acceptance/extract.rspin each form. - Politeness and the deadline.
Fetchertakes the host gate before the global permit, so one slow host can't starve the others, and every wait is bounded by the deadline, after whichrundrops the runtime without waiting for hung requests. Tests insrc/check.rsandsrc/archive.rscheck spacing, 1 request per host and hanging servers. - The action's ref detection. The fetch step reads the ref as everything after
_actions/<owner>/<repo>/in$GITHUB_ACTION_PATH. A wrong reading picks the wrong mode: a branch gets a release download that 404s, or a tag builds slowly. A non-matching ref or a platform without a binary builds from source, so a miss is slower, never wrong. A branch named exactly like a tag counts as a tag.tests/acceptance/fetch.rspins the cases. - Soft-404 judgement. It decides between
Ok,Suspect,UnverifiableandBlockedfrom a shared probe cache, so a change to a probe key or to the root-redirect rule shifts verdicts across links. The root control and the JavaScript check only downgrade aSuspecttoUnverifiable, and the root control also downgrades aDead, never the other way, so a wrong guess costs a hand check, not a missed rotten link; but a host that really serves 1 page for everything, or whose root 404s, is reported as unverifiable too.tests/acceptance/soft404.rs,blocked.rs,directory_probe.rs,root_redirect.rsandforeign_sites.rshold the agreed cases. - The noise rule. A locale segment can follow the machine that runs the check
(
/en-gb/from a UK laptop,/en-us/from the GitHub runner), so writing it into a file breaks the link for other readers. But a rule that is too loose keeps a real move as written. So a step is noise only when the origin and every query pair stay and the path gains 1 locale-shaped segment or a pair is added; any other segment, a changed id or a lost pair is a real step. A wrong call keeps a link as written or stops a fix 1 step early, and writes nothing that wasn't reached.tests/acceptance/redirect_noise.rsholds the agreed cases.
- Report by default;
--applywrites only fixes that can't be wrong: permanent-redirect targets that carry no noise and snapshots for dead links, never suspects or unverifiable links (docs/design/link-health.md, D1, D6;docs/design/redirect-noise.md, D1, D3). --applyrewrites only markdown link destinations and TOML front-matter strings; YAML, raw HTML and bare URLs are reported asmanual fix: not verbatim(docs/design/simplify.md, D1, D3).- A dead link's fix is the snapshot closest to the page's date (
docs/design/link-health.md, D2). - Soft-404s are detected by probing, not by a list of known sites
(
docs/design/link-health.md, D4). robots.txtis not consulted (docs/design/link-health.md, D9).- Progress goes to stderr, so stdout stays a clean report for scripts
(
docs/design/progress-and-config.md, D1). - One
--formatflag picks text, JSON, HTML or markdown; JSON keys are stable and always present (docs/design/report-format.md, D1, D3). - A 401, 403 or 999 is a bot wall only when a same-folder probe gets the same status, and blocked
links are listed only with
--strict(docs/design/bot-walls.md, D1, D2). - A 404 or 410 is dead only when the host's root doesn't answer 404 or 410 too, and a root that
fails or answers anything else leaves the link dead (
docs/design/404-walls.md, D1, D2). - A DNS failure is dead only when the answer says the name doesn't exist; any other lookup
failure is unverifiable, so it never leads to a write (
docs/design/dns-hiccup.md, D1). - A fix
--applycouldn't write is a hand fix, marked in apply so every report format says so (docs/design/apply-failed-fix.md, D1). - A link ending in
/is probed with a sibling that ends in/too, so the probe takes the same route through the site's server as the link (docs/design/directory-probe.md, D1). - A redirect to the site root counts only where the link ends up or where its fix would point,
so a chain that goes on to a real page keeps its own soft-404 checks
(
docs/design/root-redirect.md, D1). - The weekly run lives in CI, not in the binary: a composite action runs rotguard with no secret,
and only exit 0 or 1 opens a PR (
docs/design/weekly-fix-pr.md, D1, D6). On non-tag refs (@main, a branch, a SHA) the action builds rotguard from its own source. - The action restores and saves its build cache in separate steps, the save inside the action, so
a week whose job fails on a hand fix still saves (
docs/design/build-cache.md, D3). - A tag ref downloads the release binary and any other ref builds from source, so an action
change can run on a real site before it is released
(
docs/design/release-binary.md, D1). - The action reads the ref from its folder path, not from
github.action_ref, which holds the caller's ref inside a nested composite action (docs/design/release-binary.md, D2). - The release workflow publishes only after every check and moves
v0last; a published tag is never rewritten (docs/design/release-binary.md, D4). - A host that gives every URL the same page, and a page that needs JavaScript, are unverifiable,
not suspect, and dead links get their snapshot lookups first
(
docs/design/foreign-sites.md, D1, D2). - The PR body is the markdown report, rendered by rotguard; the log and the job summary show the
same report (
docs/design/readable-pr-body.md, D1, D4).
- Markdown sources only: no crawling of built HTML and no internal-link or anchor checks.
- No memory between runs, so a host that is down on the day of a run shows up that week.
- A site that answers 429 to every request shows up as unverifiable links.
--applynever edits YAML front matter, raw HTML or bare URLs; those stay hand fixes.