Remote modules: programs on other machines joining a Psyclone system

Remote modules: any program, any language, a full Psyclone module

A Psyclone system is built from modules. Each module has triggers that fire it, posts that it sends, and whiteboards and catalogs it reads from. Until 2.3.2 every module lived inside the system: as a C++ or Python crank, or as an external C++ program that shares memory with a node on the same machine.

That leaves out a lot of real programs: a speech recogniser on a GPU box, a Java service your team already runs, a script on a laptop. Remote modules bring them in. A program on any machine, in any language with a DataMessage codec, attaches to the system over the network and from then on is a module.

What a remote module can do 🧩

Once attached, a remote module does what a local module does: – Triggers: its triggers fire it, by trigger name. – Posts: it posts by post name, and the spec decides the message type. – Retrieves: it retrieves from whiteboards with the named <retrieve>s in its spec. One retrieve returns at most maxretrieve messages (100 by default). – Queries: it queries catalogs with its named <query>s. – Parameters: they work in both directions. A PsyProbe edit reaches the program, and the program can set its own. – PsyProbe control: stop, start and restart reach it. PsyProbe 2 shows its session in the component inspector: state, transport, peer, sequence numbers, and what was sent, queued or dropped.

Inside Psyclone the module is an ordinary component without a crank. What it posts comes from the module, so from="Asr" filters and whiteboard origins work as they do for any other module.

The spec: a host and a module πŸ“„

A remote module rides on a service. A <service> with a <modules> element is a module host, and its interfaces are the transports the module can use: TCP Message, WebSocket and, if you allow it, UDP.

<service name="Modules">
  <modules auth="key" lease="30000" takeover="device" maxretrieve="100"/>
</service>
<interface name="ModTCP" port="18700" protocol="Message" service="Modules" ssl="yes"/>
<interface name="ModWS"  port="18701" protocol="HTTP"    service="Modules" ssl="yes"/>

<!-- the spec is the whole contract: a process only attaches to it -->
<module name="Asr" type="remote" service="Modules">
  <trigger name="Audio" type="Voice.Audio.Frame"/>
  <trigger name="Go" type="Psyclone.Ready"/>
  <post name="Text" type="Voice.Asr.Text"/>
  <retrieve name="Recent" source="ConvWB" type="Voice.Asr.Text" maxcount="10"/>
  <query name="Notes" source="NoteStore"/>
  <parameter name="Language" value="en-GB"/>
</module>

The module and its host service must be on the same node, so give the <module> the service’s node="..." in a multi-node system. Modules on other nodes trigger on it and post to it as usual.

The program: Python 🐍

from psyclone_client import PsycloneClient, RemoteModule, Auth, DataMessage

psy = PsycloneClient.tcp("host", 18700, "asr-box-1")                   # host, port, device id
asr = RemoteModule.attach(psy, "Asr", Auth.key("psy_<id>_<secret>"))   # or Auth.token(t), Auth.none()
print(asr.posts, asr.triggers)                                         # the contract, from the server

asr.on_trigger("Audio", lambda m: asr.post("Text", DataMessage().set_string("Text", recognise(m))))
recent = asr.retrieve("Recent", max_count=5)                           # list of DataMessage
notes = asr.query("Notes", DataMessage().set_string("List", "1"))     # the catalog's reply
asr.on_stop(lambda reason: pause())
asr.on_start(lambda reason: resume())
asr.on_dropped(lambda count, from_seq, reason: print("missed", count, "from", from_seq, reason))

The program can also pull messages crank-style, with asr.wait_for_new_message(1.0) and asr.current_trigger_name. asr.close() detaches cleanly.

The program: Kotlin β˜•

import cmlabs.cmsdk.DataMessage
import cmlabs.psyclone.client.PsycloneClient
import cmlabs.psyclone.client.RemoteModule
import java.util.concurrent.Executors

val psy = PsycloneClient.tcp("host", 18700, device = "asr-box-1",
    options = PsycloneClient.Options().callbackExecutor(Executors.newSingleThreadExecutor()))
val asr = RemoteModule.attach(psy, "Asr", RemoteModule.Auth.key("psy_<id>_<secret>"))
asr.onTrigger("Audio") { msg -> asr.post("Text", DataMessage().setString("Text", recognise(msg))) }
val recent: List<DataMessage> = asr.retrieve("Recent", maxCount = 5)
asr.onStop { reason -> pause() }
asr.onStart { reason -> resume() }
asr.onDropped { count, fromSeq, reason -> log("missed $count from $fromSeq ($reason)") }

retrieve, query and describe wait for the host’s reply. Call them from a handler only when the client has a callbackExecutor, as above. Without one, handlers run on the thread that delivers the replies.

