@zakkster/lite-pick 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,55 @@ All notable changes to `@zakkster/lite-pick` are documented here. The format fol
4
4
  [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project adheres to
5
5
  [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.4.0] - 2026-09-23
8
+
9
+ M4: the exact LeastConn family (IPVS `lc` / `sed` / `nq` made zero-GC) + the seeded invariant
10
+ fuzzer (ROADMAP.md M4).
11
+
12
+ ### Added
13
+
14
+ - `LeastConnBalancer extends BalancerBase` -- EXACT fewest-in-flight (IPVS `lc`). A full O(cap)
15
+ scan of the caller-owned in-flight view returning the eligible node with the lowest count
16
+ (lowest index on a tie); the deterministic complement to P2C's O(1) approximation. In-flight
17
+ is read LIVE (no `setWeight`, no derived aggregate -- the caller may mutate it directly).
18
+ 0 B/op. Fails closed (`PICK_NONE`) when the whole pool is down.
19
+ - `SedBalancer extends BalancerBase` -- shortest-expected-delay (IPVS `sed`). Returns the
20
+ eligible, positive-weight node minimizing `(inflight + 1) / weight`. BOTH inflight and weights
21
+ are caller-owned, read live. A weight-0 eligible node is not a candidate; all-zero-weight fails
22
+ closed even with the pool up. O(cap), 0 B/op.
23
+ - `NqBalancer extends BalancerBase` -- never-queue (IPVS `nq`). Returns the FIRST idle eligible
24
+ positive-weight node (in-flight 0) if one exists, else the SED minimum -- the worker-pool fit.
25
+ O(cap) worst case, O(1) when an early node is idle, 0 B/op.
26
+ - **The invariant fuzzer** (`test/fuzz.mjs` + the reusable `test/invariants.mjs` checker): a
27
+ seeded, property-based state-machine attack asserting STATE-SYNCHRONISATION invariants after
28
+ EVERY op (strict mode) per strategy -- `live` and (SmoothWRR) `_totalEligibleWeight` stay EXACT
29
+ vs a manual recompute, owned Float64 accumulators stay finite, `PICK_NONE` holds IFF the
30
+ pickable mass is 0, and LeastConn/SED/NQ return the true optimum (NQ its idle-first rule). Prints
31
+ the seed on failure for byte-for-byte replay; CI runs a fixed seed + a random seed + a regression
32
+ corpus + a pathological corpus (max-weight 0xFFFFFFFF summed, all-zero-weight while live>0,
33
+ single-node). Retrofits M2 SmoothWRR. Wired into `npm run fuzz` and `npm run verify`.
34
+ - `test/LeastConn.test.js` (10), `test/SED.test.js` (8), `test/NQ.test.js` (10) -- boundary +
35
+ behaviour suites (exact minimum, weight-0 exclusion, feedback-loop balance, idle-first fan-out,
36
+ never-a-down-index under 200k churned picks).
37
+ - Balance anchors (`test/balance.mjs`): LeastConn is greedy-perfect (max-minus-min <= 1, tighter
38
+ than P2C's gap; peak <= P2C's on the same run); SED converges to load proportional-to-weight
39
+ (< 1% drift; weighted-imbalance far below a random foil); NQ fans the first n dispatches out to
40
+ n distinct idle workers.
41
+ - Gates extended for all three: torture (retention + 0 B/op `pick()` phases 6-8), PerfGate
42
+ (three `zgcSuite` scenarios + three `mustFail` teeth-checks), witness (`linear` complexity ->
43
+ flat work-rate), benchmark matrix (LeastConn/SED/NQ subjects + a per-pick-allocating
44
+ `lc-array` foil).
45
+ - `decisions/0006-leastconn-family.md` -- P2C-is-already-least-conn (no redundant alias), exact-
46
+ O(cap)-scan-first, caller-owned live-read counters (the documented asymmetry with SmoothWRR),
47
+ and the confirmed-but-deferred lite-logn `BinaryHeap` exact-O(log n) peer seam.
48
+
49
+ ### Changed
50
+
51
+ - Version 0.3.0 -> 0.4.0 across `package.json`, `Pick.js` `VERSION`, and `llms.txt`.
52
+ - Folded the invariant-fuzz testing methodology (RESEARCH s3, ROADMAP s3/s0) into an adopted,
53
+ shipped gate: vectors 2 (flap chaos) + 3 (zero-GC soak) were already covered; the net-new
54
+ seeded state-synchronisation fuzzer is now `test/fuzz.mjs`.
55
+
7
56
  ## [0.3.0] - 2026-09-23
8
57
 
9
58
  M3: P2C (power-of-two-choices), the headline strategy -- and the balance-quality anchor
package/Pick.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * @zakkster/lite-pick -- TypeScript declarations.
3
3
  *
4
- * M3 (0.3.0): substrate seams + RoundRobin + SmoothWRR + P2C (the headline). The
5
- * remaining strategy classes (LeastConn/SED/NQ, PeakEWMA, ConsistentHash, BoundedLoad,
4
+ * M4 (0.4.0): substrate seams + RoundRobin + SmoothWRR + P2C + the exact LeastConn family
5
+ * (LeastConn/SED/NQ). The remaining strategy classes (PeakEWMA, ConsistentHash, BoundedLoad,
6
6
  * WeightedRandom) are added one per session.
7
7
  */
8
8
 
@@ -105,3 +105,58 @@ export class P2cBalancer extends BalancerBase {
105
105
  /** Pick by power-of-two-choices (lower in-flight of two random eligibles), or `PICK_NONE`. */
106
106
  pick(): number;
107
107
  }
108
+
109
+ /**
110
+ * LeastConnBalancer -- EXACT fewest-in-flight (M4, IPVS `lc`). A full O(cap) scan of the
111
+ * caller-owned in-flight view returning the eligible node with the lowest count (lowest index
112
+ * on a tie) -- the deterministic complement to P2C's O(1) approximation. In-flight counts are
113
+ * caller-owned and read LIVE (no `setWeight`, no derived aggregate). 0 B/op. Fails closed
114
+ * (`PICK_NONE`) when the whole pool is down.
115
+ */
116
+ export class LeastConnBalancer extends BalancerBase {
117
+ /**
118
+ * @param capacity endpoint count (fixed).
119
+ * @param eligible shared view: 1 = pickable, 0 = down (length >= capacity).
120
+ * @param inflight per-endpoint in-flight counts (length >= capacity), caller-owned, read live.
121
+ */
122
+ constructor(capacity: number, eligible: Uint8Array, inflight: Uint32Array);
123
+ /** The eligible node with the fewest in-flight requests, or `PICK_NONE`. O(cap). */
124
+ pick(): number;
125
+ }
126
+
127
+ /**
128
+ * SedBalancer -- shortest-expected-delay (M4, IPVS `sed`). Returns the eligible, positive-weight
129
+ * node minimizing `(inflight + 1) / weight`; converges to load proportional-to-weight. BOTH
130
+ * inflight and weights are caller-owned Uint32Arrays, read LIVE (no `setWeight`, no derived
131
+ * aggregate). A weight-0 eligible node is not a candidate. O(cap), 0 B/op. Fails closed
132
+ * (`PICK_NONE`) when no eligible node has a positive weight.
133
+ */
134
+ export class SedBalancer extends BalancerBase {
135
+ /**
136
+ * @param capacity endpoint count (fixed).
137
+ * @param eligible shared view: 1 = pickable, 0 = down (length >= capacity).
138
+ * @param inflight per-endpoint in-flight counts (length >= capacity), caller-owned, read live.
139
+ * @param weights per-endpoint weights (length >= capacity), caller-owned, read live.
140
+ */
141
+ constructor(capacity: number, eligible: Uint8Array, inflight: Uint32Array, weights: Uint32Array);
142
+ /** The eligible node minimizing (inflight+1)/weight, or `PICK_NONE`. O(cap). */
143
+ pick(): number;
144
+ }
145
+
146
+ /**
147
+ * NqBalancer -- never-queue (M4, IPVS `nq`). Returns the first idle eligible positive-weight
148
+ * node (in-flight 0) if one exists, else the SED minimum -- the worker-pool fit. BOTH inflight
149
+ * and weights are caller-owned, read LIVE. O(cap) worst case, O(1) when an early node is idle,
150
+ * 0 B/op. Fails closed (`PICK_NONE`) when no eligible node has a positive weight.
151
+ */
152
+ export class NqBalancer extends BalancerBase {
153
+ /**
154
+ * @param capacity endpoint count (fixed).
155
+ * @param eligible shared view: 1 = pickable, 0 = down (length >= capacity).
156
+ * @param inflight per-endpoint in-flight counts (length >= capacity), caller-owned, read live.
157
+ * @param weights per-endpoint weights (length >= capacity), caller-owned, read live.
158
+ */
159
+ constructor(capacity: number, eligible: Uint8Array, inflight: Uint32Array, weights: Uint32Array);
160
+ /** The first idle eligible node, else the SED minimum, or `PICK_NONE`. O(cap). */
161
+ pick(): number;
162
+ }
package/Pick.js CHANGED
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * @zakkster/lite-pick -- zero-GC load-balancing SELECTION KERNEL.
3
3
  *
4
- * M3 (0.3.0): substrate seams + RoundRobin + SmoothWRR + P2C (the headline). This file ships:
4
+ * M4 (0.4.0): substrate seams + six strategies -- RoundRobin, SmoothWRR, P2C, and the
5
+ * exact LeastConn family (LeastConn, SED, NQ). This file ships:
5
6
  *
6
7
  * - VERSION the single source-of-truth version stamp (3-place sync).
7
8
  * - PICK_NONE the fail-closed sentinel (-1): "no endpoint", never a dead pick.
@@ -15,8 +16,16 @@
15
16
  * the eligibility view, skipping down nodes, O(1) amortized, 0 B/op.
16
17
  * - SmoothWRRBalancer the weighted default: nginx smooth weighted round-robin over
17
18
  * caller-configured integer weights, O(cap)/pick, 0 B/op.
18
- * - P2cBalancer the headline: power-of-two-choices over caller-owned in-flight counts;
19
- * the ln ln n balance ceiling, O(1)/pick, 0 B/op.
19
+ * - P2cBalancer power-of-two-choices over caller-owned in-flight counts; the ln ln n
20
+ * balance ceiling, O(1)/pick, 0 B/op. This IS the O(1) least-connections
21
+ * APPROXIMATION ("P2C-least-conn") -- the LeastConn family below is exact.
22
+ * - LeastConnBalancer EXACT fewest-in-flight (IPVS `lc`): a full O(cap) scan of the
23
+ * caller-owned in-flight view, 0 B/op. The deterministic complement to
24
+ * P2C's O(1) approximation.
25
+ * - SedBalancer shortest-expected-delay (IPVS `sed`): minimizes (inflight+1)/weight --
26
+ * charges the NEW request's marginal cost. O(cap)/pick, 0 B/op.
27
+ * - NqBalancer never-queue (IPVS `nq`): an IDLE eligible endpoint immediately if one
28
+ * exists, else SED. The worker-pool fit. O(cap)/pick, 0 B/op.
20
29
  *
21
30
  * The identity (decisions/0001): lite-pick OWNS NO mutable state it can avoid owning.
22
31
  * It reads pre-allocated views (eligibility, inflight, weights, scores) that siblings or
@@ -24,13 +33,15 @@
24
33
  * counters live OUTSIDE the kernel. The steady-state pick path allocates 0 B/op.
25
34
  *
26
35
  * Roster (one strategy per session -- see ROADMAP.md): RoundRobin [M1], SmoothWRR [M2],
27
- * P2C [M3], LeastConn/SED/NQ, PeakEWMA, ConsistentHash, BoundedLoad, WeightedRandom [planned].
36
+ * P2C [M3], LeastConn/SED/NQ [M4], PeakEWMA, ConsistentHash, BoundedLoad, WeightedRandom
37
+ * [planned]. The EXACT-O(log n) fewest-in-flight variant is a deferred @zakkster/lite-logn
38
+ * BinaryHeap optional-peer seam (decisions/0006), not this exact-O(cap) scan.
28
39
  *
29
40
  * Zero runtime dependencies. node:test only. ESM, single file, tree-shakeable.
30
41
  */
31
42
 
32
43
  /** Version stamp. Synced across package.json and llms.txt (three-place rule). */
33
- export const VERSION = '0.3.0';
44
+ export const VERSION = '0.4.0';
34
45
 
35
46
  /**
36
47
  * Fail-closed sentinel returned by pick() when no endpoint is eligible.
@@ -383,3 +394,178 @@ export class P2cBalancer extends BalancerBase {
383
394
  return this._inflight[b] < this._inflight[a] ? b : a;
384
395
  }
385
396
  }
397
+
398
+ /**
399
+ * LeastConnBalancer -- EXACT fewest-in-flight (M4), IPVS `lc` made zero-GC.
400
+ *
401
+ * `pick()` scans the whole pool and returns the eligible endpoint with the lowest in-flight
402
+ * count -- the deterministic, exact complement to P2cBalancer's O(1) two-choice APPROXIMATION
403
+ * of the same objective. Where P2C trades a tiny balance gap for O(1), LeastConn pays O(cap)
404
+ * for the exact minimum: in a closed feedback loop (the caller increments inflight on dispatch
405
+ * and decrements on settle) it is the greedy-optimal assignment -- max-minus-min load stays
406
+ * within 1 (test/balance.mjs proves the perfect balance, tighter than P2C's ln ln n gap).
407
+ *
408
+ * Ownership (ADR 0001, ADR 0006): in-flight counts live in the CALLER's Uint32Array, read-only
409
+ * to `pick()`. LeastConn owns NO derived state beyond the base `_live` -- it reads inflight
410
+ * live each scan, so (unlike SmoothWRR's weights) the caller may mutate the inflight view
411
+ * directly between picks; that is the whole point of the shared-counter seam.
412
+ *
413
+ * Bound: O(cap) per pick (one scan). Steady-state pick(): integer compares + one index write,
414
+ * no object/closure/array created -- 0 B/op. Tie-break is the lowest index (deterministic);
415
+ * the feedback loop breaks a startup all-zero tie by raising the picked node's count. Fails
416
+ * closed (PICK_NONE) when the whole pool is down.
417
+ *
418
+ * NOTE: without a feedback loop (inflight never changes) LeastConn returns the same lowest-load
419
+ * index every call -- correct by contract (it IS the least-loaded), but the caller must feed
420
+ * load back for it to distribute. The M5 lite-query adapter provides that increment/decrement.
421
+ */
422
+ export class LeastConnBalancer extends BalancerBase {
423
+ /**
424
+ * @param {number} capacity endpoint count (fixed).
425
+ * @param {Uint8Array} eligible shared view: 1 = pickable, 0 = down (length >= capacity).
426
+ * @param {Uint32Array} inflight per-endpoint in-flight counts (length >= capacity),
427
+ * caller-owned and only READ here.
428
+ */
429
+ constructor(capacity, eligible, inflight) {
430
+ super(capacity, eligible);
431
+ if (!(inflight instanceof Uint32Array) || inflight.length < capacity) {
432
+ throw new RangeError('[lite-pick] inflight must be a Uint32Array of length >= capacity');
433
+ }
434
+ this._inflight = inflight;
435
+ }
436
+
437
+ /**
438
+ * The eligible endpoint with the fewest in-flight requests, or PICK_NONE (fail closed).
439
+ * O(cap), zero-alloc. Lowest index on a tie.
440
+ * @returns {number}
441
+ */
442
+ pick() {
443
+ if (this._live === 0) return PICK_NONE; // whole pool down: fail closed
444
+ const cap = this._cap, el = this._eligible, inf = this._inflight;
445
+ let best = -1, bestLoad = 0;
446
+ for (let i = 0; i < cap; i++) {
447
+ if (el[i]) {
448
+ const c = inf[i];
449
+ if (best < 0 || c < bestLoad) { best = i; bestLoad = c; }
450
+ }
451
+ }
452
+ return best; // best >= 0 guaranteed while _live > 0
453
+ }
454
+ }
455
+
456
+ /**
457
+ * SedBalancer -- shortest-expected-delay (M4), IPVS `sed` made zero-GC.
458
+ *
459
+ * `pick()` returns the eligible endpoint that minimizes `(inflight + 1) / weight` -- the
460
+ * expected delay if the NEW request were placed there (the +1 charges the request itself).
461
+ * Higher-weight endpoints absorb proportionally more load; SED converges to inflight/weight
462
+ * equal across the pool (test/balance.mjs proves the weighted fairness). It is the weighted
463
+ * generalization of least-connections: with all weights equal, SED and LeastConn agree.
464
+ *
465
+ * Ownership (ADR 0001, ADR 0006): BOTH inflight AND weights are caller-owned Uint32Arrays,
466
+ * read-only to `pick()`. SED (like LeastConn, unlike SmoothWRR) owns NO derived weight
467
+ * aggregate -- it reads weights live each scan, so there is no `setWeight` and no total to
468
+ * desync: the caller may retune weights directly between picks. An eligible endpoint whose
469
+ * weight is 0 is NOT a candidate (its expected delay is infinite); if every eligible endpoint
470
+ * has weight 0, `pick()` fails closed.
471
+ *
472
+ * Bound: O(cap) per pick (one scan, one Float64 division per eligible node), 0 B/op. Lowest
473
+ * index on a tie. Fails closed (PICK_NONE) when no eligible endpoint has a positive weight.
474
+ */
475
+ export class SedBalancer extends BalancerBase {
476
+ /**
477
+ * @param {number} capacity endpoint count (fixed).
478
+ * @param {Uint8Array} eligible shared view: 1 = pickable, 0 = down (length >= capacity).
479
+ * @param {Uint32Array} inflight per-endpoint in-flight counts (length >= capacity), caller-owned.
480
+ * @param {Uint32Array} weights per-endpoint weights (length >= capacity), caller-owned; read live.
481
+ */
482
+ constructor(capacity, eligible, inflight, weights) {
483
+ super(capacity, eligible);
484
+ if (!(inflight instanceof Uint32Array) || inflight.length < capacity) {
485
+ throw new RangeError('[lite-pick] inflight must be a Uint32Array of length >= capacity');
486
+ }
487
+ if (!(weights instanceof Uint32Array) || weights.length < capacity) {
488
+ throw new RangeError('[lite-pick] weights must be a Uint32Array of length >= capacity');
489
+ }
490
+ this._inflight = inflight;
491
+ this._weights = weights;
492
+ }
493
+
494
+ /**
495
+ * The eligible endpoint minimizing (inflight + 1) / weight, or PICK_NONE (fail closed).
496
+ * O(cap), zero-alloc. Lowest index on a tie; weight-0 nodes are not candidates.
497
+ * @returns {number}
498
+ */
499
+ pick() {
500
+ if (this._live === 0) return PICK_NONE; // whole pool down: fail closed
501
+ const cap = this._cap, el = this._eligible, inf = this._inflight, wt = this._weights;
502
+ let best = -1, bestScore = Infinity;
503
+ for (let i = 0; i < cap; i++) {
504
+ if (el[i]) {
505
+ const w = wt[i];
506
+ if (w > 0) {
507
+ const score = (inf[i] + 1) / w;
508
+ if (score < bestScore) { bestScore = score; best = i; }
509
+ }
510
+ }
511
+ }
512
+ return best; // -1 when every eligible node has weight 0
513
+ }
514
+ }
515
+
516
+ /**
517
+ * NqBalancer -- never-queue (M4), IPVS `nq` made zero-GC.
518
+ *
519
+ * `pick()` returns an IDLE eligible endpoint (in-flight 0, positive weight) the instant one
520
+ * exists -- never leaving a free server idle while queueing elsewhere -- and otherwise falls
521
+ * back to SED (`(inflight + 1) / weight`). This is the best fit for the in-process worker-pool
522
+ * case: spin up idle capacity first, only weigh expected delay once everyone is busy.
523
+ *
524
+ * Ownership (ADR 0001, ADR 0006): identical to SED -- caller-owned inflight + weights, read
525
+ * live, no derived aggregate. The first idle eligible node (lowest index, in-flight 0, weight
526
+ * > 0) short-circuits the scan.
527
+ *
528
+ * Bound: O(cap) worst case (no idle node -> a full SED scan); O(1) when a low-index endpoint is
529
+ * idle. 0 B/op. Fails closed (PICK_NONE) when no eligible endpoint has a positive weight.
530
+ */
531
+ export class NqBalancer extends BalancerBase {
532
+ /**
533
+ * @param {number} capacity endpoint count (fixed).
534
+ * @param {Uint8Array} eligible shared view: 1 = pickable, 0 = down (length >= capacity).
535
+ * @param {Uint32Array} inflight per-endpoint in-flight counts (length >= capacity), caller-owned.
536
+ * @param {Uint32Array} weights per-endpoint weights (length >= capacity), caller-owned; read live.
537
+ */
538
+ constructor(capacity, eligible, inflight, weights) {
539
+ super(capacity, eligible);
540
+ if (!(inflight instanceof Uint32Array) || inflight.length < capacity) {
541
+ throw new RangeError('[lite-pick] inflight must be a Uint32Array of length >= capacity');
542
+ }
543
+ if (!(weights instanceof Uint32Array) || weights.length < capacity) {
544
+ throw new RangeError('[lite-pick] weights must be a Uint32Array of length >= capacity');
545
+ }
546
+ this._inflight = inflight;
547
+ this._weights = weights;
548
+ }
549
+
550
+ /**
551
+ * The first idle eligible endpoint (in-flight 0, weight > 0), else the SED minimum, else
552
+ * PICK_NONE (fail closed). O(cap) worst case, O(1) when an early node is idle. Zero-alloc.
553
+ * @returns {number}
554
+ */
555
+ pick() {
556
+ if (this._live === 0) return PICK_NONE; // whole pool down: fail closed
557
+ const cap = this._cap, el = this._eligible, inf = this._inflight, wt = this._weights;
558
+ let best = -1, bestScore = Infinity;
559
+ for (let i = 0; i < cap; i++) {
560
+ if (el[i]) {
561
+ const w = wt[i];
562
+ if (w > 0) {
563
+ if (inf[i] === 0) return i; // idle: never queue -- take it immediately
564
+ const score = (inf[i] + 1) / w;
565
+ if (score < bestScore) { bestScore = score; best = i; }
566
+ }
567
+ }
568
+ }
569
+ return best; // -1 when every eligible node has weight 0
570
+ }
571
+ }
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @zakkster/lite-pick
2
2
 
3
- > Zero-GC load-balancing **selection kernel**: one hot `pick()` that returns an endpoint **index** over a fixed pool and allocates **0 B/op** on the steady-state path. A pure selector, never a proxy -- it consumes health and circuit state, it never owns them. **v0.3.0 ships three strategies -- `RoundRobinBalancer`, `SmoothWRRBalancer`, and `P2cBalancer`** (power-of-two-choices, the headline) -- on the substrate seams (`VERSION`, `PICK_NONE`, a deterministic `Prng`, and `BalancerBase`'s shared read-only eligibility view). The rest of the roster -- LeastConn/SED/NQ, PeakEWMA, ConsistentHash, BoundedLoad, WeightedRandom -- lands one per session.
3
+ > Zero-GC load-balancing **selection kernel**: one hot `pick()` that returns an endpoint **index** over a fixed pool and allocates **0 B/op** on the steady-state path. A pure selector, never a proxy -- it consumes health and circuit state, it never owns them. **v0.4.0 ships six strategies -- `RoundRobinBalancer`, `SmoothWRRBalancer`, `P2cBalancer`, and the exact `LeastConnBalancer` / `SedBalancer` / `NqBalancer` family** -- on the substrate seams (`VERSION`, `PICK_NONE`, a deterministic `Prng`, and `BalancerBase`'s shared read-only eligibility view). The rest of the roster -- PeakEWMA, ConsistentHash, BoundedLoad, WeightedRandom -- lands one per session.
4
4
 
5
5
  [![npm version](https://img.shields.io/npm/v/@zakkster/lite-pick.svg?style=for-the-badge&color=latest)](https://www.npmjs.com/package/@zakkster/lite-pick)
6
6
  [![sponsor](https://img.shields.io/badge/sponsor-PeshoVurtoleta-ea4aaa.svg?logo=github)](https://github.com/sponsors/PeshoVurtoleta)
@@ -21,7 +21,7 @@ The npm landscape has old algorithm libraries (`load-balancers`, `loadbalance`,
21
21
  - **Two pieces of evidence, both shipped.** A **0 B/op** witness on the pick path (no object, closure, string, or array created per pick), and a measured **balance-quality anchor** -- peak-to-average load within the strategy's theoretical ceiling (for P2C, the Azar-Broder-Karlin-Upfal `ln ln n / ln 2` bound) and strictly better than a random foil.
22
22
  - **A pure selector, not a proxy.** It **consumes** health and circuit state; it never owns them. Health is a shared read-only bitmap written by [`@zakkster/lite-di-health`](https://www.npmjs.com/package/@zakkster/lite-di-health); circuit state comes from [`@zakkster/lite-statechart`](https://www.npmjs.com/package/@zakkster/lite-statechart); load counters are caller-owned typed arrays. `pick()` only reads.
23
23
 
24
- > **Status: M3 (v0.3.0).** Ships the substrate seams **plus `RoundRobinBalancer`, `SmoothWRRBalancer`, and `P2cBalancer`**. Every strategy is gated: `pick()` proven **0 B/op** (torture + PerfGate), RoundRobin **perfectly fair** with **zero dead picks** vs the naive `i++ % n` foil, SmoothWRR **exactly weighted** and **smooth**, and **P2C proves the `ln ln n` balance ceiling** -- peak-to-mean gap ~2 vs a random foil's ~21 at n=1024, holding flat as the pool grows. See [ROADMAP.md](./ROADMAP.md) for the M3 -> M10 path to 1.0.0, and [decisions/](./decisions) for the ownership boundary (ADR 0001), anti-flapping (ADR 0002), and the RoundRobin (0003), SmoothWRR (0004), and P2C (0005) design forks.
24
+ > **Status: M4 (v0.4.0).** Ships the substrate seams **plus `RoundRobinBalancer`, `SmoothWRRBalancer`, `P2cBalancer`, and the exact `LeastConnBalancer` / `SedBalancer` / `NqBalancer` family**. Every strategy is gated: `pick()` proven **0 B/op** (torture + PerfGate), RoundRobin **perfectly fair** with **zero dead picks** vs the naive `i++ % n` foil, SmoothWRR **exactly weighted** and **smooth**, **P2C proves the `ln ln n` balance ceiling** (peak-to-mean gap ~2 vs a random foil's ~21 at n=1024), **LeastConn is greedy-perfect** (max-minus-min load <= 1), and **SED tracks weight within 1%**. New in M4: a **seeded invariant fuzzer** (`test/fuzz.mjs`) that asserts each strategy's state-synchronisation invariants after *every* op. See [ROADMAP.md](./ROADMAP.md) for the M4 -> M10 path to 1.0.0, and [decisions/](./decisions) for the ownership boundary (ADR 0001), anti-flapping (ADR 0002), and the RoundRobin (0003), SmoothWRR (0004), P2C (0005), and LeastConn-family (0006) design forks.
25
25
 
26
26
  ```bash
27
27
  npm install @zakkster/lite-pick
@@ -102,6 +102,43 @@ The proof (from `test/balance.mjs`, the library's analytical anchor):
102
102
 
103
103
  P2C's gap stays a small `ln ln n` constant while the random foil's grows with the pool. `pick()` is **0 B/op** and **O(1)** (two expected-O(1) rejection draws + a compare); in-flight counts are your caller-owned `Uint32Array` ([ADR 0005](./decisions/0005-p2c-draw.md)).
104
104
 
105
+ ## LeastConn / SED / NQ -- the exact load-aware family (v0.4.0)
106
+
107
+ P2C above is the **O(1) approximation** of least-connections. When you want the **exact** least-loaded endpoint -- and the weighted (SED) and worker-pool (NQ) variants -- M4 ships the IPVS `lc` / `sed` / `nq` cohort, made zero-GC. All three read your caller-owned `inflight` (and, for SED/NQ, `weights`) **live** -- no `setWeight`, no derived total, so you mutate the counters directly in your feedback loop ([ADR 0006](./decisions/0006-leastconn-family.md)).
108
+
109
+ ```js
110
+ import { LeastConnBalancer, SedBalancer, NqBalancer } from '@zakkster/lite-pick';
111
+
112
+ const eligible = Uint8Array.from([1, 1, 1, 1]);
113
+ const inflight = new Uint32Array(4); // YOU own this; increment on dispatch, decrement on settle
114
+
115
+ // LeastConn: the EXACT fewest-in-flight endpoint (O(cap) scan). In a feedback loop it is
116
+ // greedy-optimal -- load spreads within 1 of the mean (tighter than P2C's ln ln n gap).
117
+ const lc = new LeastConnBalancer(4, eligible, inflight);
118
+ const a = lc.pick(); inflight[a]++;
119
+
120
+ // SED (shortest-expected-delay): minimizes (inflight + 1) / weight -- higher weight absorbs
121
+ // proportionally more load. A weight-0 eligible node is never a candidate.
122
+ const weights = Uint32Array.from([1, 2, 3, 4]);
123
+ const sed = new SedBalancer(4, eligible, inflight, weights);
124
+ const b = sed.pick(); inflight[b]++;
125
+
126
+ // NQ (never-queue): jump to an IDLE endpoint (in-flight 0) the instant one exists, else SED.
127
+ // The best fit for a worker pool -- fill free workers before queueing anywhere.
128
+ const nq = new NqBalancer(4, eligible, inflight, weights);
129
+ const c = nq.pick(); inflight[c]++;
130
+ ```
131
+
132
+ The proof (from `test/balance.mjs`):
133
+
134
+ | strategy | claim | measured |
135
+ |---|---|---|
136
+ | **LeastConn** | exact greedy balance | **max-minus-min load <= 1** (peak/avg 1.00), tighter than P2C; peak <= P2C's on the same run |
137
+ | **SED** | load proportional to weight | **< 1% share drift** from each node's weight fraction; weighted-imbalance far below a random foil |
138
+ | **NQ** | never queue while idle | first *n* dispatches hit **n distinct idle workers**, then falls back to SED |
139
+
140
+ Each `pick()` is **0 B/op** and **O(cap)** (NQ is O(1) when an early node is idle). Because these are the state-heaviest strategies so far, M4 also introduces the **invariant fuzzer** (`npm run fuzz`): a seeded state-machine attack that, after *every* `pick` / `setEligible` / weight / load op, asserts the chosen endpoint is the *exact* optimum, `live` stays exact, and `PICK_NONE` holds *iff* nothing is pickable -- printing the seed on any failure for byte-for-byte replay.
141
+
105
142
  ## The substrate (under every strategy)
106
143
 
107
144
  ```js
@@ -127,10 +164,10 @@ rng.nextBelow(4); // -> a uint32 in [0, 4)
127
164
  rng.reset(); // replays the exact stream
128
165
 
129
166
  PICK_NONE; // -> -1 (fail-closed sentinel: no endpoint, never a dead pick)
130
- VERSION; // -> '0.3.0'
167
+ VERSION; // -> '0.4.0'
131
168
  ```
132
169
 
133
- `BalancerBase.pick()` is **abstract** -- it throws, so an unfinished strategy fails loudly rather than returning a dead index. Every shipped strategy (`RoundRobinBalancer`, `SmoothWRRBalancer`, `P2cBalancer`) extends it and reads the same shared eligibility view; you subclass it the same way to add your own.
170
+ `BalancerBase.pick()` is **abstract** -- it throws, so an unfinished strategy fails loudly rather than returning a dead index. Every shipped strategy (`RoundRobinBalancer`, `SmoothWRRBalancer`, `P2cBalancer`, `LeastConnBalancer`, `SedBalancer`, `NqBalancer`) extends it and reads the same shared eligibility view; you subclass it the same way to add your own.
134
171
 
135
172
  ## Design ownership (ratified before any strategy)
136
173
 
@@ -161,6 +198,7 @@ npm run torture # lite-leak retention + lite-gc-profiler 0 B/op (needs --expo
161
198
  npm run test:perf # lite-perf-gate HARD zero-alloc gate + a mustFail teeth-check
162
199
  npm run witness # pick throughput flatness across a pool-size sweep
163
200
  npm run balance # peak-to-average vs the strategy ceiling + random foil (the anchor)
201
+ npm run fuzz # seeded invariant fuzzer: state-sync invariants after every op
164
202
  npm run verify # all of the above
165
203
  ```
166
204
 
package/llms.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  # @zakkster/lite-pick
2
2
 
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  License: MIT (c) Zahary Shinikchiev <shinikchiev@yahoo.com>
5
5
  Runtime dependencies: none. ESM only. ASCII-only source. sideEffects: false.
6
6
  Node: >= 18.
@@ -14,12 +14,15 @@ reading pre-allocated views that siblings or the caller write, and returning an
14
14
  The complementary evidence lite-pick ships is a measured balance-quality anchor (peak-to-
15
15
  average load vs the strategy's theoretical ceiling) alongside the 0 B/op pick witness.
16
16
 
17
- 0.3.0 ships the substrate seams + three strategies: RoundRobin, SmoothWRR (the weighted
18
- default), and P2C (power-of-two-choices, the headline). It exports `VERSION`, the fail-closed
19
- sentinel `PICK_NONE` (-1), a deterministic `Prng` (xorshift32), `BalancerBase` (the shared
20
- read-only eligibility seam + O(1) live count), `RoundRobinBalancer`, `SmoothWRRBalancer`, and
21
- `P2cBalancer`. The remaining strategies land one per session (see ROADMAP.md): LeastConn/SED/NQ,
22
- PeakEWMA, ConsistentHash, BoundedLoad, WeightedRandom.
17
+ 0.4.0 ships the substrate seams + six strategies: RoundRobin, SmoothWRR (the weighted
18
+ default), P2C (power-of-two-choices -- also the O(1) least-connections APPROXIMATION), and the
19
+ EXACT LeastConn family (LeastConn, SED, NQ). It exports `VERSION`, the fail-closed sentinel
20
+ `PICK_NONE` (-1), a deterministic `Prng` (xorshift32), `BalancerBase` (the shared read-only
21
+ eligibility seam + O(1) live count), `RoundRobinBalancer`, `SmoothWRRBalancer`, `P2cBalancer`,
22
+ `LeastConnBalancer`, `SedBalancer`, and `NqBalancer`. The remaining strategies land one per
23
+ session (see ROADMAP.md): PeakEWMA, ConsistentHash, BoundedLoad, WeightedRandom. The EXACT-O(log
24
+ n) fewest-in-flight variant is a deferred @zakkster/lite-logn `BinaryHeap` optional-peer seam
25
+ (decisions/0006), not this exact-O(cap) scan.
23
26
 
24
27
  ## Design ownership (decisions/0001, 0002)
25
28
 
@@ -78,7 +81,31 @@ PeakEWMA, ConsistentHash, BoundedLoad, WeightedRandom.
78
81
  - `pick()` -> number. Draws two DISTINCT eligible endpoints uniformly at random (rejection
79
82
  sampling over the bitmap, no peer) and returns the one with the lower in-flight load; ties
80
83
  to the first draw. The `ln ln n / ln 2` peak-load ceiling (ADR 0005). O(1) per pick, 0 B/op.
81
- `PICK_NONE` when the whole pool is down.
84
+ `PICK_NONE` when the whole pool is down. This IS the O(1) least-connections APPROXIMATION;
85
+ `LeastConnBalancer` below is the exact-O(cap) complement.
86
+ - `LeastConnBalancer extends BalancerBase` -- class. EXACT fewest-in-flight (M4, IPVS `lc`).
87
+ - `new LeastConnBalancer(capacity, eligible, inflight)` -- `inflight` a caller-owned Uint32Array
88
+ (length >= capacity), read LIVE each pick. No `setWeight`, no derived aggregate: the caller may
89
+ mutate inflight directly between picks (that is the shared-counter seam).
90
+ - `pick()` -> number. Full O(cap) scan returning the eligible node with the fewest in-flight
91
+ requests (lowest index on a tie), or `PICK_NONE` when the whole pool is down. 0 B/op. In a
92
+ feedback loop (increment on dispatch, decrement on settle) it is greedy-optimal: max-minus-min
93
+ load stays within 1 (balance.mjs). Without feedback it returns the same lowest-load index --
94
+ correct by contract; the M5 lite-query adapter provides the increment/decrement.
95
+ - `SedBalancer extends BalancerBase` -- class. Shortest-expected-delay (M4, IPVS `sed`).
96
+ - `new SedBalancer(capacity, eligible, inflight, weights)` -- `inflight` AND `weights` are
97
+ caller-owned Uint32Arrays (length >= capacity), read LIVE (no `setWeight`, no derived total --
98
+ the caller may retune weights directly, UNLIKE SmoothWRR).
99
+ - `pick()` -> number. O(cap) scan returning the eligible, positive-weight node minimizing
100
+ `(inflight + 1) / weight` (the new request's marginal expected delay), lowest index on a tie,
101
+ or `PICK_NONE` when no eligible node has a positive weight. A weight-0 eligible node is NOT a
102
+ candidate. Converges to load proportional-to-weight (balance.mjs). 0 B/op.
103
+ - `NqBalancer extends BalancerBase` -- class. Never-queue (M4, IPVS `nq`); the worker-pool fit.
104
+ - `new NqBalancer(capacity, eligible, inflight, weights)` -- same caller-owned, read-live
105
+ inflight + weights contract as SED.
106
+ - `pick()` -> number. Returns the FIRST idle eligible positive-weight node (in-flight 0) if one
107
+ exists -- never queueing while a server is free -- else the SED minimum, else `PICK_NONE`.
108
+ O(cap) worst case, O(1) when an early node is idle. 0 B/op.
82
109
 
83
110
  ## Gates (every session)
84
111
 
@@ -88,6 +115,8 @@ PeakEWMA, ConsistentHash, BoundedLoad, WeightedRandom.
88
115
  lite-perf-gate `zgcSuite` HARD zero-alloc gate + a `mustFail` teeth-check.
89
116
  - `npm run witness` -- pick throughput flatness across a pool-size sweep.
90
117
  - `npm run balance` -- peak-to-average load vs the strategy ceiling + random foil (the anchor).
118
+ - `npm run fuzz` -- the seeded invariant fuzzer (the state-machine attack): strict-mode
119
+ state-synchronisation invariants after every op, per strategy; prints the seed on failure.
91
120
  - `npm test` / `npm run test:types` -- node:test boundary suite + tsc type-surface check.
92
121
 
93
122
  ## Composes with
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@zakkster/lite-pick",
3
3
  "author": "Zahary Shinikchiev <shinikchiev@yahoo.com>",
4
- "version": "0.3.0",
5
- "description": "Zero-dependency, zero-GC load-balancing selection kernel: one hot pick() -> endpoint index over a fixed pool, 0 B/op steady-state. A pure selector (consumes health/circuit state, never a proxy) for the in-process hop, complementary to AWS NLB/ALB. Tree-shakeable ESM roster: RoundRobin, SmoothWRR, P2C, least-conn, PeakEWMA, consistent hashing.",
4
+ "version": "0.4.0",
5
+ "description": "Zero-dependency, zero-GC load-balancing selection kernel: one hot pick() -> endpoint index over a fixed pool, 0 B/op steady-state. A pure selector (consumes health/circuit state, never a proxy) for the in-process hop, complementary to AWS NLB/ALB. Tree-shakeable ESM roster: RoundRobin, SmoothWRR, P2C, LeastConn, SED, NQ, plus PeakEWMA and consistent hashing.",
6
6
  "type": "module",
7
7
  "main": "./Pick.js",
8
8
  "module": "./Pick.js",
@@ -29,10 +29,11 @@
29
29
  "torture": "node --expose-gc test/torture.mjs",
30
30
  "witness": "node test/witness.mjs",
31
31
  "balance": "node test/balance.mjs",
32
+ "fuzz": "node test/fuzz.mjs",
32
33
  "test:perf": "node --expose-gc --max-semi-space-size=4 --test test/perf/PerfGate.test.mjs",
33
34
  "bench": "node benchmark/Matrix.mjs",
34
35
  "bench:report": "node benchmark/Matrix.mjs && node benchmark/Report.mjs",
35
- "verify": "npm test && npm run test:types && npm run torture && npm run witness && npm run balance && npm run test:perf"
36
+ "verify": "npm test && npm run test:types && npm run torture && npm run witness && npm run balance && npm run fuzz && npm run test:perf"
36
37
  },
37
38
  "keywords": [
38
39
  "load-balancer",