Examples
Demand Signal
What this example shows
Not every loop needs AI. This example models rule-based detection where an automation actor watches metrics and emits a start signal when thresholds are crossed. It is the SENSE stage in the operational loop pattern.
Loop diagram
1METRIC_STREAM --[threshold_breach]--> SIGNAL_DETECTED2SIGNAL_DETECTED --[start_loop]--> LOOP_STARTED3LOOP_STARTED --[run_governed_flow]--> COMPLETEActors
| Actor | Type | Transitions | Guards |
|---|---|---|---|
| detector | automation | threshold_breach, start_loop | none |
| runtime | system | loop_start | none |
Key annotated snippet
1import { createSignalEngine, thresholdBreachRule } from "@loop-engine/signals";2import { aggregateId } from "@loop-engine/core";3 4const signalEngine = createSignalEngine();5signalEngine.registerRule(6 thresholdBreachRule({7 field: "demandChange",8 operator: "gt",9 threshold: 0.7510 })11);12 13signalEngine.subscribe(async (signal) => {14 await engine.start({15 loopId: "scm.replenishment",16 aggregateId: aggregateId(`repl-${Date.now()}`),17 actor: { type: "automation", id: "system:signal-detector" },18 metadata: {19 sourceSignalType: signal.type,20 sourceSignalId: signal.id,21 sku: signal.payload?.sku22 }23 });24});What emitted events look like
1{2 type: "loop.started",3 loopId: "scm.replenishment",4 aggregateId: "repl-1710339001422",5 initialState: "SIGNAL_DETECTED",6 actor: { type: "automation", id: "system:signal-detector" },7 occurredAt: "2026-03-13T12:40:50.111Z"8}Try it yourself
1cd loop-examples/demand-signal2pnpm install3pnpm dev