Running Loops
Event Subscriptions
EventBus subscription model
InMemoryEventBus (@loop-engine/events) exposes:
1emit(event: LoopEvent): Promise<void>2subscribe(handler: (event: LoopEvent) => Promise<void>): () => voidEvent types
loop.startedloop.transition.requestedloop.transition.executedloop.transition.blockedloop.guard.failedloop.completedloop.errorloop.spawnedloop.signal.receivedloop.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})