Skip to content

docs: measure whether the documented examples compile - #540

Merged
godofecht merged 2 commits into
mainfrom
docs/verify-example-blocks
Aug 18, 2026
Merged

godofecht merged 2 commits into
mainfrom
docs/verify-example-blocks

Conversation

@godofecht

@godofecht godofecht commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

First of the wiki series. Builds the instrument before touching content, so every later claim about coverage is a measured one.

The baseline

774 ```flow blocks across the tracked markdown. Nothing checked any of them.

Flow examples in documentation: 774

  verified          399  (51.6%)
  expected-error      0  ( 0.0%)
  ignored             0  ( 0.0%)
  unverified        375  (48.4%)

  verified by harness rung:
    standalone      369
    stmt-wrap        30

  unverified stops at:
    parse           258
    types            62
    vacuous          42
    clang            13

Every block goes through the front end a real build uses: the field and dynamics source expanders, the parser, the strict type checker, the C generator, and clang. The whole run is about six seconds, with clang batched at 150 translation units per process.

The first version of this was wrong, and that shaped the design

I wrote a fast tier first: parse plus type check, in process, 0.4 seconds, 569 of 775 verified. Then I compiled those 569 through transpile and clang. A fifth of them do not build. On the first 120, 52 failed.

The cause was checker.strict = False. In lenient mode the type checker does not resolve names, so undefined identifiers sail through to clang and nothing ever looks. Three consequences, each now fixed because leaving it out produced green on documentation that does not work:

Strict checking, then codegen, then clang. Not a cheaper approximation of it.

Route through the source expanders. module_resolver expands the field and dynamics DSLs before parsing; the checker did not. So dsys plant { ... } and field T: f64[32] on Line were reported as syntax errors, which is the more damaging mistake: it pushes an author to rewrite documentation that is already correct.

Reject blocks that compile to nothing. theorem declarations and unused generics are erased before codegen, so clang was being handed an empty file and approving it. 42 blocks were passing this way. Two are worth naming: the theorem nat_zero_add example on the page whose entire subject is that theorems get checked, and a block under the heading ## Part 2: What gets rejected whose body is 100% commented out.

Two harness rungs, designed and then measured out

The harness exists because most documented examples are fragments by design: a page about struct syntax shows a struct, not a program wrapped around one. But two of the rungs I planned rescue zero blocks repo-wide:

  • flow-body (wrap in flow Demo { ... }) cannot work. flow_blocks._validate_flow rejects a flow that declares no state and restricts members to f32/f64, while the documentation writes state angle : Angle, input voltage : Voltage. Every dimensioned example is structurally excluded. Its intended cases are either aspirational syntax or already-complete flow blocks, where wrapping replaces a useful diagnostic (nested flow member 'angle' has type 'Angle', which is not a flow in this file) with a worse one.
  • decl-wrap (append a main) was worse than useless. A Flow translation unit needs no entry point to parse, type-check or generate C, so blocks with real declarations already pass standalone. All it ever did was hand an empty main to otherwise-empty blocks, which is exactly how 42 vacuous blocks were passing.

What survives is standalone and stmt-wrap, and the ledger records which one each block needed.

The no-op guard

Flow parses state angle as two bare variable expressions, and angle evolves as velocity as a variable followed by a cast of evolves to type velocity:

--- 'angle evolves as velocity'
    Variable        Variable(name='angle')
    CastExpression  CastExpression(expr=Variable(name='evolves'), target_type=Type(name='velocity'...

Both parse once wrapped in a main, and neither means anything like what the prose says. The guard rejects any harness result containing no-op statements, and recurses, so a bare identifier inside an if is caught the same as one at the top level. Checked in both directions: it rejects exactly the blocks that are genuinely not statements, with no false negatives.

Deduplicating the extractor

build_wiki.py:554 and verify_browser_interp.py:44 each held a copy of the same regex and the same "function main" in code filter, kept in sync by a comment saying so. They had already diverged: only build_wiki derived a section heading.

The shared regex was also wrong. With re.DOTALL and a non-greedy body, ```flow\n(.*?)``` can start at the closing fence of one block and run to the opening fence of the next, capturing the prose between as code. scripts/docs_blocks.py walks lines under the CommonMark fence rule instead. Tutorial extraction is byte-identical across the change, verified by running the old logic side by side: 257 lessons, same ids, titles, sections and bodies.

Info strings

```flow expect-error                     must FAIL to compile
```flow ignore="needs a GPU device"      excluded, reason required
```flow no-harness                       verify as written or not at all

host=, preamble= and from= are designed but deliberately not shipped. They parsed and did nothing, which is worse than absent: an author writes preamble=docs/_preambles/gfx.flow, the block is checked without it, and a pass means nothing. They return when something honours them.

Unknown words raise, so expect_error fails loudly rather than quietly verifying a block the author meant to exempt.

Ledger keyed by content, not line

