| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | # The flags are one string so the formatter app and this check cannot |
| 5 | # drift; split it back into argv. |
| 6 | read -ra fmtArgs <<< "$proseFmtArgs" |
| 7 | cd "$tree" || exit 1 |
| 8 | find . -name '*.md' -print0 | xargs -0 --no-run-if-empty \ |
| 9 | prettier "${fmtArgs[@]}" --check |
| 10 | |
| 11 | python3 - <<'PY' |
| 12 | import glob |
| 13 | import os |
| 14 | import re |
| 15 | import sys |
| 16 | |
| 17 | LINK = re.compile(r"\]\(\s*([^)\s]+)(?:\s+\"[^\"]*\")?\s*\)") |
| 18 | INLINE_CODE = re.compile(r"(`+)[^`\n]*?\1") |
| 19 | FENCE_OPEN = re.compile(r"^\s*(`{3,}|~{3,})") |
| 20 | HEADING = re.compile(r"^#{1,6}\s+(.*?)\s*#*?\s*$") |
| 21 | MD_LINK_IN_HEADING = re.compile(r"\[([^\]]*)\]\([^)]*\)") |
| 22 | NOT_IN_SLUG = re.compile(r"[^\w\s-]") |
| 23 | EXTERNAL = re.compile(r"^[a-zA-Z][a-zA-Z0-9+.-]*:") |
| 24 | |
| 25 | |
| 26 | def outside_fences(lines): |
| 27 | fence = None |
| 28 | for number, line in enumerate(lines, start=1): |
| 29 | if fence is None: |
| 30 | m = FENCE_OPEN.match(line) |
| 31 | if m: |
| 32 | fence = m.group(1) |
| 33 | else: |
| 34 | yield number, line |
| 35 | else: |
| 36 | if re.match(rf"^\s*{fence[0]}{{{len(fence)},}}\s*$", line): |
| 37 | fence = None |
| 38 | |
| 39 | |
| 40 | def anchors(path, cache): |
| 41 | if path in cache: |
| 42 | return cache[path] |
| 43 | found, counts = set(), {} |
| 44 | with open(path, encoding="utf-8") as handle: |
| 45 | for _, line in outside_fences(handle.read().splitlines()): |
| 46 | heading = HEADING.match(line) |
| 47 | if not heading: |
| 48 | continue |
| 49 | title = MD_LINK_IN_HEADING.sub(r"\1", heading.group(1)) |
| 50 | title = title.replace("`", "").lower() |
| 51 | slug = NOT_IN_SLUG.sub("", title).strip().replace(" ", "-") |
| 52 | counts[slug] = counts.get(slug, 0) + 1 |
| 53 | found.add(slug if counts[slug] == 1 else f"{slug}-{counts[slug] - 1}") |
| 54 | cache[path] = found |
| 55 | return found |
| 56 | |
| 57 | |
| 58 | findings = [] |
| 59 | cache = {} |
| 60 | documents = [p for p in ["README.md", "AGENTS.md"] if os.path.exists(p)] + sorted( |
| 61 | glob.glob("docs/*.md") |
| 62 | ) |
| 63 | |
| 64 | for path in documents: |
| 65 | with open(path, encoding="utf-8") as handle: |
| 66 | lines = handle.read().splitlines() |
| 67 | for number, line in outside_fences(lines): |
| 68 | prose = INLINE_CODE.sub(" ", line) |
| 69 | for target in LINK.findall(prose): |
| 70 | if EXTERNAL.match(target): |
| 71 | continue |
| 72 | relative, has_hash, anchor = target.partition("#") |
| 73 | if not relative: |
| 74 | resolved = path |
| 75 | else: |
| 76 | resolved = os.path.normpath( |
| 77 | os.path.join(os.path.dirname(path), relative) |
| 78 | ) |
| 79 | if resolved == ".." or resolved.startswith("../"): |
| 80 | findings.append( |
| 81 | f"{path}:{number}: {target} points outside the repository" |
| 82 | ) |
| 83 | continue |
| 84 | if not os.path.exists(resolved): |
| 85 | findings.append(f"{path}:{number}: {target} does not exist") |
| 86 | continue |
| 87 | if has_hash: |
| 88 | if not anchor: |
| 89 | findings.append( |
| 90 | f"{path}:{number}: {target} has an empty heading anchor" |
| 91 | ) |
| 92 | elif not os.path.isfile(resolved) or not resolved.endswith(".md"): |
| 93 | findings.append( |
| 94 | f"{path}:{number}: {target} names a heading in non-markdown target {resolved}" |
| 95 | ) |
| 96 | elif anchor not in anchors(resolved, cache): |
| 97 | findings.append( |
| 98 | f"{path}:{number}: {target} names no heading in {resolved}" |
| 99 | ) |
| 100 | |
| 101 | if findings: |
| 102 | print( |
| 103 | f"prose-format: {len(findings)} broken link(s) or anchor(s):", |
| 104 | file=sys.stderr, |
| 105 | ) |
| 106 | for item in findings: |
| 107 | print(f" {item}", file=sys.stderr) |
| 108 | sys.exit(1) |
| 109 | PY |
| 110 | |
| 111 | # The style lint: the rules of AGENTS.md's style section that a pattern can |
| 112 | # catch. It prints one finding per rule broken on a line, as |
| 113 | # file:line: rule: what it found, and exits 1 if it printed any. A line |
| 114 | # allows one rule with the marker <!-- prose-allow: rule --> outside inline |
| 115 | # code, and a marker that allows nothing is itself a finding. |
| 116 | cat > "$TMPDIR/style.awk" <<'AWK' |
| 117 | BEGIN { |
| 118 | split("line-number narration vocabulary", names, " ") |
| 119 | for (i in names) rules[names[i]] |
| 120 | marker = "<!--[[:space:]]*prose-allow:[^>]*-->" |
| 121 | # A source path is one with a directory, or a file name with an |
| 122 | # extension of a source file in this tree. A host and port or a |
| 123 | # timestamp is neither. |
| 124 | seg = "[[:alnum:]_.-]+" |
| 125 | path = seg "(/" seg ")+|" seg "\\.(go|sh|bash|nix|cue|py|md|txt|json|ya?ml|toml)" |
| 126 | cite = "(^|[^[:alnum:]_./-])(" path "):[0-9]+(-[0-9]+)?([^0-9]|$)" |
| 127 | } |
| 128 | |
| 129 | function finding(rule, message) { |
| 130 | if (rule in allowed) { |
| 131 | used[rule] |
| 132 | return |
| 133 | } |
| 134 | printf "%s:%d: %s: %s\n", FILENAME, FNR, rule, message |
| 135 | failed = 1 |
| 136 | } |
| 137 | |
| 138 | # The start of the first match of re in s that ends after its first skip |
| 139 | # characters, which are the end of the line before, or 0. RLENGTH is the |
| 140 | # match's length. |
| 141 | function find(s, re, skip, start) { |
| 142 | for (start = 0; match(substr(s, start + 1), re); start += RSTART) |
| 143 | if (start + RSTART + RLENGTH - 1 > skip) |
| 144 | return start + RSTART |
| 145 | return 0 |
| 146 | } |
| 147 | |
| 148 | # A word rule, matched case-insensitively when fold is set. |
| 149 | function word(rule, re, why, fold, at) { |
| 150 | if (at = find(fold ? low : s, re, skip)) |
| 151 | finding(rule, "\"" substr(s, at, RLENGTH) "\": " why) |
| 152 | } |
| 153 | |
| 154 | FNR == 1 { fence = ""; tail = "" } |
| 155 | |
| 156 | { |
| 157 | delete allowed |
| 158 | delete used |
| 159 | code = $0 |
| 160 | gsub(/`[^`]*`/, "", code) |
| 161 | while (match(code, marker)) { |
| 162 | rule = substr(code, RSTART, RLENGTH) |
| 163 | code = substr(code, RSTART + RLENGTH) |
| 164 | sub(/^<!--[[:space:]]*prose-allow:[[:space:]]*/, "", rule) |
| 165 | sub(/[[:space:]]*-->$/, "", rule) |
| 166 | if (rule in rules) |
| 167 | allowed[rule] |
| 168 | else |
| 169 | finding("allowance", "prose-allow names no rule \"" rule "\"; the rules are line-number, narration and vocabulary") |
| 170 | } |
| 171 | if (match($0, "^[[:space:]]*(([-*+>]|[0-9]+[.)])[[:space:]]+)?" marker)) |
| 172 | finding("allowance", "a marker that begins a line begins an HTML block; put it after a word") |
| 173 | text = $0 |
| 174 | gsub(marker, " ", text) |
| 175 | |
| 176 | infence = fence != "" |
| 177 | if (!infence && match($0, /^[[:space:]]*(```+|~~~+)/)) { |
| 178 | fence = substr($0, RSTART, RLENGTH) |
| 179 | sub(/^[[:space:]]*/, "", fence) |
| 180 | infence = 1 |
| 181 | } else if (infence && $0 ~ ("^[[:space:]]*" substr(fence, 1, 1) "{" length(fence) ",}[[:space:]]*$")) |
| 182 | fence = "" |
| 183 | |
| 184 | if (match(text, cite)) { |
| 185 | found = substr(text, RSTART, RLENGTH) |
| 186 | sub(/^[^[:alnum:]_.\/-]/, "", found) |
| 187 | sub(/[^0-9]$/, "", found) |
| 188 | finding("line-number", "\"" found "\": cite the file or a name in it, not a line") |
| 189 | } |
| 190 | |
| 191 | # Fenced code is exempt from the word rules. A phrase may wrap, so each |
| 192 | # line is read after the last two words of the line before it. |
| 193 | if (!infence) { |
| 194 | s = text |
| 195 | sub(/^[[:space:]]+/, "", s) |
| 196 | s = tail " " s |
| 197 | skip = length(tail) + 1 |
| 198 | low = tolower(s) |
| 199 | word("vocabulary", "\\<ow(e|es|ed)\\>", "a change requires checks", 1) |
| 200 | word("vocabulary", "\\<pull requests?\\>", "a valley change is a change", 1) |
| 201 | word("vocabulary", "\\<PRs?\\>", "a valley change is a change", 0) |
| 202 | word("vocabulary", "\\<knowledge[ -]?bases?\\>", "a plan item is a knot in the plan graph", 1) |
| 203 | word("vocabulary", "\\<tickets?\\>", "a plan item is a knot in the plan graph", 1) |
| 204 | word("narration", "\\<(no longer|previously|was changed to|has been replaced|is now)\\>", "describe what HEAD does today", 1) |
| 205 | word("narration", "\\<((it|this|that|these|those|they|we|which|who) used to|used to be)\\>", "describe what HEAD does today", 1) |
| 206 | } |
| 207 | tail = "" |
| 208 | if (!infence && match(text, /([^[:space:]]+[[:space:]]+)?[^[:space:]]+[[:space:]]*$/)) { |
| 209 | tail = substr(text, RSTART, RLENGTH) |
| 210 | sub(/[[:space:]]+$/, "", tail) |
| 211 | } |
| 212 | |
| 213 | for (i = 1; i in names; i++) |
| 214 | if (names[i] in allowed && !(names[i] in used)) |
| 215 | finding("allowance", "allows " names[i] ", but nothing on this line breaks it") |
| 216 | } |
| 217 | |
| 218 | END { exit failed } |
| 219 | AWK |
| 220 | style() { gawk -f "$TMPDIR/style.awk" "$@"; } |
| 221 | |
| 222 | # The lint's own fixtures. A doc that breaks each rule fails with exactly |
| 223 | # these findings, and the same doc with an allowance on each line passes. |
| 224 | fixtures="$TMPDIR/style" |
| 225 | mkdir -p "$fixtures" |
| 226 | cd "$fixtures" || exit 1 |
| 227 | cat > broken.md <<'EOF' |
| 228 | A change never owes a check, and nothing is owed. |
| 229 | Open a PR, or a Pull Request, against main. |
| 230 | Track it in the knowledgebase as a ticket. |
| 231 | The rule is in status/ready.go:146-165. |
| 232 | The verb is at `bin/valley:57`. |
| 233 | See [the hook](../valleyhook/policy.go:12) for the rest. |
| 234 | The hook no longer reads the tree. |
| 235 | It previously read it, and it is now a script. |
| 236 | The verb was changed to print JSON. |
| 237 | The flag has been replaced. |
| 238 | The check used to be slow, and it used to time out. |
| 239 | - A phrase can wrap: the hook is no |
| 240 | longer a script. |
| 241 | |
| 242 | ```sh |
| 243 | valley checks # see status/ready.go:146 |
| 244 | ``` |
| 245 | EOF |
| 246 | cat > broken.want <<'EOF' |
| 247 | broken.md:1: vocabulary: "owes": a change requires checks |
| 248 | broken.md:2: vocabulary: "Pull Request": a valley change is a change |
| 249 | broken.md:2: vocabulary: "PR": a valley change is a change |
| 250 | broken.md:3: vocabulary: "knowledgebase": a plan item is a knot in the plan graph |
| 251 | broken.md:3: vocabulary: "ticket": a plan item is a knot in the plan graph |
| 252 | broken.md:4: line-number: "status/ready.go:146-165": cite the file or a name in it, not a line |
| 253 | broken.md:5: line-number: "bin/valley:57": cite the file or a name in it, not a line |
| 254 | broken.md:6: line-number: "../valleyhook/policy.go:12": cite the file or a name in it, not a line |
| 255 | broken.md:7: narration: "no longer": describe what HEAD does today |
| 256 | broken.md:8: narration: "previously": describe what HEAD does today |
| 257 | broken.md:9: narration: "was changed to": describe what HEAD does today |
| 258 | broken.md:10: narration: "has been replaced": describe what HEAD does today |
| 259 | broken.md:11: narration: "used to be": describe what HEAD does today |
| 260 | broken.md:13: narration: "no longer": describe what HEAD does today |
| 261 | broken.md:16: line-number: "status/ready.go:146": cite the file or a name in it, not a line |
| 262 | EOF |
| 263 | if style broken.md > broken.got; then |
| 264 | echo "prose-format: the style lint passed a doc that breaks every rule" >&2 |
| 265 | exit 1 |
| 266 | fi |
| 267 | diff -u broken.want broken.got >&2 || { |
| 268 | echo "prose-format: the style lint's findings on broken.md are not the expected ones" >&2 |
| 269 | exit 1 |
| 270 | } |
| 271 | # allowed.md is broken.md with a marker for each rule a line breaks. |
| 272 | gawk -F': ' ' |
| 273 | NR == FNR { |
| 274 | split($1, at, ":") |
| 275 | if (!((at[2], $2) in seen)) |
| 276 | allow[at[2]] = allow[at[2]] " <!-- prose-allow: " $2 " -->" |
| 277 | seen[at[2], $2] |
| 278 | next |
| 279 | } |
| 280 | { print $0 allow[FNR] }' broken.want broken.md > allowed.md |
| 281 | style allowed.md >&2 || { |
| 282 | echo "prose-format: the style lint failed broken.md with an allowance on each line" >&2 |
| 283 | exit 1 |
| 284 | } |
| 285 | |
| 286 | # What the rules must pass: the words and the narration in a fenced code |
| 287 | # block, a marker quoted in inline code, an address, a timestamp, a hash, |
| 288 | # and "used to" that is not narration. |
| 289 | cat > clean.md <<'EOF' |
| 290 | Allow a word with `<!-- prose-allow: vocabulary -->` on its line. |
| 291 | The bus listens on nats://127.0.0.1:4222, and the run began at 2026-10-14T21:30. |
| 292 | A version is git-sha1:2222222222222222222222222222222222222222. |
| 293 | The verifier keys used to verify a landing are the integrator's. |
| 294 | |
| 295 | ~~~ |
| 296 | A PR owes a ticket, and the hook no longer reads the tree. |
| 297 | ~~~ |
| 298 | EOF |
| 299 | style clean.md >&2 || { |
| 300 | echo "prose-format: the style lint failed clean.md, which breaks no rule" >&2 |
| 301 | exit 1 |
| 302 | } |
| 303 | |
| 304 | # A marker that allows a rule the line does not break, names no rule, or |
| 305 | # begins a line is a finding of its own. |
| 306 | cat > markers.md <<'EOF' |
| 307 | A change requires checks. <!-- prose-allow: vocabulary --> |
| 308 | A change has a version. <!-- prose-allow: words --> |
| 309 | <!-- prose-allow: vocabulary --> A PR is a change. |
| 310 | EOF |
| 311 | cat > markers.want <<'EOF' |
| 312 | markers.md:1: allowance: allows vocabulary, but nothing on this line breaks it |
| 313 | markers.md:2: allowance: prose-allow names no rule "words"; the rules are line-number, narration and vocabulary |
| 314 | markers.md:3: allowance: a marker that begins a line begins an HTML block; put it after a word |
| 315 | EOF |
| 316 | if style markers.md > markers.got; then |
| 317 | echo "prose-format: the style lint passed a marker that allows nothing" >&2 |
| 318 | exit 1 |
| 319 | fi |
| 320 | diff -u markers.want markers.got >&2 || { |
| 321 | echo "prose-format: the style lint's findings on markers.md are not the expected ones" >&2 |
| 322 | exit 1 |
| 323 | } |
| 324 | |
| 325 | # The tree's own docs: the same documents the link check reads. |
| 326 | cd "$tree" || exit 1 |
| 327 | style README.md AGENTS.md docs/*.md > "$TMPDIR/style.got" || { |
| 328 | echo "prose-format: $(wc -l < "$TMPDIR/style.got") style finding(s):" >&2 |
| 329 | sed 's/^/ /' "$TMPDIR/style.got" >&2 |
| 330 | exit 1 |
| 331 | } |
| 332 | |
| 333 | touch "$out" |