Invariants before algorithms¶
A useful invariant says what stays true while the algorithm runs. It also says what the caller may rely on when the algorithm stops. Write it before writing the loop.
Preorder subtree intervals¶
Assume a tree is emitted in preorder. Every node is emitted before its children. Assume each subtree is contiguous. Let \(e_i\) be the first index after node \(i\) and all its descendants.
Claim. A traversal can skip the subtree by advancing to \(e_i\).
Proof. Contiguity puts every descendant before \(e_i\). The exclusive-end definition puts every later node at or after \(e_i\). Advancing there therefore skips exactly the subtree. The validator checks the assumptions before a renderer relies on them.
A complete scan visits each node once. Its traversal cost is \(O(N)\). Skipping a subtree takes one index update. Payload work still costs what it uses. Do not confuse a constant-time skip with constant-time rendering.
Bounded emission¶
A renderer runs once, into an emitter that holds an output limit \(c\)
(Limits::max_output_bytes). Let \(m\) be the number of bytes the rendering
produces. The emitter keeps counting after the limit is passed, without storing more:
The failure therefore reports the exact size that would have fitted, and a caller
that raises the limit to \(m\) succeeds on the next run. check_render runs the
same renderer in check mode and returns \(m\) and any support problems without
keeping output. One run with a counting emitter gives this guarantee: the output is never measured in one pass and written in a second.
A failure returns no valid partial artifact. The builder and the emitter poison
themselves on the first failure, so every later call returns that failure and ?
carries it out. The host owns publication, so incomplete bytes cannot replace a
previous output.
Cache validity¶
A cache key must cover the data that can change a result. Sources alone are insufficient. Includes, templates, configuration, reference lookups, resources, plugin identities, and adapter versions can affect the output. Reuse is allowed only when the recorded dependencies still agree. A missing output invalidates reuse even when its key matches.
Guidedog records, for every output unit, the digests of what it read and the answers to the lookups it made (a verifying trace with dynamic dependencies). A rebuild recomputes each answer and stops early where the answers agree. One place, the generation’s reuse check, decides whether a unit is written again. Incremental builds lists every digest input.
Tests cover the bounds rather than counting on them. A counting allocator checks that
a conversion in steady state allocates nothing. Limit tests check that oversized
input becomes a Capacity or Limit result, never a panic or a silently shortened
document.