3 · Roles and the world master¶
In the classroom of chapter 2 one agent became the teacher and all the others became students. Two decisions produced that result: the owner of the world declared which node is a world master and the world turned that status into the role teacher in assign_role. Both decisions belong to the world and the members cannot change them. The second stage of the classroom (tutorial_steps/step2) adds a third piece: what the teacher does as soon as it receives its role.
Assigning a role¶
Every join request reaches assign_role(profile, is_world_master) on the world node. The method returns the name of one of the roles of the world or None to refuse the request. It runs once per join before the grant and can read anything in the profile of the joining node.
The CoLLAs classroom uses the master status alone:
The social learning world of the examples (unaiverse-examples/worlds/social_learning/src/world.py) combines the master status with the role that the joiner asks for. Every extra keyword argument of node.run(join_world=..., ...) reaches the world as an entry tmp_<option> of the dynamic profile. The world reads that entry in assign_role and drops it right after:
def assign_role(self, profile, is_world_master):
if is_world_master:
return "teacher" if len(self.world_masters) <= 1 else "student"
preference = profile.get_dynamic_profile().get("tmp_role_preference")
return "student_isolated" if preference == "student_isolated" else "student"
A joiner then asks for a role with node.run(join_world="DigitSocialLearning", role_preference="student_isolated"). The world stays in charge: a preference that it does not read has no effect.
The default assign_role of World returns the built-in role world_master for a master and world_agent for everybody else. A real world overrides it to return its own roles.
Roles and their bits¶
The SDK represents a role as an integer. The two lowest bits tell whether the agent is in a world and whether it is a master:
| Built-in role | Value | Meaning |
|---|---|---|
public_agent |
0 |
A lone wolf on the public network and in no world. |
world_agent |
1 |
A member of a world. |
world_master |
3 |
A member of a world that is also one of its world masters. |
The custom roles of the world take the higher bits in the alphabetical order of their JSON files. In the classroom student is 4 and teacher is 8. The full role of a member combines the two parts. A world master that is a teacher has the role world_master~teacher (value 11) and a student that is not a master has the role world_agent~student (value 5). A world can have up to 30 custom roles.
Most code never touches these numbers. get_agents_by_role("student") returns the peer identifiers of the members whose custom role is student regardless of their master bit. By default it lists only the members whose handshake is complete.
The world masters¶
A world master is a member that the owner of the world trusts to lead it. The owner declares the masters when starting the world node:
Node(node_name="CoLLAsTutorial", hosted=world,
world_masters_node_ids=["<node id of the teacher>"]) # preferred
# or
Node(node_name="CoLLAsTutorial", hosted=world,
world_masters_node_names=["CoLLAsTeacher"]) # names of your account
Node identifiers never change and do not depend on the account that owns the node. They are therefore the preferred form. Names are convenient during development: the node converts them to identifiers of nodes of your account when it starts. At startup the node also loads the masters configured in the online profile of the world node.
The master status says nothing about what a master does. In the classroom the master teaches. In a hotel it could be the manager and in a competition the judge. A world can have several masters and its assign_role decides what each of them becomes. The social learning world, for example, makes only the first master a teacher.
allowed_node_ids restricts the world to a list of nodes. When the list is present, only those nodes can join. Like the masters, the list can come from the online profile of the world.
When an agent receives its role¶
Every agent runs accept_new_role(role) when the world assigns or changes its role. The base implementation records the role in the profile and loads the behavior of the role from the grant. A role class can extend it to prepare whatever the role needs. In the second stage of the classroom the teacher creates its data streams there:
import os
from collections import defaultdict
from unaiverse.agent import Agent
from unaiverse.streams import Stream, ImageFileStream, StringStream
class WAgent(Agent):
LECTURE_DELTA = 7.0 # seconds between two consecutive lecture samples
EXAM_DELTA = 10.0 # seconds between two consecutive exam samples
FEEDBACK_DELTA = EXAM_DELTA # the same for the feedback requests
def accept_new_role(self, role: int):
super().accept_new_role(role)
data_path = os.environ["TEACHER_DATA_PATH"]
for i in range(1, 4): # three lectures, data/lectures/1 to 3
folder = os.path.join(data_path, "lectures", str(i))
# helper defined in the full file: "012_Empoleon.jpg" -> class "Empoleon"
file_names, class_names = collect_file_names_and_class_names(folder)
self.add_streams([
Stream.create(stream=ImageFileStream(folder, file_names, circular=True),
group=f"lecture{i}", name="images",
pubsub=False, public=False, delta=self.LECTURE_DELTA),
Stream.create(stream=StringStream(class_names, circular=True),
group=f"lecture{i}", name="class_names",
pubsub=False, public=False, delta=self.LECTURE_DELTA)])
# ... the same for the groups "exam" and "feedback"
# Publish the new streams in the profile, so that connecting agents can see them
self.update_streams_in_profile()
Each lecture becomes a group of two streams: one of pictures and one of class names, read in a loop from the files of the lecture. The arguments of Stream.create decide how the stream travels:
groupandnameidentify the stream. Behaviors and actions refer to it as"images@lecture1".pubsub=Falsesends the samples directly to the members that ask for them, instead of publishing them on a topic of the world network.public=Falsekeeps the stream inside the world network.deltais the number of seconds between two consecutive samples. A lecture shows a new picture every 7 seconds.
update_streams_in_profile() publishes the new streams in the profile of the teacher. Other members see them when they connect. Data streams describes the stream classes and every option of Stream.create.
Since the role code runs on the machine of the member, accept_new_role is the right place for anything local: files, devices, credentials. The data folder of the teacher is on the machine of the teacher and the code reads it from an environment variable. The world never sees the pictures until the teacher streams them.
Changing a role at runtime¶
A role is not permanent. The world can change the role of a member at any time with await self.set_role(peer_id, role). Its role argument is the integer of the new custom role. The world keeps the master bits of the member and sends the new role together with the behavior of that role. The node of the member replaces its agent with an instance of the class of the new role. The new instance runs accept_new_role and starts the new behavior.
A world master can ask the world for such a change with the built-in action suggest_role_to_world(agent, role) that takes the role by name. The world applies the suggestions of its masters and ignores those of other members. A promotion is a typical case: in a teaching world, a student that passes every exam can become a teacher for the newcomers.
Run it¶
Run the world, the teacher and the students of tutorial_steps/step2. The visible behavior is the same as in step 1. The teacher, however, now owns ten streams in five groups and they appear in its profile.
What just happened¶
assign_role turned the master status into the role teacher. The teacher received the role world_master~teacher (value 11) and ran accept_new_role. Its override read the data folder and created one group of streams for each lecture, one for the exam and one for the feedback requests. It then published the streams in its profile. The students ran the base accept_new_role and loaded the behavior of their role.
Where next¶
The roles exist and the teacher has its material. Chapter 4 gives both roles a behavior.