7. Composition and behaviour
7.1 Open graphs#
A scope that interacts with its context only through boundary ports is an open graph. To give open graphs an algebra, we encode the connection fragment of a Reaktor graph as a hypergraph in the sense of string-diagram rewriting [35]. Each port becomes a vertex. Each node becomes a box labelled by its kind, attached to its ports’ vertices. Each connection becomes a box labelled by its family, attached to its participants’ vertices in role order. The net discipline (W4) gives every vertex at most one connection box. A boundary port is a vertex left open.
Definition 7 (Open Reaktor graph). Let be the monoidal signature whose generators are node kinds and family instances, typed by their ordered port types. An open Reaktor graph is a cospan of -hypergraphs, where encodes and , are discrete hypergraphs of open, typed ports. Composition along is pushout, which plugs open ports into the waiting nets.
Figure 11. Composing open graphs. Each scope is a cospan of its boundaries ( and ). Composition glues the shared boundary by pushout. The glued ports become internal and their nets join, here into one three-party hyperedge. Theorem 3 makes behaviour respect this gluing, so a scope can be mounted at any boundary that matches (Corollary 4).
Theorem 3 (Compositional semantics). Assign to every generator of a behaviour: a span of labelled transition systems from the generator’s transition system to the alphabets of its ports. Then:
the encoding of Appendix A sends well-formed open Reaktor graphs, up to isomorphism, injectively to morphisms of the free hypergraph category on , and mounting a scope at its boundary preserves well-formedness;
there is a unique hypergraph functor from that category to extending the assignment, so that (synchronise on shared ports, then hide) and ;
composition is monotone for trace refinement: if then for every context .
Part (a) is the correspondence between discrete cospans of hypergraphs and free hypergraph categories [35], in its many-sorted form. Part (b) follows from freeness, since is a hypergraph category for any finitely complete [136]. Composition in is the synchronised product of transition systems introduced as Span(Graph) [193]. Part (c) holds because a trace of a pullback is a pair of compatible traces (Appendix A).
Two corollaries carry the requirements R1 and R11.
Corollary 4 (Mount anywhere). If two contexts have equal behaviour at a boundary , then mounting the same scope with boundary in either produces equal behaviour. A self-contained scope therefore depends on its context only through .
Corollary 5 (Sound replacement). If a candidate region refines the boundary traces of the region it replaces, then no context can observe a boundary trace with that it could not observe with .
Corollary 5 is the semantic core of code push. It does not by itself cover state migration, in-flight sessions or durable continuations; Section 9.5 adds those obligations. Three limits are deliberate. First, trace refinement captures safety. Liveness requires assume–guarantee reasoning with explicit fairness [2]. Second, asynchrony lives inside generators: a Channel is a buffer automaton, and composition stays synchronous on events. This avoids the Brock–Ackerman anomaly [38], which breaks relational semantics of nondeterministic dataflow over untimed histories. It also matches the timed-stream semantics of FOCUS [43]. Third, an opaque Kotlin body contributes its declared contract behaviour, an over-approximation. Its actual behaviour is checked by tests and, for selected protocols, by models (Section 15).
Scopes as operations#
A scope with unfilled slots is an operation of the operad of wiring diagrams [315], and filling slots is operadic composition. Associativity and unit laws say that the order in which nested regions are mounted does not matter. This is the algebra behind the current runtime’s nested graphs, made explicit so that it can be checked.
7.2 Operational semantics and its TLA projection#
The state of a running system is . Here is the set of admitted revisions, the activations with their keys and epochs, the local states of activations, the protocol state of each realized connection (buffers, calls in flight, credits), the store states, and the leases and epochs held by authorities. A step is labelled by a local computation, a send, delivery or acknowledgement on a connection, a commit at a store, a firing of a transition, an activation or deactivation, a crash or recovery, a revision publication, or an ownership handover.
Each family contributes a parameterised state machine. A with fan-out, at-least-once delivery and per-key order is a per-consumer queue with Publish, Deliver, Ack and Redeliver. A call has Request, Accept, Commit(), Reply, LoseReply, Timeout and Lookup. An idempotency key binds the digest of its request: a replay with the same key and a different digest is a rejection, not a second effect [219]. Because composition in is a synchronised product, the TLA reading is conjunction over shared variables, with hiding as temporal existential quantification [2]: where is the kind of a node or the family of a connection, is the set of connection hyperedges, ranges over the variables of internal ports, and constrains the boundary ports. A binding is qualified for a family at a quality level when its implementation refines the family’s specification under a refinement mapping [1]. For example, Cloudflare Queues refine a competing-consumer, at-least-once Channel, but not an ordered one; Google Cloud Pub/Sub with ordering keys refines a fan-out, at-least-once, per-key-ordered one. Condition (A2) consults these qualifications. Placement therefore never chooses a binding by name; it chooses one whose refinement has been established, by proof or by a qualification suite.
Execution profiles#
The family fixes what composition means, and the component profile fixes what a component may do. A pure component computes a function. An effectful one acts sequentially through its ports. An exclusive one takes one message per turn from its mailbox. A stream component transforms sequences under demand. A durable one persists each step’s committed outcome and recovers by replaying outcomes rather than stacks. A native one is opaque and bound by its contract, and a human one has unbounded latency and its own authority. Attaching a model of computation to a region and composing heterogeneous regions through typed boundaries follows Ptolemy [104]. In RG the composition rule is Theorem 3, and each profile’s guarantees are explicit and are never inherited by a neighbour.