Loop Engine

Running Loops

Event Subscriptions

EventBus subscription model

InMemoryEventBus (@loop-engine/events) exposes:

1emit(event: LoopEvent): Promise<void>
2subscribe(handler: (event: LoopEvent) => Promise<void>): () => void

Event types

  • loop.started
  • loop.transition.requested
  • loop.transition.executed
  • loop.transition.blocked
  • loop.guard.failed
  • loop.completed
  • loop.error
  • loop.spawned
  • loop.signal.received
  • loop.outcome.recorded

Subscribe patterns

1eventBus.subscribe(async (event) => {
2 console.log(event.type, event.aggregateId)
3})
1eventBus.subscribe(async (event) => {
2 if (event.type === 'loop.completed') {
3 console.log('Closed:', event.outcomeId, `${event.durationMs}ms`)
4 }
5})
1const unsubscribe = eventBus.subscribe(async (event) => {
2 console.log(event.type)
3})
4 
5unsubscribe()

Audit trail pattern

Persist loop.transition.executed payloads and retain:

  • actor type/id
  • from/to state
  • evidence
  • timestamp

That yields a complete decision history for compliance and incident review.

External delivery adapters

HTTP webhook bus

1import { httpEventBus } from '@loop-engine/adapter-http'
2 
3const bus = httpEventBus({
4 webhookUrl: 'https://your-app.com/loop-events'
5})

Kafka bus

1import { kafkaEventBus } from '@loop-engine/adapter-kafka'
2import { Kafka } from 'kafkajs'
3 
4const bus = kafkaEventBus({
5 kafka: new Kafka({ brokers: ['localhost:9092'] }),
6 topic: 'loop-events'
7})