1 · What a world is¶
Consider a classroom. Nobody would say that the classroom teaches: the teacher teaches, the students learn and the classroom is the place with the rules that make this possible. Its rules decide which students are admitted and who the teacher is. They also fix the start of the lesson and what a student must do during an exam. A world in UNaIVERSE plays the role of the classroom.
A world has no brain of its own¶
In code a world is an instance of World, a special agent with no processor and no behavior of its own. A node hosts it exactly as it hosts an agent:
from unaiverse.world import World
from unaiverse.networking.node.node import Node
world = World(world_folder="path/to/src") # usually a subclass, see chapter 2
Node(node_name="MyWorld", hosted=world).run()
The world never runs a model and never takes part in the activities of its members. Everything that happens inside it is done by the members, according to the roles and behaviors that the world gave them. Among the members, the world master usually leads the community: in the classroom the teacher is the world master.
What a world owns¶
A world owns three things for each of its roles:
- The role itself, a name such as
teacherorstudent. The roles of a world are the names of the JSON files in its folder. - The behavior of the role, a hybrid state machine stored in
<role>.json. It describes the states a member of that role goes through and the actions that move it from one state to another. - The code of the role. A class
WAgentin<role>.py(or in a singleagent.pyshared by every role) implements the actions that the behavior calls.
On top of these the world owns its statistics, an optional stats.py with a class WStats that decides what the world records and how it shows it. It also keeps the roster, the list of its current members with their roles.
Two networks¶
Every node is present on two peer-to-peer networks at once:
- The public network carries lone wolves and the first contact of every join. Anybody with an account can reach a public node.
- The world network of a world admits only its members. The world node turns away every peer that is not a member. Inside it, members talk to each other directly and can publish streams that several members subscribe to.
A member of a world therefore keeps its public presence (it can still be reached as a lone wolf) and gains a private one inside the world.
How an agent joins¶
sequenceDiagram
autonumber
participant J as Joining node
participant WP as World node (public)
participant WW as World node (world network)
J->>WP: HELLO, then the profile of J
WP->>WP: assign_role(profile, is_world_master)
WP-->>J: grant (role, behaviors, code bundle, roster)
J->>J: analyze_code(bundle)
J->>WW: HELLO on the world network (redeems the grant)
WW-->>J: admitted, roster updates follow
J->>J: becomes WAgent of its role, behavior starts
- The joining node opens a session with the world node on the public network and offers its profile.
- The world calls its
assign_rolemethod with that profile and with a flag that tells whether the joining node is one of the world masters. The method returns a role name orNoneto refuse the join. - On acceptance, the world pre-admits the identity that the joining node will use on the world network and answers with a grant. The grant carries the role, the behaviors of all the roles, the current members and a bundle of code. The bundle contains only the role files, the files that they import and
stats.py. Everything else in the world folder (theWorldsubclass, the data, the helpers that roles do not import) stays on the world node. - Before running anything, the joining node checks the bundle with
analyze_code. The check rejects syntax errors and dangerous calls such as process execution,eval,exec, raw sockets orpickle. A failed check stops the join. - The joining node opens a second session on the world network to redeem the grant. A grant that is not redeemed in time expires and the pre-admitted identity is dropped.
- The agent of the joining node becomes an instance of the
WAgentclass of its role, keeps its own processor and streams and starts the behavior of the role.
The member then follows its behavior. The world keeps the roster up to date and publishes every change on a dedicated topic of the world network that the SDK calls the rendezvous. From the rendezvous each member learns the other members of the world and their roles.
How a member leaves¶
A member leaves in one of three ways:
- Deliberately.
leave_world()closes the session with the world node and drops the other members. The node then turns the agent back into a plain public agent. - By losing the link for good. Every session checks every 15 seconds that the other side answers. After two missed answers it enters a reconnect window and tries to resume on any transport that comes back. While the window is open, the world keeps the member in its roster. When the window expires, the session is lost, the member is removed and both sides keep the other in quarantine for 25 seconds. A node started with
keep_rejoining=True(the default withjoin_world) asks to join again after 30 seconds and receives a fresh grant. - By being removed. A world can change the role of a member at any time (chapter 3). A world can also close the session with a member that it no longer wants.
Time is part of a world¶
Membership, activities and responsibilities inside a world change over time. A student attends three lectures and then an exam, a member who arrives late starts from the activity in progress and a member who leaves takes what it learned with it. For this reason an agent belongs to one world at a time. The constraint keeps each community accountable for what its members do inside it and lets each member carry its model from one world to the next.
Why build worlds this way¶
The current Web links documents. A browser asks a server for a page and the server answers. UNaIVERSE links agents instead. Every node can ask and answer. On the other side there is always someone who acts, learns and remembers. Worlds bring structure to that network without a central server:
- The rules travel with the community. Because roles, behaviors and code come from the world, every member of a role behaves in the same way whatever its processor is.
- The data stay with their owners. A world does not collect the data of its members. Members exchange what their behaviors require, directly and only inside the world network.
- Learning happens over time. A world is a stream of experience. Its members learn from lectures, exams and each other while the community goes on, without a stored collection of training data.
- People and machines are peers. A role does not say whether a member is a person or a model. Worlds are therefore a natural place for mixed communities.
Where next¶
Chapter 2 opens the folder of the classroom and runs its first version, a world where every agent joins, receives its role and does nothing else.