monoblok docs
Docs menu · Large patchbays

Examples

Large patchbays

Two patchbays of two dozen rules each, showing how the small forms compose into a whole conditioning layer: JSON demux at the head, stable dashboard channels, safety alerts, roll-ups, liveness and audit trails.

Both files live in advanced-examples/. They were written by a coding agent with AGENTS_PATCHBAY.md loaded, for deliberately fictional domains. They demonstrate rule composition and can be validated and run locally; their fictional domain models are not intended for production use. Each has a matching Python pump script that publishes randomised telemetry.

How a large patchbay is laid out#

Both files follow the same order, and it is a good template for your own:

  1. Cache. One (lvc ...) form opts the input, alert, ops and audit trees into the last-value cache, so a dashboard that connects late is populated at once.
  2. Demux. One :reentrant true rule per JSON frame type breaks the frame into scalar subjects. Everything after this works on plain numbers and strings.
  3. Stable channels. Short -> pipelines (smooth, round, gate, publish) give dashboards quiet subjects to subscribe to.
  4. Liveness. sample! republishes the latest frame on a fixed cadence and dropout flags a rig that has gone silent.
  5. Alerts. transition over a windowed statistic raises one alert and one all-clear; rising-edge plus throttle caps one-shot alerts.
  6. Roll-ups. aggregate!, bar! and count! produce the numbers a report wants.
  7. Audit. Rules build dated subjects with subject-with and now, so events land on subjects like audit.2026-11-11.rig-03.seal.
  8. Fleet meter. One rule on the whole tree publishes the ingest rate.

Moonwell#

Advanced A fictional lunar ice-mining habitat. Rigs publish three nested JSON frames (env, drill, power) and free-form log lines under moonwell.<rig>.*.

advanced-examples/moonwell_patchbay.edn
; Moonwell patchbay: a fictional lunar ice-mining habitat under Shackleton
; crater. Prospectors publish JSON frames; rules derive stable telemetry,
; drill safety alerts, power rollups, and operator audit streams.

(lvc ["moonwell.>"
      "alerts.>"
      "ops.>"
      "audit.>"])

; Environmental frame:
; moonwell.rig-03.env {"sensor":{"thermal":{"temp":-142.1},
;                      "atmo":{"pressure":69.4,"dust":0.12,"radiation":0.08},
;                      "prospect":{"ice":41.2},"hatch":{"seal":"LOCKED"}}}
(on "moonwell.*.env" :reentrant true
  (json-demux! ["sensor.thermal.temp" "temp"]
               ["sensor.atmo.pressure" "pressure"]
               ["sensor.atmo.dust" "dust"]
               ["sensor.atmo.radiation" "radiation"]
               ["sensor.prospect.ice" "ice"]
               ["sensor.hatch.seal" "seal"]
               payload))

; Drill frame:
; moonwell.rig-03.drill {"sensor":{"bore":{"depth":18.4},
;                        "motor":{"torque":62,"rpm":94,"mode":"AUTO"},
;                        "bit":{"vibration":0.31,"temp":81.4}}}
(on "moonwell.*.drill" :reentrant true
  (json-demux! ["sensor.bore.depth" "depth"]
               ["sensor.motor.torque" "torque"]
               ["sensor.motor.rpm" "rpm"]
               ["sensor.bit.vibration" "vibration"]
               ["sensor.bit.temp" "bit_temp"]
               ["sensor.motor.mode" "mode"]
               payload))

; Power frame:
; moonwell.rig-03.power {"sensor":{"storage":{"battery":77.4},
;                        "array":{"solar":410},
;                        "bus":{"load":360,"heater":44,"state":"NOMINAL"}}}
(on "moonwell.*.power" :reentrant true
  (json-demux! ["sensor.storage.battery" "battery"]
               ["sensor.array.solar" "solar"]
               ["sensor.bus.load" "load"]
               ["sensor.bus.heater" "heater"]
               ["sensor.bus.state" "bus"]
               payload))

; Stable dashboard channels.
(on "moonwell.*.env.temp"
  (-> payload-float
      (moving-avg :ms 45000)
      (round 1)
      (squelch)
      (publish! (subject-append "stable"))))

(on "moonwell.*.env.pressure"
  (-> payload-float
      (round 1)
      (deadband 0.8)
      (publish! (subject-append "stable"))))

(on "moonwell.*.drill.depth"
  (-> payload-float
      (quantize 0.5)
      (squelch)
      (publish! (subject-append "meter"))))

