diff
The steps that move ledger to new: prepare every edge it lacks, close and retire every edge it no longer wants, and activate the replacements.
Ownership comes from the ledger. Every edge in ledger whose id is in the form Topology.lower or Topology.attach writes, and whose child lies under new's root, belongs to the diff — whichever topology wrote it, and whether or not the caller still has that topology. So an edge left closing by an earlier reload is still retired by this one, and a diff after a restart needs nothing but the ledger.
Attached children (Topology.attach) are owned the same way, and which of them exist is the ledger's to say, not new's. Under a group that keeps its template in new, an attached child — one with an active edge, or a prepared one waiting for a drain — keeps its edge at the template's weight, so a reload that leaves the template alone leaves the child alone, history and all, and a new template weight gives it a new edge. Under a group that lost its template, or is gone, every attached child is closed and retired, and its prepared edges are retired without a close, so it does not come back if the template does.
Re-entrant. The result is a function of ledger, new and generation alone. Apply any prefix of it, call diff again with the same generation, and you get exactly the rest; a ledger that already holds new gives an empty list. A step that was refused simply reappears the next time.
For each child new declares, the diff keeps an active edge at the declared weight if there is one, else adopts the newest prepared edge at that weight, else prepares a new one. Every other active or closing edge into that child is closed and retired, and so is every active or closing edge of a child new no longer declares.
A prepared edge nothing wants is left prepared. It carries nothing and nothing can cross it, and closing it would make it a second live edge into its child — which quarantines the child, and with it every group below, until the retire lands. If its weight comes back, it is adopted, unless the ledger has forgotten it: LocalHeddle forgets such an edge in the swap that lands a list (unwantedPrepared), and a new edge is prepared instead. An attached child nothing wants is the exception, as above: its prepared edge is retired, so the child cannot come back with the template. It is retired straight from PREPARED, never closed, and ahead of the child's close, so no prefix of the list gives the child two live inbound edges or leaves it closed beside a prepared edge that reads as attached.
A change of weight is a new edge, and its history resets: the group restarts at its parent's front. A rename or a move is a delete plus an add, so the new child starts with no history and the old edges keep theirs.
The list is in a fixed order, the same on every target: prepares from the top down, then the activations of brand-new children, then closes from the top down, each attached child's close preceded by the retires of its abandoned prepared edges, then retires from the bottom up, the activations of replacement edges from the top down, and last the TopologyStep.Reconciles of internal groups whose old edges carried work. Within a phase, steps sort by depth, then child id, then edge id, except that a child's steps the ledger cannot refuse come first: the retire of an abandoned prepared edge among the closes, and a retire with nothing outstanding. The ledger is all the diff reads, so it cannot see an open lease: a LocalHeddle still holds that second retire back as StepRefusal.Draining while an open lease reserving nothing crosses the edge, and the held step ends that child's steps for the round. A replacement edge activates only after its predecessor retires, so a child never has two inbound edges carrying work.
Parameters
a counter, >= 0, for the edges this diff prepares. It is raised past every generation the ledger holds, so a caller that lost its counter still never reuses an id. The list is valid only against this ledger, and an interpreter applies it in order, under the same lock as the diff. The contracts are on TopologyStep.
Throws
if ledger belongs to another root — minted at one, or holding edges in this grammar and none under new's root — since a change of root is not a reload; or if generation is negative.
Samples
val before = draft("pool") {
group("acme", Weight.of(3)) { lane("batch") }
}
// A ledger with supply and no edges yet: the diff prepares the whole tree, then activates it.
var ledger = EntitlementLedger.bootstrap(before.groupId(), mapOf(ReplicaId("me") to 100L), "boot")
for (step in Topology.diff(before, ledger, generation = 0)) {
val patch = when (step) {
is TopologyStep.Prepare -> ledger.prepare(step.record)
is TopologyStep.Activate -> ledger.activate(step.edge)
else -> error("a new tree is only prepared and activated, not $step")
}
ledger = ledger.piece(checkNotNull(patch))
}
check(Topology.diff(before, ledger, generation = 0).isEmpty())
// A new weight is a new edge: prepare it, close and retire the old one, then activate.
val after = draft("pool") {
group("acme", Weight.of(4)) { lane("batch") }
}
val acme = after.groupId("acme")
val steps = Topology.diff(after, ledger, generation = 1)
check(
steps == listOf(
TopologyStep.Prepare(AttachmentRecord(AttachmentId("pool/acme@4:1#1"), after.groupId(), acme, Weight.of(4))),
TopologyStep.Close(AttachmentId("pool/acme@3:1#0"), acme),
TopologyStep.Retire(AttachmentId("pool/acme@3:1#0"), acme),
TopologyStep.Activate(AttachmentId("pool/acme@4:1#1"), acme),
),
)