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
1"cmt">// @no-typecheck2"cmt">// Roadmap-oriented narrative; published @loop-engine/signals is `SignalRegistry` only at 1.0.0-rc.0.3import { createSignalEngine, thresholdBreachRule } from "@loop-engine/signals";4import { aggregateId } from "@loop-engine/core";5 6const signalEngine = createSignalEngine();7signalEngine.registerRule(8 thresholdBreachRule({9 field: "demandChange",10 operator: "gt",11 threshold: 0.7512 })13);14 15signalEngine.subscribe(async (signal) => {16 await engine.start({17 loopId: "scm.replenishment",18 aggregateId: aggregateId(`repl-${Date.now()}`),19 actor: { type: "automation", id: "system:signal-detector" },20 metadata: {21 sourceSignalType: signal.type,22 sourceSignalId: signal.id,23 sku: signal.payload?.sku24 }25 });26});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