Learn · Hidden Machines · Part II
budding
The Newest Messenger Holds the Seal
Generation fences and stale-result rejection.
You will recover a generation fence from stale asynchronous settlement, separate completion from publication, and identify cases requiring leases or stronger authority.
Arrival order is not intent order
A user types ca, then cat. The first request is slow. The second returns and paints suggestions for cat; a moment later the first paints suggestions for ca. Both callbacks succeeded. Only one retained publication authority.
The pure model carries currentGeneration. Starting an intent increments it and returns a request effect labeled with that value. Settlement carries the same label back. The transition publishes only when the labels match.
The invariant is compact: published output belongs to the maximal admitted generation. “Maximal” means newest in the program's intent order, not greatest timestamp. A trace test permutes completion order and checks that only the current identity can change visible output.
Why cancellation is insufficient
Canceling old transport may save work, but it is not the correctness proof. Cancellation can arrive after completion, be unsupported, or merely signal a cooperative request. The generation check remains at the publication boundary.
A lease strengthens the idea when authority may expire or transfer: identity plus a validity condition determines whether an actor may act. Distributed systems may need epochs issued by an authority rather than a process-local counter. The structure grows only when reachable failures demand it.
Where the model stops
A generation fence rejects stale publication. It does not make repeating an external effect safe. If an old payment request may have committed, assigning the retry a newer generation does not undo the first charge. That problem needs receiver-recognized operation identity or reconciliation.
Lessons
- Intent order, completion order, and wall-clock order differ.
- Completion creates a candidate; a matching generation authorizes publication.
- Cancellation is an optimization beside the fence, not a substitute for it.
- External retries need a different identity contract.
Practice
- Trace generations 7 and 8 completing in both orders.
- Distinguish a generation counter from a timestamp used only for display.
- Transfer the fence to cache refresh, language-server diagnostics, or image loading.
What if the uncertainty is not which answer is newest, but whether the first operation happened at all?
References
- “WHATWG HTML, “Event loops”.” — the platform ordering model behind asynchronous task settlement.
- “MDN, “AbortController”.” — cooperative cancellation, whose limits motivate retaining the publication fence.