meshSeam

suspend fun meshSeam(selfId: PeerId, connections: List<Connection>, dispatcher: CoroutineContext, random: Random = Random.Default): Mesh

Build a fully-connected N-peer mesh Seam from a set of raw point-to-point connections.

For each Connection in connections, meshSeam exchanges a MeshHello preamble (this peer's id plus a random per-connection nonce) to learn the remote PeerId. Both sides of a link exchange their preamble concurrently, so all exchanges in connections run in parallel — this function suspends until every handshake completes.

Dedup (cross-node agreement): if two connections resolve the same remote id (duplicate links from a simultaneous dial), both ends keep the link with the lexicographically smallest link nonce — a canonical, order-independent function of the two per-connection nonces. Because both ends see both nonces, they derive the same survivor and close the same loser, with no coordination. The old self-relative selfId < remoteId rule could leave a link half-open (the two ends disagreed on the survivor); the nonce-based rule cannot.

Per-link failure: if a link's remote peer disconnects or errors, that peer is removed from Seam.peers and the mesh continues operating. The seam remains SeamState.Woven until Seam.close is called.

Dynamic join: admit a link that arrives later via Mesh.addLink.

Parameters

selfId

This peer's identity. Sent in the MeshHello preamble on each conn.

connections

Raw Connections to each prospective peer. These must be fresh and unread. Each is wrapped with singleCollection before reading, so the preamble read and the per-link read loop share ONE collection of Connection.incoming — a cold, single-collection connection (a stream fabric's framed()) works as well as a hot channel-backed one (us.tractat.kuilt.test.fabric.connectionPair).

dispatcher

The scope for the per-link readLoop coroutines (scheduling only — see the thread-safety note on the returned seam). Production callers pass Dispatchers.Default; test callers pass a dispatcher derived from the test scheduler so seam internals share the same virtual clock as the test's withTimeout.

random

Source of per-connection nonces. Production defaults to Random.Default; tests pass a seeded Random so the dedup tiebreak is deterministic.