Plan: the hub election prefers the newest pi-remote

Closes: #856. Relates to: #186 (the election), #589 (the hub’s version, told to members), #611 (a session with no UI never holds the link). Branch: feat/election-newest-first. Status: Done (2026-11-18) — as planned

Erratum (2026-11-18): the measured rows, real pi processes on the Windows box, a scratch agent directory, ten rounds an arm. Hub on this build, a member on the released 0.166.1 attached first and a member on this build second: the newer member held the link 10 of 10, and the stop notice named it. Control, both members on the released package: the first-attached 10 of 10, under a new hub and under a released one. A released hub joined by two members on this build stayed the hub in 3 of 3 rounds, with no election event. The older-hub notice was not shown in that row: the two builds report the same pi-remote version. Not run: three or more members, a member with no UI, a hub that dies without stopping, the newer member attaching first (the last two are covered in the suite only). The Windows suite had one failure, in a property test of attachment names that fails on main too (#857).

Context

One pi session per box holds the relay link (the hub); the others attach to it as members. When the hub exits, each member waits by its place in the roster the hub last sent and then tries the lock: "the oldest survivor tries at once" (electionDelay, rank() in packages/pi-remote/member.ts; the roster’s members is the member ids ascending, memberIds() in packages/pi-remote/hub.ts).

The oldest survivor is the session most likely to predate an update. Seen twice on panther: after sessions were restarted onto a new release the link came to rest on the one session not restarted, first a session three days old on pi-remote 0.27.18, then one on 0.28.0 with 0.30.0 installed, after moving eight times in ninety seconds. Whatever runs in the hub (delivery, replay, the checks a post’s result reports) then runs at the oldest version on the box, and a fix does not take effect there until the last old session is restarted.

The operator’s decision (2026-11-18): the election prefers the newest version; a running hub is never unseated. Handing the link from a running hub to a newer session that joins was considered and declined: it would put every new release in charge of a box at its first restart, and a fault in the hub would then spread as fast as a fix.

Two facts that shape the design:

  • A member computes its rank as the index of its own id in the roster’s members array and reads that array for nothing else (member.ts, onRoster and rank()).

  • The hub’s check of an attach (attachProblem in hub.ts) names the fields it requires and ignores any other, so a member may send a field an older hub does not know.

Design

D1. A member says its version

The attach line (ToHub, t: "attach") gains an optional version: the member’s pi-remote version, PI_REMOTE_VERSION, as the hub already tells members its own in attached (#589). MemberDeps gains version?: string | null: absent, the member sends PI_REMOTE_VERSION; a string, that string; null, no version field at all, which is how a test plays a member from before this change. The session’s seam for it is the one attachTimeoutMs already uses: RemoteDeps.memberVersion (index.ts) to RemoteState.memberVersion (state.ts) to memberClient (lifecycle.ts), so a test of whole sessions can start a member at a chosen version or with none. attachProblem refuses a version that is present and not a string, as it does the other optional fields. The hub keeps it on the member, cut to 32 characters. The protocol number (HUB_PROTOCOL) does not change: the field is optional both ways.

D2. The hub orders the roster for the election

A new HubServer.electionOrder(): SessionInfo[] returns the members' session infos (each has session, the id, dir and headless) newest version first, then lowest id (the oldest) first. A member that gave no version, or one that does not parse as numbers and dots, comes after every member that gave one. publishRoster sends members as the ids of that order.

memberIds() goes. Its other callers are ten test assertions (test/hub.test.ts, nine; test/member.test.ts, one) that ask which members are attached; each becomes sessions().map((s) ⇒ s.session), which is the same ids ascending, so none of them changes what it asserts.

So the rule is the hub’s alone and the members are unchanged: each still waits by its index in members. A member running an older pi-remote under a new hub therefore follows the new order too, with no change to it.

The comparison is versionOlder, today in lifecycle.ts. It moves to a new packages/pi-remote/version.ts, since hub.ts cannot import lifecycle.ts (which imports it); lifecycle.ts, index.ts and the test that names it import it from there.

D3. The named successor

takeDown (lifecycle.ts) names "the member expected to take the link" as sessions()[0], the oldest. It becomes the dir of the first entry of electionOrder() whose headless is not true, since a session without one never takes the lock (#611). sessions(), which the phone’s list reads, keeps its order.

D4. What does not change

A hub that is running stays the hub whatever attaches to it. The election’s timing (150 ms a rank, capped at 1.5 s), the lock, and the headless rule are as they are. A headless member keeps its place in the order and so its 150 ms slot, as now.

The order is the dying hub’s. A box whose hub runs a pi-remote from before this change elects as before, oldest first; the new order applies from the first election run by a hub that has it.

Scope

In: packages/pi-remote/ hub.ts, member.ts, lifecycle.ts, index.ts, version.ts (new), their tests; docs/modules/ROOT/pages/remote.adoc; CHANGELOG.adoc.

Out: handing the link from a running hub; any change to the notice a newer member shows under an older hub; ranking headless members last; pivot-wire and the phone.

Risks

Risk Mode Mitigation

A release with a fault in its hub wins every election on a box

silent

Accepted by the operator with the alternative weighed: a running hub is never unseated, so the fault spreads only as hubs exit. The release rehearsal runs a hub; the older session’s notice already says which version holds the link.

A member that sends no version (older code) never wins while a newer one is present

silent

Intended. It is ranked last, not excluded: with no newer member it wins as before. Pinned by the regression case with no versions.

A version string that is malformed or hostile

fail-closed

Refused when not a string; cut to 32 characters; one that does not parse ranks with the members that gave none. The attach is already gated by the box’s token. Pinned by unit cases.

Something reads members as ids ascending

fail-closed

In shipped code only rank() reads it (D2); memberIds() is removed, so a caller that wanted ids ascending fails to compile, and the ten test assertions move to sessions(), which keeps that order.

The first update after this ships still lands on old code

silent

Stated in the docs and CHANGELOG (D4): the order is the dying hub’s, so it takes one restart of the old hub.

Tests

Unit (packages/pi-remote/test/hub.test.ts):

  • electionOrder: newest version first; equal versions oldest first; no version and an unparseable one after every parseable one, oldest first among themselves; 0.9.0 before 0.10.0 is wrong, so 0.10.0 first (numeric, not text).

  • The roster a member receives carries members in that order, and changes when a newer member attaches.

  • attachProblem: a version that is not a string is refused; an absent one is accepted; a long one is kept cut.

Contract (packages/pi-remote/test/member.test.ts): the attach line a member writes carries its version; a member given a roster in election order reports the rank of its place in it, for the first, a middle and the last place.

Concurrency (packages/pi-remote/test/hub-e2e.test.ts, real sockets): a hub with an older member attached first and a newer member attached second exits; the newer member becomes the hub and the older one attaches to it. The same with the versions swapped: the first-attached, newer one wins.

Pins "a running hub is never unseated" (same file): a hub playing an older version, with a newer member attaching to it, stays the hub: the member’s role is member, no election starts on either side, and the hub’s lock is unchanged; the newer member shows the older-hub notice as today.

Regression (same file): two members that give no version (memberVersion: null) elect as before, the first-attached one winning. takeDown names the newer member’s directory as the successor, and skips a headless one.

Unit (packages/pi-remote/test/attach-notice.test.ts): versionOlder’s cases, imported from `version.ts, unchanged.

Measured before merge, prediction on file first, by the Windows seat on panther with real processes: a hub on this build, one member on the released package and one on this build, the older one attached first; the hub is stopped; which takes the link, ten rounds, with the control arm of both members on the released package (the first-attached should win).

Delivery

  1. Plan committed in docs/modules/ROOT/pages/plans/election-newest-first.adoc with its nav.adoc line.

  2. One branch: code and tests; `remote.adoc’s Handover section (step one, and the sentence in the headless paragraph about an older pi-remote keeping the box); CHANGELOG under Unreleased, Changed.

  3. node tools/task.mjs validate, full consumer-sim, the Windows suite and the measured rows; MR closing #856, squash off, the merge SHA on the issue.

Edit this page · latest