On this page

Several rooms, explicit shared worlds๏ƒ

Prerequisites: room setup and a free-threaded Python runtime. The concurrency example opens a private room for each worker and points the rooms at one shared target world. Each room owns its workstation and memory subscriptions.

Check results across the boundary๏ƒ

Generated code places its answer in result; the execution payload carries that answer alongside acceptance and error information. Expert 21 starts with a single sum, then launches four coordinated workers and checks the exact results: 5050, 9, "0-1-4-9-16", and 5040.

It also checks that the workstations have distinct identities. This demonstrates private working spaces with a shared runtime target. It is not a timing benchmark and does not establish a universal absence of contention.

Match coordination to the operation๏ƒ

Computing an answer and changing topology have different coordination needs. Continue to the admission and gate guide for structural changes and shutdown. Use multiple-world targeting when workers should operate on different runtime worlds rather than a shared one.

The workflow in code๏ƒ

These are the core steps from the saved example. Use its complete linked script for all class definitions and setup.

  1def main() -> None:
  2    # THE SHARED WORLD. One frame, postured for codegen (expert 11).
  3    book = md.Spellbook(aetheric_frame=FRAME)
  4    book.bind(spell=Meter, existence="unique", binding_name="factory-meter")
  5    book.configure_aether_frame(
  6        system_state="dynamic",
  7        disposal=None,
  8        disposal_method_names=None,
  9        rift_enabled=True,
 10        ai_native=True,
 11    )
 12    book.conjure(name="factory-root")
 13
 14    nexus = md.Nexus()
 15    system_configuration = nexus.create_configuration()
 16    system_configuration.with_rift_creation_enabled(True)
 17    system_configuration.with_allowed_target_frame_names([FRAME])
 18    system_configuration.with_multiple_target_frames(True)
 19    system_configuration.with_max_target_frame_count(4)
 20    nexus.activate(system_configuration)
 21    print("one shared world:", FRAME)
 22
 23    # ONE AGENT FIRST, SLOWLY, SO THE PAYLOAD IS VISIBLE.
 24    solo = _open_room(nexus, "solo")
 25    verdict = solo.command_system.validate_codegen(
 26        JOBS["adder"], frame_name=FRAME,
 27    )
 28    print()
 29    print("validate ->", verdict)
 30    assert verdict["accepted"] is True
 31
 32    payload = solo.command_system.execute_codegen(
 33        JOBS["adder"], frame_name=FRAME,
 34    )
 35    print("execute  -> keys:", sorted(payload))
 36    assert payload["accepted"] is True
 37    assert payload["frame_name"] == FRAME
 38    assert payload["result"] == 5050
 39    print("execute  -> result:", payload["result"])
 40    print("  the code set `result`; the executor lifted THAT ONE NAME out")
 41    print("  everything else it computed died with the sandbox")
 42
 43    # NOW FOUR AGENTS AT ONCE, EACH IN ITS OWN ROOM.
 44    rooms = {}
 45    outcomes = {}
 46    errors = []
 47    guard = threading.Lock()
 48    ready = threading.Barrier(len(JOBS))
 49
 50    def run_agent(agent_name: str) -> None:
 51        try:
 52            room = _open_room(nexus, agent_name)
 53            with guard:
 54                rooms[agent_name] = room
 55            # Line them up so the executions genuinely overlap.
 56            ready.wait(timeout=10)
 57            result = room.command_system.execute_codegen(
 58                JOBS[agent_name], frame_name=FRAME,
 59            )
 60            with guard:
 61                outcomes[agent_name] = result
 62        except Exception as error:  # noqa: BLE001 - reported, not swallowed
 63            with guard:
 64                errors.append((agent_name, repr(error)))
 65
 66    threads = [
 67        threading.Thread(target=run_agent, args=(name,), name=f"agent-{name}")
 68        for name in JOBS
 69    ]
 70    for thread in threads:
 71        thread.start()
 72    for thread in threads:
 73        thread.join(timeout=30)
 74
 75    assert not errors, f"agent failures: {errors}"
 76    assert len(outcomes) == len(JOBS)
 77    print()
 78    print("four agents ran their own code concurrently:")
 79    for agent_name in sorted(outcomes):
 80        result = outcomes[agent_name]
 81        assert result["accepted"] is True
 82        assert result["result"] == EXPECTED[agent_name]
 83        print(f"   {agent_name:<8} -> {result['result']!r}")
 84    print("  none of them waited on another - computing a value is not")
 85    print("  a structural change, so there was nothing to serialize")
 86
 87    # PRIVATE BENCHES. Four rooms, four workstations, four ids.
 88    workstation_ids = {name: room.workstation.workstation_id
 89                       for name, room in rooms.items()}
 90    assert len(set(workstation_ids.values())) == len(rooms)
 91    print()
 92    print("four rooms ->", len(set(workstation_ids.values())),
 93          "distinct workstations")
 94    print("  private bench each, one shared world - that is the isolation")
 95
 96    # AND A ROOM'S MEMORY IS ITS OWN. Subscribe on one; run on two.
 97    watcher = rooms[sorted(rooms)[0]]
 98    other = rooms[sorted(rooms)[1]]
 99    seen = []
100    subscription = watcher.memory_system.register_memory_callback(seen.append)
101    assert watcher.memory_system.memory_enabled is True
102
103    watcher.command_system.execute_codegen(JOBS["adder"], frame_name=FRAME)
104    other.command_system.execute_codegen(JOBS["adder"], frame_name=FRAME)
105
106    print()
107    print("subscribed to ONE room, then ran in two:")
108    print("   records captured:", len(seen))
109    print("  the other room's identical call is absent - per-agent audit")
110    print("  falls out of per-agent rooms, nobody had to build it")
111    watcher.memory_system.unregister_memory_callback(subscription)
112
113    print()
114    print("four agents, four benches, one world, no ceremony")
115    print("`result` is the whole boundary: one value out, nothing leaks")

Runnable examples๏ƒ

All expert examples ยท Level contents ยท Full contents

Canonical page source