(on "moonwell.*.power.load"
  (-> payload-float
      (hold-off 10000)
      (publish! (subject-append "sampled"))))

; Liveness and current snapshots.
(on "moonwell.*.env"
  (do
    (sample! :ms 5000 (subject-append "snapshot") payload)
    (dropout :ms 60000
      :lost  (publish! (subject-append "stale") "true")
      :found (publish! (subject-append "stale") "false"))))

(on "moonwell.*.drill"
  (sample! :ms 3000 (subject-append "snapshot") payload))

; Habitat safety.
(on "moonwell.*.env.pressure"
  (transition (< (moving-avg 8 payload-float) 61.0)
    (publish! (subject-append "alert") "pressure-low")
    (publish! (subject-append "ok") "pressure-restored")))

(on "moonwell.*.env.dust"
  (-> (> payload-float 0.55)
      (rising-edge)
      (throttle :ms 300000 2)
      (publish! (subject-with "alerts" "dust" (subject-token 1)))))

(on "moonwell.*.env.radiation"
  (transition (> (percentile 40 0.95 payload-float) 0.18)
    (publish! (subject-append "alert") "radiation-p95-high")
    (publish! (subject-append "ok") "radiation-normal")))

(on "moonwell.*.env.seal"
  (when (changed? payload)
    (publish! (subject-with "audit" (now :date) (subject-token 1) "seal")
              (str-concat subject ": " payload))))

; Drill protection.
(on "moonwell.*.drill.torque"
  (when (> (- (moving-max 20 payload-float)
              (moving-min 20 payload-float)) 32.0)
    (publish! (subject-append "chatter") payload)))

(on "moonwell.*.drill.vibration"
  (transition (> (moving-avg :ms 20000 payload-float) 0.72)
    (publish! (subject-with "ops" (subject-token 1) "drill" "slow") "vibration")
    (publish! (subject-with "ops" (subject-token 1) "drill" "resume") "vibration-clear")))

(on "moonwell.*.drill.bit_temp"
  (-> payload-float
      (moving-avg 10)
      (clamp -200.0 140.0)
      (deadband 2.0)
      (publish! (subject-append "managed"))))

(on "moonwell.*.drill.mode"
  (when (contains? ["MANUAL" "SAFE" "FAULT"] payload)
    (do
      (count!)
      (publish! (subject-with "alerts" "drill-mode" (subject-token 1)) payload))))

; Ice yield and power economics.
(on "moonwell.*.env.ice"
  (do
    (debounce! :ms 2500 (subject-append "settled") payload)
    (aggregate! :ms 300000 (subject-append "avg5m") :avg payload-float)
    (bar! :ms 60000 payload-float)))

(on "moonwell.*.power.battery"
  (transition (< (moving-avg 12 payload-float) 24.0)
    (publish! (subject-append "alert") "reserve-low")
    (publish! (subject-append "ok") "reserve-recovered")))

(on "moonwell.*.power.solar"
  (aggregate! :ms 300000 (subject-append "sum5m") :sum payload-float))

(on "moonwell.*.power.heater"
  (when (> (variance 30 payload-float) 260.0)
    (publish! (subject-append "unstable") payload)))

(on "moonwell.*.power.bus"
  (when (not (= payload "NOMINAL"))
    (publish! (subject-with "alerts" "power-bus" (subject-token 1)) payload)))

; Free-form rig diagnostics.
(on "moonwell.*.log"
  (do
    (count! (contains? payload "ERROR"))
    (when (or (contains? payload "ERROR")
              (contains? payload "ICE_VOID"))
      (publish! (subject-with "audit" (now :hour) (subject-token 1) "events")
                (str-concat subject ": " payload)))))

; Fleet ingest and noisy-subject meter.
(on "moonwell.>"
  (-> payload
      (rate :ms 1000)
      (round 1)
      (publish! "ops.moonwell.ingest.hz")))

Forms worth studying here:

ruletechnique
moonwell.*.env demuxjson-demux! with [PATH SUFFIX] pairs flattens nested paths such as sensor.thermal.temp to a short suffix (temp)
env.temp stablea time window feeding value gates: moving-avg :ms 45000, then round, then squelch
env livenesstwo effects in one do: sample! for a 5-second snapshot and dropout for stale and recovered
env.dust alerta boolean threaded through rising-edge then throttle :ms 300000 2: at most two dust alerts in five minutes
env.radiation alerttransition over a 95th percentile, so one outlier does not raise the alarm
env.seal auditchanged? as a predicate, publishing to a dated subject built with subject-with and (now :date)
drill.torque chatterspread over a window: moving-max minus moving-min
drill.modevector membership with contains?, a count! and an alert in one do
env.icethree clock forms on one subject: debounce!, aggregate! and a one-minute bar!
moonwell.> meterrate :ms 1000 across the whole tree for a fleet-wide messages-per-second figure
shell
cd advanced-examples
monoblok moonwell_patchbay.edn
python pump_moonwell.py

A recorded session shows a coding assistant building this patchbay.

Cloudhook#

Advanced A fictional offshore fleet of autonomous kite turbines. Kites publish flat JSON frames for flight, weather and power under cloudhook.<kite>.*.

advanced-examples/cloudhook_patchbay.edn
; Cloudhook patchbay: a fictional fleet of autonomous kite turbines tethered
; above offshore buoys. Kites publish JSON frames; rules derive flight safety,
; power quality, stable dashboards, and operator audit streams.

(lvc ["cloudhook.>"
      "alerts.>"
      "ops.>"
      "audit.>"])

; Flight frame:
; cloudhook.kite-04.flight {"alt":612,"airspeed":28.4,"tether":43.1,
;                           "yaw":-2.4,"roll":8.1,"mode":"LOOP"}
(on "cloudhook.*.flight" :reentrant true
  (json-demux! "alt" "airspeed" "tether" "yaw" "roll" "mode" payload))

; Weather frame:
; cloudhook.kite-04.weather {"wind":31.2,"gust":39.5,"shear":0.14,
;                            "pressure":1002.3,"rain":0.0}
(on "cloudhook.*.weather" :reentrant true
  (json-demux! "wind" "gust" "shear" "pressure" "rain" payload))

; Power frame:
; cloudhook.kite-04.power {"kw":84.2,"voltage":711,"battery":68.1,
;                          "inverter":"GRID","brake":0}
(on "cloudhook.*.power" :reentrant true
  (json-demux! "kw" "voltage" "battery" "inverter" "brake" payload))

; Stable dashboard outputs.
(on "cloudhook.*.flight.alt"
  (-> payload-float
      (moving-avg 10)
      (quantize 10)
      (squelch)
      (publish! (subject-append "band"))))

(on "cloudhook.*.flight.airspeed"
  (-> payload-float
      (moving-avg :ms 20000)
      (round 1)
      (deadband 0.6)
      (publish! (subject-append "stable"))))

(on "cloudhook.*.weather.wind"
  (-> payload-float
      (round 1)
      (squelch)
      (publish! (subject-append "stable"))))

(on "cloudhook.*.power.kw"
  (-> payload-float
      (hold-off 5000)
      (publish! (subject-append "sampled"))))

; Liveness and recent snapshots.
(on "cloudhook.*.flight"
  (do
    (sample! :ms 4000 (subject-append "snapshot") payload)
    (dropout :ms 30000
      :lost  (publish! (subject-append "stale") "true")
      :found (publish! (subject-append "stale") "false"))))

(on "cloudhook.*.weather"
  (sample! :ms 10000 (subject-append "snapshot") payload))

; Flight envelope protection.
(on "cloudhook.*.flight.alt"
  (transition (or (< (moving-avg 6 payload-float) 350.0)
                  (> (moving-avg 6 payload-float) 900.0))
    (publish! (subject-append "alert") "altitude-envelope")
    (publish! (subject-append "ok") "altitude-normal")))

(on "cloudhook.*.flight.tether"
  (transition (> (percentile 30 0.95 payload-float) 72.0)
    (publish! (subject-with "ops" (subject-token 1) "winch" "ease") "high-tension")
    (publish! (subject-with "ops" (subject-token 1) "winch" "hold") "tension-normal")))

(on "cloudhook.*.flight.roll"
  (when (> (stddev 20 payload-float) 14.0)
    (publish! (subject-append "unstable") payload)))

(on "cloudhook.*.flight.mode"
  (when (changed? payload)
    (publish! (subject-with "audit" (now :date) (subject-token 1) "mode")
              (str-concat subject ": " payload))))

; Weather hazards.
(on "cloudhook.*.weather.gust"
  (-> (> payload-float 47.0)
      (rising-edge)
      (throttle :ms 600000 3)
      (publish! (subject-with "alerts" "gust" (subject-token 1)))))