Rows are keyed path#sha256(code)[:12]. Inserting one paragraph at the top of VISION.md shifts all 30 of its blocks; across recent history 178 of 813 commits would have renumbered at least one row, up to 223 at once. A line-keyed 160 KB JSON in that traffic is a standing merge conflict that trains reviewers to resolve it blindly, which silently un-ratchets. A content key changes exactly when the block does, which is also when it genuinely needs re-verifying.

Scope

Reporting only. No CI gate, no content changed. The gate arrives later in the series once the count is down, using the ledger as a ratchet. The 375 unverified are not one population; they split into docs using syntax the language never had, aspirational VISION.md vocabulary, cross-block context, and compiler backend bugs. Each wants a different fix and a separate PR.

Validation

  • Unit suite: 1444 passed, 2 skipped (1400 baseline plus 44 new)
  • ruff check src clean; new and touched scripts clean
  • check_doc_links.py and check_wiki_links.py clean
  • build_wiki.py rebuilds and still emits 257 lessons, byte-identical

godofecht and others added 2 commits August 18, 2026 09:24
First step of the wiki work: build the instrument before changing any
content, so every later claim about coverage is measured.

There are 775 ```flow blocks across the tracked markdown. None of them was
checked by anything. `scripts/check_doc_examples.py` compiles all of them
in about half a second, in process, and writes a ledger to
docs/generated/example-status.json. The baseline is 569 verified (73%) and
206 unverified.

Most documented examples are fragments by design, so the checker tries a
short ladder of wrappers and records which rung each block needed. A block
that only passes under the loosest wrapper stays visible in the ledger
rather than disappearing into a green count.

The ladder needs a guard. Flow reads `state angle` as two bare variable
expressions and `angle evolves as velocity` as a variable followed by a
cast of `evolves` to type `velocity`. Both parse once wrapped in a main,
and neither means anything like what the surrounding prose says. Without a
check for no-op statements the tool reported 581 verified; 12 of those were
prose. All 12 are in VISION.md, describing syntax the language does not
have yet, which is what the ignore= tag is for.

Extraction moves into scripts/docs_blocks.py. build_wiki.py and
verify_browser_interp.py each carried a copy of the same regex and the same
"function main" filter, kept in sync by a comment, and had already
diverged: only one derived a section heading. The regex also mispaired
fences, since with DOTALL and a non-greedy body it can start at a closing
fence and run to the next opening one, capturing prose as code. The
replacement walks lines and applies the CommonMark fence rule. Tutorial
extraction is byte-identical before and after: 257 lessons, same ids,
sections and bodies.

Per-block metadata goes in the info string, which markdown offers and
nothing was using: expect-error, ignore="reason", host=, preamble=, from=,
flow-body. Unknown words are rejected, so a typo fails loudly instead of
quietly verifying a block the author meant to exempt.

Reporting only. No CI gate yet.

Unit 1426 passed, ruff clean, doc-links and wiki-links clean.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
A stress test of the first version found it was overstating. It reported
569 of 775 verified; compiling those through transpile and clang showed
that a fifth of them do not build. On the first 120 alone, 52 failed.

The cause was the fast tier. It ran the type checker with strict=False,
and in lenient mode names are not resolved, so undefined identifiers
survived to clang and the checker never looked. Three fixes, each one
because leaving it out produced green on documentation that does not work:

- Strict type checking, then C generation, then clang. Batching clang at
  150 translation units per process keeps the whole run near six seconds,
  so there is no reason to stop early.
- Route source through the field and dynamics expanders the way
  module_resolver does. Without them `dsys plant { ... }` and
  `field T: f64[32] on Line` were reported as syntax errors, which is the
  opposite mistake: correct documentation called broken.
- Reject blocks that compile to an empty translation unit. `theorem`
  declarations and unused generics are erased before codegen, so clang was
  being asked to approve an empty file. 42 blocks were passing this way,
  including the theorem example on the page whose subject is that theorems
  are checked, and a block under the heading "What gets rejected" whose
  body is entirely commented out.

Two harness rungs are removed after measuring that both rescue zero blocks.
`flow-body` cannot work: _validate_flow rejects a flow with no state and
limits members to f32/f64, while the docs write `state angle : Angle`.
`decl-wrap` was worse than useless; a translation unit needs no entry point
to compile, so all it ever did was hand an empty `main` to otherwise empty
blocks and defeat the vacuity check.

The no-op guard now recurses, so a bare identifier inside an `if` is caught
the same as one at the top level.

`host=`, `preamble=` and `from=` are removed from the accepted info string
until something honours them. They parsed and did nothing, which is worse
than absent: an author writes preamble= and gets a green that means nothing.

Ledger rows are keyed by content hash rather than line number. Inserting a
paragraph at the top of VISION.md moves all 30 of its blocks; across recent
history 178 of 813 commits would have renumbered rows, up to 223 at once.
That is a standing merge conflict that trains reviewers to resolve it
blindly. A content key changes exactly when the block does.

Honest baseline: 399 of 774 verified (52%). Unverified stops at parse 258,
types 62, vacuous 42, clang 13.

Unit 1444 passed, ruff clean.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant