On this page

Clusters unique per cluster๏ƒ

๐ŸŸก Intermediate ยท Lesson 25

Clusters - named groups of dynamic conduits that AUTO-SHARE cluster-scoped spells. unique_per_conduit_cluster finally comes alive: one instance per cluster, shared by every member. (This completes the declaration parked in the expert tier's example 01.)

Before you run๏ƒ

Use the Intermediate guide for prerequisite concepts. Run from a checkout with Melder installed and Python 3.14 free-threading selected. The collection download includes the level's local helper modules.

Run the saved script๏ƒ

python UX_and_AIX_experiences/02_intermediate/25_clusters_unique_per_cluster.py
py -3.14t UX_and_AIX_experiences/02_intermediate/25_clusters_unique_per_cluster.py

Download this collection ยท Source on GitHub

Public surface๏ƒ

create_cluster, add_conduit_to_cluster, existence="unique_per_conduit_cluster"

Code๏ƒ

 1"""
 2TIER: intermediate (25)
 3GOAL: Clusters - named groups of dynamic conduits that AUTO-SHARE
 4      cluster-scoped spells. unique_per_conduit_cluster finally comes
 5      alive: one instance per cluster, shared by every member. (This
 6      completes the declaration parked in the expert tier's example 01.)
 7SURFACE EXERCISED: create_cluster, add_conduit_to_cluster,
 8                   existence="unique_per_conduit_cluster"
 9"""
10import sys
11from pathlib import Path
12
13sys.path.insert(0, str(Path(__file__).parent))  # local helper (see _dynamic_world)
14from _dynamic_world import dynamic_spellbook
15
16import melder as md
17
18
19class ClusterBus:
20    pass
21
22
23def main() -> None:
24    """Build a linked cluster and assert that its members resolve the same bus."""
25    owner_book = dynamic_spellbook()
26    # permissions="create" matters: cluster auto-sharing contracts the
27    # spell to each joining member WITH the spell's own permissions.
28    owner_book.bind(spell=ClusterBus,
29                    existence="unique_per_conduit_cluster",
30                    permissions="create")
31    owner = owner_book.conjure(dynamic=True, name="cluster-owner")
32    member = dynamic_spellbook().conjure(dynamic=True, name="cluster-member")
33
34    # Contracts ride LINKS: the cluster auto-share adds spells into the
35    # members' existing contracts, and the contract bucket is born at
36    # link(). Unlinked members share NOTHING.
37    owner.link(member)
38
39    cloud = owner.get_conduit_cloud()
40    cloud.create_cluster("workers")
41    cloud.add_conduit_to_cluster(owner, "workers")
42
43    # ELECT THE LEADER before more members join: the cluster's shared
44    # store belongs to the elected leader, and joins auto-share against
45    # the armed cluster - then the rest falls into place.
46    cloud.get_cluster("workers").elect_leader(owner.id)
47
48    cloud.add_conduit_to_cluster(member, "workers")
49
50    bus_a = owner.meld(spell=ClusterBus)
51    bus_b = member.meld(spell=ClusterBus)
52    assert bus_a is bus_b, "cluster members must share the elected leader's bus"
53    print("one bus per cluster, shared by members:", bus_a is bus_b)
54
55
56if __name__ == "__main__":
57    main()

Check the outcome๏ƒ

The script contains its own assertions or demonstrated refusal paths. Run it to evaluate those checks against your installed version. The code above is taken directly from the saved file; no run output is invented here.

More intermediate examples ยท Level guide