(on "cloudhook.*.weather.shear"
  (transition (> (moving-avg :ms 30000 payload-float) 0.32)
    (publish! (subject-with "ops" (subject-token 1) "pattern" "widen") "shear")
    (publish! (subject-with "ops" (subject-token 1) "pattern" "normal") "shear-clear")))

(on "cloudhook.*.weather.rain"
  (when (> (- (moving-max 20 payload-float)
              (moving-min 20 payload-float)) 4.0)
    (publish! (subject-append "squall") payload)))

; Power quality and generation rollups.
(on "cloudhook.*.power.kw"
  (do
    (aggregate! :ms 300000 (subject-append "avg5m") :avg payload-float)
    (bar! :ms 60000 payload-float)))

(on "cloudhook.*.power.voltage"
  (transition (or (< (moving-avg 8 payload-float) 640.0)
                  (> (moving-avg 8 payload-float) 780.0))
    (publish! (subject-append "alert") "voltage-out-of-band")
    (publish! (subject-append "ok") "voltage-normal")))

(on "cloudhook.*.power.battery"
  (transition (< (moving-avg 12 payload-float) 25.0)
    (publish! (subject-append "alert") "reserve-low")
    (publish! (subject-append "ok") "reserve-recovered")))

(on "cloudhook.*.power.inverter"
  (when (contains? ["ISLAND" "FAULT" "STARTUP"] payload)
    (do
      (count!)
      (publish! (subject-with "alerts" "inverter" (subject-token 1)) payload))))

(on "cloudhook.*.power.brake"
  (-> (= payload-int 1)
      (rising-edge)
      (publish! (subject-with "audit" (now :minute) (subject-token 1) "brake"))))

; Free-form kite diagnostics.
(on "cloudhook.*.log"
  (do
    (count! (contains? payload "ERROR"))
    (when (or (contains? payload "ERROR")
              (contains? payload "LINE_SNAP")
              (contains? payload "BIRD_STRIKE"))
      (publish! (subject-with "audit" (now :hour) (subject-token 1) "events")
                (str-concat subject ": " payload)))))

; Fleet ingest rate.
(on "cloudhook.>"
  (-> payload
      (rate :ms 1000)
      (round 1)
      (publish! "ops.cloudhook.ingest.hz")))

The structure matches Moonwell, with a few different techniques:

  • Compound conditions in a transition. The altitude and voltage rules use (or (< avg LO) (> avg HI)) to alert when a smoothed value leaves a band in either direction. Each moving-avg inside the or is its own call site with its own ring.
  • Operator commands as subjects. The tether and shear rules publish to ops.<kite>.winch.ease and ops.<kite>.pattern.widen: the alert is addressed to whatever acts on it.
  • Integer payloads. (= payload-int 1) through rising-edge records the moment a brake engages, once.
  • Quantised bands. moving-avg, quantize 10, squelch reports altitude in 10-unit bands, so a dashboard updates only when the kite changes band.
shell
cd advanced-examples
monoblok cloudhook_patchbay.edn
python pump_cloudhook.py

The pump scripts default to 127.0.0.1:4222. Use --host and --port if monoblok is listening somewhere else.

Making your own#

To make more examples like these, download and install the Patchbay authoring skill. No repository checkout is needed. Invoke $patchbay-authoring in Codex or /patchbay-authoring in Claude Code, then ask for a complete pair: one patchbay .edn file and one pump script that publishes matching test data.

text
Create a patchbay .edn file and a matching Python script that publishes test data.

Use a fictional telemetry domain, optionally with JSON input subjects, scalar
demux rules, alerts, stable dashboard outputs, audit subjects, and a few
windowed stats. The pump script should connect to monoblok on 127.0.0.1:4222
and publish randomised test data for the subjects the patchbay expects.

Check whatever comes back before running it:

shell
monoblok --validate your_example_patchbay.edn

Sizing a large patchbay#

  • Cost follows the rules that match a message, not the number of rules in the file. Narrow filters keep the per-message work small.
  • Each re-entrant demux adds one round of rule evaluation per emitted field. Demux only the fields that later rules or subscribers use.
  • Every stateful form keeps a slot per concrete subject. A wildcard over thousands of devices is thousands of slots; see Budgeting bar state for how to measure it.
  • (now :minute) in a subject creates a new subject every minute. Prefer :hour or :date unless you mean it.

MIT licensed · © 2026 Alex Reid
Lexvica offers solutions built with monoblok.