kuilt-heddle-core
Decides who gets the next slice of something shared, fairly, and keeps the books on it — with no network involved.
Imagine several teams drawing from one budget of computing time. You want each team to get the slice it was promised (say, three parts to one), and you want a team that isn't using its share to lend it to a busier one. This module is the arithmetic of that: a tally of who was granted what, who passed their grant down to a sub-team, and who actually spent it, plus the rule that picks who is served next. Two copies of the tally can always be merged, in any order, and agree.
:kuilt-heddle puts exactly this onto a live connection between peers. Take this module alone when you only need the decision and the books in one process: it has no Seam, no Raft, no replication and no coroutines, and its build fails if it ever gains one (verifyHeddleCoreDependencies).
Start here: a scheduler in one program
Most programs want the whole loop rather than its parts: when a worker frees up, ask whose work runs next, run it, and charge what it really cost. That is LocalHeddle. Describe the groups and their weights as a Topology (in code with draft, or from an admin's file with Topology.from), start an engine with LocalHeddle.fromTopology, which also reports any group it left out because its children's weights do not fit together, tell it how many slots you run with reconcileCapacity, and then lease one slot at a time:
// Both groups have work queued, and this slot can run either.
val queued = setOf(heavy, light)
val first = heddle.acquire(waiting = queued, eligible = queued, startedAt = 100L)
check(first is AcquireOutcome.Acquired)
// The job overran its 3-unit estimate: all 5 units are charged, 2 of them as overrun.
check(heddle.complete(first.lease.id, actualCost = 5L, waiting = queued) == LeaseOutcome.Done(charged = 5L, overrun = 2L, unrecorded = 0L))The rest of the surface, by what you would ask it:
Who goes next, and why not me?
previewPickslists the next picks without making them, from a count per leaf or from each leaf's queue of job estimates;explainreturns aWaitExplanation;entitlementis a leaf's fair share of the slots as an exactRational;shareLine(waiting)reads the tree once and itsShareLine.sharegives any group's share of the pool, the root and every group above a lane included, andShareLine.laghow far the group stands from that share at its parent, in units of service, positive when it is owed. A reader holding only a stored snapshot asks the same four ofLocalHeddleSnapshot.view(policy)(frompersist()), with no engine.No more than N of these at once. A
maxConcurrenton a group, a lane or a group's template is a ceiling: the most leases open beneath it at once,0to pause it. A pick skips a leaf beneath a node at its ceiling for a sibling,explaintells that leafWaitExplanation.AtCeiling(group, limit), andentitlementowes it nothing until a lease beneath the node closes. Work already running is never refused or preempted.Other endings.
extendreserves more for a job still running,cancelhands back a job that never ran and charges nothing, andadmitcharges work that started without a pick. A job that ran settles withcompleteat what it cost, however it ended.The tree changes.
applytakes a whole newTopologyand returns anApplyResult, whoseStepRefusals name what is still draining.attachanddetachadd and remove children the program names as it runs.What would this edit do?
previewApplyworks anapplyout on a copy and returns aTopologyPreview: theApplyResultthe apply would return, and aLocalHeddleViewof the engine straight after it, to ask who goes next and what each group is owed. Nothing it came from changes. Itsbasisis the engine'srevisionat the time; pass the samegenerationto the apply. A page holding only a stored snapshot asksLocalHeddleSnapshot.previewApply(policy, …).Two admins edit at once. Pass the tree the edit was loaded from,
apply(new, generation, startedFrom), and the save applies only while that tree is stilltopology; otherwise it returnsApplyOutcome.TreeChangedand changes nothing.previewApplytakes the samestartedFrom. Leases never refuse a save: it compares trees, not revisions.Something crashed.
reconcileLeasescompares the open leases with the work still running and reports aLeaseReconciliation.The program restarts.
persistreturns aLocalHeddleSnapshotto encode as JSON or CBOR;LocalHeddle.decodereads it back as aRestoreResult, andLocalHeddle.restorecarries on from it under a later epoch, a boot counter you persist and raise on every start. 0.7.5 writes and reads format 1 alone (SUPPORTED_FORMAT_VERSIONS), the first frozen format. A later release reads 0.7.5's format 1 by translating each origin it holds into its children's positions, rounded up, and writes format 1 with none. A consumer coming from 0.7.4 starts from its topology again: 0.7.4's snapshots are not read.
Every refusal is a typed outcome (AcquireOutcome, AdmitOutcome, LeaseOutcome) that changes nothing, and the engine is not thread-safe: one writer, under your own scheduling lock; in a coroutine program, FairSharePool (:kuilt-heddle-coroutines) is that lock. Two fairness gaps are known. An estimate's size leans share toward groups that under-reserve, and a lease still running when its group returns from idle can hand idle credit back. Both are explained, with what to do about them, in the guide's Fair share in one program, which walks a whole build farm through every call above.
The sections below are the parts LocalHeddle is built from, for a caller that needs to drive them directly.
What the ledger keeps
The ledger's data layer — the shared tally and its order-independent merge rule — plus the economics that ride on top: who may spend what (derived holdings), the conserving operations that move entitlement around (granting, returning, transferring, spending), and an integrity report that flags tampering.
Safety lives in one place: before it moves anything, an operation checks feasibility against the peer's own complete copy and either returns a small patch to merge in or refuses — so no honest peer ever spends beyond its share, with no coordination. The integrity report is a diagnostic, not a gate: on a fully-shared copy it reads the same everywhere, but while a hand-off is still propagating it can momentarily flag a multi-hop transfer that has not yet caught up; that clears itself as copies reconcile. Don't block work on the report being empty — block on the operation refusing.
Every edge also carries a lifecycle — prepared → active → closing → retired — kept as a max-register that only ever climbs. Preparing an edge creates it without letting entitlement cross; activating it opens delegation; closing it stops new delegation while still letting entitlement drain back out; retiring it is allowed only once the edge has fully drained (or straight from prepared, if nothing ever crossed it), after which nothing crosses it again and its history stays queryable forever. Because merging takes the higher state, closure always wins over a lagging peer still trying to activate — a closed edge can never be resurrected. Changing a child's weight or parent is not a mutation: it mints a new generation (a new edge id) and drains the old one, so old history is never overwritten. Two peers that concurrently activate different inbound edges for one child surface a reported DualActiveInbound conflict — not a silently-picked winner — and delegation across either is refused until the control plane resolves it.
How the tally is kept
The library is built entirely from the conflict-free replicated data types in :kuilt-crdt — grow-only counters (GCounter) and grow-only maps. Everything is a value in a join-semilattice: merging two copies (piece) takes the least-upper-bound, so merging is idempotent, commutative, and associative, and any two peers that have seen the same set of updates — in any order, with any repeats — compute exactly the same ledger. Nothing is ever resolved by a wall clock or by who-merged-first.
Fairness state lives on the edge between a parent group and a child group — an immutable attachment generation (AttachmentRecord) — not on the child itself. For each edge the ledger keeps a small set of monotone counters:
| Counter | Meaning |
|---|---|
issued | entitlement delegated down across this edge |
returned | entitlement handed back up across this edge |
leafSpent | service charged where this edge is the final edge of the spending path |
rollupSpent | service charged where this edge is a strict prefix of the spending path |
A peer's spendable authority is always derived from these counters (subtracting only slots that peer itself wrote), never stored — which is what makes the local "can I spend?" check safe with zero coordination. Root supply enters through minted, keyed by a unique MintId so two independently-recorded mints union rather than collide. Peer-to-peer hand-offs at one path live in transfers.
piece is just the componentwise join of all of these: grow-only union for the immutable topology and mints, per-edge counter max for the accounting, and a nested per-row max for transfers. A finite product of semilattices is a semilattice, so the whole ledger inherits the three merge laws for free.
Types at a glance
GroupId/AttachmentId/MintId/PathKey— opaque, stably-serialized identifiers; aPathKeynames an entitlement path by its final edge, with aROOTsentinel for the root path.Weight— a positive integer ratio; sibling shares are compared by exact cross-multiplication, never floating point, so every peer orders them identically.ServiceUnits— a non-negative quantity of service, with overflow-checked arithmetic.AttachmentRecord/MintRecord— the immutable facts the ledger unions. AMintRecordnames the root it was minted at, so supply is creditable at that group and nowhere else: merging two separately bootstrapped ledgers cannot count either one's mint twice.Lifecycle— an edge'sPREPARED → ACTIVE → CLOSING → RETIREDstate, merged by taking the higher one so closure always dominates activation.LedgerConflict— the surfaced integrity/topology faults, includingDualActiveInbound(a child with two active inbound generations),ClosureViolation(entitlement crossing a retired generation),LineageCycle(inbound edges that loop instead of reaching a root),OrphanedTransferPath(a peer-to-peer hand-off the topology moved out from under, which conservation is structurally blind to),MultipleRoots(supply minted at more than one root — two bootstraps merged into one ledger),ConservationViolation(the global backstop: more service charged than supply ever minted) andFrozenCarriedHandoff(a hand-off frozen on a replaced generation, naming the peer whose fence ack the blocked move is waiting on — the one report here that names a peer as the cause rather than as a party to the fault).Gauge— an edge's replicated virtual-time seat: where a new child starts competing, written by the scheduler and merged by taking the larger reading, so no single peer's guess is frozen in. The position is kept in 128 bits (ticksHigh,ticksLow), exactly and never capped, so a long-lived busy parent cannot fill it. A gauge carrying a non-zerofloor, v0.7.4's form, does not decode, whatever the format's settings, since read without it the edge would sit lower than it was written.EdgeSummary— the parent-facing projection of one edge (issued/returned/spentand the derivedoutstanding).EntitlementLedger— the replicatedQuiltedstate itself.HeddlePolicy— the pure EEVDF scheduling policy.HeddlePolicy.pick(edges, config, localHoldings)decides which child to delegate the next quantum to: among the children that both want service (Demand) and are not running ahead of their fair share, it serves the one whose next grant finishes soonest in virtual time, breaking ties by a stable identity so every replica picks the same winner. No wall clock, no randomness, no floating point — so partitioned peers stay consistent.FairShareAllocator/LedgerCell— the loop that drives the policy against a ledger, and the one-method seam it reads and writes through (below).Demand— how much more service a child could usefully take right now. Advisory only: it can be stale or lost and the worst outcome is entitlement briefly parked in the wrong pocket; it can never authorize a spend.Rational— an exactnumerator/denominatorvalue overLong, the type the policy reasons in. Virtual times are compared by exact cross-multiplication (neverDouble), which is what makes a scheduling decision bit-identical on every platform.HeddleInternalApi— an opt-in marker on the few declarations:kuilt-heddle's control plane reads and no consumer should: the finals a replica declares on a fenced edge (SlotFinals), the move derived from them (Relocation), and the projections behind both. They carry no stability promise and may change in any release.
Arithmetic that would exceed Long throws rather than wrapping: a fair-share tally that silently overflowed would be worse than one that stopped. A child's position is 128-bit everywhere, and a pick compares the full value, so no position refuses a pick: the gauge stores it exactly, and the raw position, the front, a wake clamp and the deadline a pick orders are wide too (#3353). What a long-lived engine reaches first is the lifetime limit: two Long counters, an edge's effective issuance and the root's minted supply. A pick keeps LocalHeddle.LIFETIME_HEADROOM (2^58 units) in hand on each, and answers LifetimeExhausted(parent) rather than pass Long.MAX_VALUE less that, from LocalHeddle.acquire and FairShareAllocator.schedule alike. Work already running is never refused for it: admit, extend and complete use the headroom, and a charge that still does not fit is recorded whole or not at all, never in part, and reported in LeaseOutcome.Done.unrecorded, and an overrun above LocalHeddle.MAX_RECORDED_OVERRUN is never recorded, so one wrong cost cannot end an engine. No call throws at either counter (#3475). A parent's load, the sum of its children's positions, is held at or below the position limit, 2^125 ticks, wherever a child lands: a seat, a wake clamp or a return that would pass it is not made, and a pick answers LifetimeExhausted(parent) instead, with nothing written. LocalHeddle.apply refuses such a joiner alone, up front (design §7.1). A position does still have a ceiling, past which the public virtual-time reads saturate, the penalty side. Nothing rebases a busy parent (#3472): a position only counts up from its edge's start, so a long-running parent passes its ceiling, and only those reads saturate. A newborn is seated at the front, a clamped waker at the front plus what its open leases reserve, less its sleeper credit (#3238), and a returner where its parent's net-service clock puts it, each exactly, wherever that is, and the picks order them all exactly (design §7.1). Every LocalHeddle call that charges, admit, extend and complete, takes the waiting set and observes it before its charge lands (#3471).
Deciding who runs next
Fairness state is kept per edge, but the decision — which child to hand the next quantum to — is a separate, pure step: HeddlePolicy.pick. It reads only the parent's immediate children (their EdgeSummary, their advertised Demand, the weight from the AttachmentRecord, and the edge's virtual-time seat from its Gauge) and returns a single Grant, or null when nobody is both eligible and demanding.
The rule is earliest eligible virtual deadline first (EEVDF): each child has a virtual time that advances as it is granted service (so a hoarder is charged the moment it is issued entitlement, not only when it spends); a child is eligible when it is not ahead of the weighted-mean of its siblings; among the eligible, the one whose next grant would finish soonest wins. A freshly created child starts level with its siblings — no credit for the parent's whole past — and an idle child that wakes is clamped forward to the current front, so neither can bank an unfair head start (design §7, §10.5–6). Each child's progress is a whole number of sixteenths of a unit, and every comparison is integer cross-multiplication, so the same inputs produce the same pick on JVM, Android, iOS, macOS, and wasmJs.
Scheduling without a network
FairShareAllocator.schedule(parent, ledger, earmarked, demand) runs the policy to exhaustion: build each child's input from the ledger, pick, apply the grant, repeat until nothing more can move. Before the first pick it seats any child that has no seat yet and clamps any child that just woke, both against one front, so neither joiner drags the front down to meet itself. When nobody else is competing, that front is the child that has had the most, so everyone joining starts level with it. It answers an AllocationOutcome: Delegated with the grant count, NothingToDelegate, WeightBudget when the parent's children cannot be ordered at all, or LifetimeExhausted when the first grant would carry the picked edge past the lifetime watermark.
Weights have a budget, and it has two halves. A pick multiplies a parent's weights together, so a mix whose products leave a 64-bit number is refused. And a weight set too fine for one full lease is refused: keep every weight's denominator at or below 30,719; a finer weight set is refused when it is applied. The limit holds at any lease size, because it is sized for the largest lease, MAX_RESERVATION, and no grant is larger. It is necessary, not sufficient: several fine weights whose denominators share no factor are refused below it too. LocalHeddle.apply answers a StepRefusal for such a child, and for each new group beneath it when the child is new itself, so no lane activates below a group the root cannot reach, whether apply, attach or a boot would add it; a running group whose reweight is refused keeps its old edge and everything beneath it. And LocalHeddle.fromTopology, which has no running tree to keep, leaves out the child's parent and everything beneath it, reporting each in its Boot. It remembers, between calls, which children it has seen idle and what clamp each carries; that memory is local to the allocator and is never part of the ledger.
It reads and writes through a LedgerCell: the current ledger, and one step that decides a write against the state it lands on. For a single process that is a var; :kuilt-heddle's HeddleNode hands it the replicator instead, and is otherwise this allocator plus a demand board and reservations. An allocator is not thread-safe: one per scheduling peer, called by one thread at a time.
See docs/heddle-design.md and docs/heddle-ledger-design.md for the full model.