Skip to content

Sensor alarm: make time an input

This program tracks each sensor's readings, emits alarm changes and reports a missing heartbeat.

The partner example tested a response and a timeout. Now ask a harder question: how do you test silence? Keep state per sensor, distinguish a threshold crossing from noise, and make the waiting period an explicit input to your scenario.

The file formats are .rkg for the program and Lab, .rkc for a reviewed scenario reference and .rkr for a real execution recording.

From the repository root, with rastok installed:

Terminal window
rastok simulate examples/sensor-alarm.rkg --compare examples/traces/sensor-alarm
PASS sensors.hysteresis
PASS sensors.silent_sensor
PASS sensors.virtual_day
3 scenario(s), 0 failed

The four lines above are stdout. The same command writes two warnings to stderr:

warning[W-EMPTY-INSTANCE]: `hysteresis` can open an instance that retains nothing on `in`
--> examples/sensor-alarm.rkg:31:7
|
31 | _ => nothing
| ^^^^^^^^^^^^
= owner: hysteresis
= trigger: in
= help: give the instance a delay, or add `stop` if that branch ends its work
warning[W-EMPTY-INSTANCE]: `watchdog` can keep an instance that retains nothing after `on (probe, heartbeat)`
--> examples/sensor-alarm.rkg:41:49
|
41 | on (probe, heartbeat) within duration("5m") {
| ^
= owner: watchdog
= trigger: (probe, heartbeat)
= help: give the instance a delay, or add `stop` if that branch ends its work

A first reading can leave hysteresis unchanged, and watchdog keeps no state after consuming a pair. Both Operators intentionally keep the sensor instance for later messages; neither warning changes the trace.

First follow the readings into the keyed stateful Operator: its two thresholds prevent an alarm from oscillating around one limit. Then find the watchdog join. A probe and heartbeat rendezvous within a bounded interval; a missing partner produces JoinExpired, whose route is as explicit as a normal output. Read advance in the Lab last: time comes from the scenario, not from sleeps hidden in the application.

var tripped: bool = false is the memory behind that behavior: a boolean cell initialized to false for each sensor's Operator instance. At 121, pump-a sets it to true and emits tripped; at 100, it keeps that state and emits nothing; at 79, it resets the cell and emits normal. Pump-b's cell is independent and starts at false, so its reading of 100 produces no alarm. Each state change commits together with its emission, not as a separate shared assignment. The preceding webhook example uses var seen for the same kind of private memory. See Values and mutable state for the difference between var, val and an instance key.

There is no Connector in this program. The Lab reference explains expectations and observation windows; advance supplies virtual time.

Source: examples/sensor-alarm.rkg

// Hysteresis per sensor and a bounded probe/heartbeat rendezvous.
// The Lab supplies clock advances, not a real periodic sensor Driver.
type Reading = struct { sensor: string, value: int }
type Sensor = struct { sensor: string }
type Alarm = struct { sensor: string, state: string }
type MissedHeartbeat = union { JoinExpired {} }
graph sensor_alarm : App {
input reading: Reading
input probe: Sensor
input heartbeat: Sensor
output alarm: Alarm
output alive: string
output silent: MissedHeartbeat
operator hysteresis {
input in: Reading
output out: Alarm
instance = { key = data.sensor }
lifetime = { kind = persistent }
var tripped: bool = false
on in match {
!tripped && data.value > 120 => {
tripped = true
out = { sensor = data.sensor; state = "tripped" }
}
tripped && data.value < 80 => {
tripped = false
out = { sensor = data.sensor; state = "normal" }
}
_ => nothing
}
}
operator watchdog {
input probe: Sensor
input heartbeat: Sensor
output out: string
output expired: MissedHeartbeat
instance = { key = data.sensor }
lifetime = { kind = persistent }
on (probe, heartbeat) within duration("5m") {
out = probe.data.sensor
}
}
reading -> hysteresis -> alarm
probe -> watchdog.probe
heartbeat -> watchdog.heartbeat
watchdog.out -> alive
watchdog.expired -> silent
}
lab sensors {
scenario hysteresis {
app.reading = { sensor = "pump-a"; value = 121 }
expect app.alarm where data.sensor == "pump-a" && data.state == "tripped"
app.reading = { sensor = "pump-a"; value = 100 }
expect app.alarm == nothing
app.reading = { sensor = "pump-b"; value = 100 }
expect app.alarm == nothing
app.reading = { sensor = "pump-a"; value = 79 }
expect app.alarm where data.state == "normal"
}
scenario silent_sensor {
app.probe = { sensor = "pump-a" }
app.probe = { sensor = "pump-b" }
app.heartbeat = { sensor = "pump-b" }
expect app.alive == "pump-b"
advance duration("5m")
expect app.silent where data is JoinExpired
expect app.alive == nothing
}
scenario virtual_day {
app.probe = { sensor = "pump-a" }
advance duration("24h")
expect app.silent where data is JoinExpired
expect app.alarm == nothing
}
}

The golden's sensors.virtual_day follows one unanswered probe, not the separate hysteresis scenario; the advance visits its five-minute deadline.

The golden's sensors.virtual_day follows one unanswered probe, not the separate hysteresis scenario; the advance visits its five-minute deadline.watchdogsensor_alarmLab: sensors.virtual_daywatchdogsensor_alarmLab: sensors.virtual_dayWait for heartbeat within 5mAt 5m: no matching heartbeatAdvance completes at 24h, no alarm emittedprobe = pump-aprobe = pump-aadvance 24hexpired to silent = JoinExpired
The golden's sensors.virtual_day follows one unanswered probe, not the separate hysteresis scenario; the advance visits its five-minute deadline.watchdogsensor_alarmLab: sensors.virtual_daywatchdogsensor_alarmLab: sensors.virtual_dayWait for heartbeat within 5mAt 5m: no matching heartbeatAdvance completes at 24h, no alarm emittedprobe = pump-aprobe = pump-aadvance 24hexpired to silent = JoinExpired

Pump-a trips above 120, stays tripped at 100, and clears below 80. Pump-b has independent state. A keyed join pairs a probe with a heartbeat within five minutes; only the unanswered probe expires in the silent-sensor scenario. The final scenario advances twenty-four virtual hours in one authored action.

With Ruby and the built target/debug/rastok available, this command measures process wall time on your machine; it does not change the scenario's logical time:

Terminal window
ruby -e '3.times { t = Process.clock_gettime(Process::CLOCK_MONOTONIC); ok = system(*ARGV); puts format("elapsed_cli_seconds=%.6f", Process.clock_gettime(Process::CLOCK_MONOTONIC) - t); exit 1 unless ok }' target/debug/rastok simulate examples/sensor-alarm.rkg

Raise the trip threshold from 120 to 130. The sample at 121 no longer trips, and the original assertion and golden fail. Alternatively, remove the route from watchdog.expired: the checker refuses E-UNWIRED-OUTPUT before execution.

Terminal window
rastok simulate examples/sensor-alarm.rkg --compare examples/traces/sensor-alarm
FAIL sensors.hysteresis
expect app.alarm where data.sensor == "pump-a" && data.state == "tripped" (sensor-alarm.rkg:56)
nothing observed
where data.sensor == "pump-a" && data.state == "tripped" is false for 0 of 0 deliveries; count 0, expected 1
expect app.alarm where data.state == "normal" (sensor-alarm.rkg:62)
nothing observed
where data.state == "normal" is false for 0 of 0 deliveries; count 0, expected 1
golden divergence: record 3: emissions(23).len; expected 1; got 0; cause: TRANSACTION "hysteresis" · trigger "p:in" · cause_id absent
PASS sensors.silent_sensor
PASS sensors.virtual_day
3 scenario(s), 1 failed

Restore the checked-in source (this discards your edits to this example):

Terminal window
git checkout -- examples/sensor-alarm.rkg

The threshold edit changes a recorded Operator transaction, including its state delta and emitted alarm. The diff gives the first changed record, expected/actual values and causal trigger. The missing expiry route is a static error, not a runtime golden difference.

Inspect the raw sealed trace (.rkc)

The versioned reference stores one hex-encoded causal record per line, with an optional final seal line. Use the chapter's comparison commands to explain a divergence.

Source: examples/traces/sensor-alarm/sensors.virtual_day.rkc

a7070108a40150f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c8040105820254820f81826673656e736f7282066670756d702d6109636c61620a000b000c6570726f62650d8182752e70726f62653e7761746368646f672e70726f62658258238281687761746368646f67818381687761746368646f6701820782066670756d702d6100
a7070211601282438280800013676c3a737461727414801780181a50f3df1105112054d397a04ed0666ca4c8
a8070211687761746368646f67128258238281687761746368646f67818381687761746368646f6701820782066670756d702d610013676c3a737461727414801780181a50f3df1105112054d397a04ed0666ca4c81822824382808000
a7070211687761746368646f67128258238281687761746368646f67818381687761746368646f6701820782066670756d702d610013676c3a6576696374148183752e70726f62653e7761746368646f672e70726f626582438280800001178183676578706972656400a50150354e01026e2d3e4214fc54f4c1b6c8900250f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c80401058202528213826b4a6f696e45787069726564820f80181a50f3df1105112054d397a04ed0666ca4c8
seal 9de2f8f4a2c1ad627873679b88408b9d90f101d99ba146d4e2362cd30ae4af75

The Lab explains this example's virtual clock and the observation window used by its expectations.

Time, like an external response, is something you can supply deliberately. You can retain a missed heartbeat as a small regression scenario instead of waiting for the fault to recur on equipment. Types, state ownership and the same causal comparison still apply; the device domain needs no special Lab.

The core evaluating this graph has no clock, network access or randomness; time and external outcomes enter as explicit inputs. CI compiles rastok-core for targets without an operating system: thumbv8m.main-none-eabihf, thumbv7m-none-eabi and riscv32imac-unknown-none-elf. It also runs the RP2350 spike under QEMU's Cortex-M33 emulation. Running this sensor example on a physical board is not part of the alpha.

Previous: Webhook to partner. Next: Agent gateway: keep a separate resident process for each conversation and interleave several turns. Before moving on, try writing a second silent-sensor scenario and explaining its first expiry from the journal.

This is a requested probe/heartbeat rendezvous, not automatic periodic polling. No sensor protocol, process or paid model runs. An unmatched value on either join input can produce JoinExpired; the scenarios explicitly control which side is silent. Expiry DATA has no sensor field: its keyed instance and causal history supply that context. The benchmark tasks informed the example, but no claim is made that a published model wave authored it.

Diagram