A refusal throws RemoteModuleException in Kotlin and raises RemoteModuleError in Python. The error carries the host’s status: denied, unknown, busy, timeout or unsupported.

Who may attach πŸ”

<modules auth=...> picks one of three modes: – key (the default): a PsyProbe 2 API key whose account has the permission modules.attach.<Module>, for example modules.attach.Asr or modules.attach.*. The secret never travels. The program asks for a nonce and sends an HMAC-SHA256 proof over the module name, its device and the nonce. – token: a shared secret of 8 characters or more. Put it in a %variable%, not in the spec file. – none: accepted from the host’s own machine only, checked on every attach. Psyclone logs a warning at start-up for each interface of a none host, because interfaces listen on every address.

Module sessions over UDP need udp="yes". The session token then travels in clear, so use UDP on trusted networks only.

Built for real networks πŸ“Ά

  • Lease: a session that the host hears nothing from for lease ms (30 s by default) goes offline. Client keepalives count, so an idle but healthy module stays online. The host posts Psyclone.RemoteModule.Online on every attach and Psyclone.RemoteModule.Offline when a session ends.
  • Resume: when the link drops, the host keeps the last buffer trigger messages (256 by default), each for bufferage ms (5 s). The client attaches again with its old session and the last sequence number it saw. The host replays everything newer. Anything it no longer had is reported as dropped, so the program always knows. The client libraries do all of this by themselves.
  • Flow control:
    • Every session has its own sender thread and a bounded outbound queue (1024 messages / 8 MB by default). Delivery to a remote module never waits for the network, and a slow program holds up nobody else.
    • When the queue is full, the oldest non-guaranteed trigger is dropped first, and the program is told.
    • Every trigger carries PsySeq, which is strictly increasing per session.
  • Takeover: a second attach from the same device replaces the old connection (takeover="device", the default). The old connection is told why.

Dynamic modules: build the contract at run time πŸ”§

Sometimes the program knows best what it needs, such as a logger, a dashboard or a research tool. With allowdynamic="yes", a process can attach a module name the spec does not declare and set up its own subscriptions. The host checks every request against an <allow> policy, and without one a dynamic module can do nothing:

<service name="Modules">
  <modules auth="key" allowdynamic="yes">
    <allow names="Ext.*" subscribe="Voice.*,Psyclone.Ready" post="Ext.*" retrieve="ConvWB" query="FileCat"/>
  </modules>
</service>
from psyclone_client import Trigger

ext = RemoteModule.attach(psy, "Ext.Logger", Auth.key(api_key))      # ext.dynamic is True
ext.subscribe(Trigger("All", "Voice.*").max_age(1000).has_key("Text"))
ext.declare_post("Note", "Ext.Logger.Note")
ext.post("Note", DataMessage().set_string("Text", "seen"))
rows = ext.retrieve_from("ConvWB", type="Voice.Asr.*", max_count=20)  # ad hoc, inside <allow retrieve>
ext.subscribe(Trigger("X", "Secret.Thing"))                          # RemoteModuleError: denied

How the host handles a dynamic module: – Triggers: they travel as the PsySpec’s own <trigger> XML, so wildcards, from, maxage, interval and filters all work. – Repeats are safe: sending the same trigger again changes nothing, and an interval trigger never gets a second timer. – Types are checked first: the host checks every type before it registers it, so a stranger cannot fill the type tables. – After a lease runs out: the client sends its whole desired set again in the new session. – Declared modules can do it too: <module type="remote" dynamic="yes"> lets a declared module subscribe and declare inside <allow>.

Any language, one protocol 🌍

Remote modules add no new framing. Every frame is one DataMessage on the service’s interface: – Control frames: they use the reserved post name PsyModule with an operation such as hello, attach, retrieve, query, subscribe, declare or detach. – Posts: they carry the module’s post name. – Triggers: they arrive with PsyTrigger and PsySeq.

The protocol (“Remote Module Protocol 1”) is written down entry by entry in User Guide chapter 11. Kotlin/Java and Python have it today. For another language you port the DataMessage codec and follow that page; the engine needs no change.

Not in 2.3.2 yet: – signals; – context triggers and trigger groups over the wire; – several processes for one module name; – a JSON mode for browsers.

The engine test is test=remotemodule, with declared and dynamic parts. Both client libraries have live tests that start a real Psyclone.

Read more

Published by CMLabs. Communicative Machines builds Psyclone AIOS, an open-source orchestration runtime for sub-millisecond, crash-resistant, self-reconfiguring AI systems.

Similar Posts

Leave a Reply