npm install @techsavvyash/bus effect @effect/platform-node-shared

The package has three entry points:

ImportWhat it is
@techsavvyash/busThe Effect API: EventBus and EventStore services, the registry, errors
@techsavvyash/bus/promiseClanker’s classes on Promises, for code that isn’t on Effect
@techsavvyash/bus/conformancerunEventStoreConformance, the node:test suite every store must pass

Events

type BusEvent<TData extends JsonObject = JsonObject> = {
  id: string; // evt_<uuid>, assigned by emit
  type: string; // "pad.task.created", "custom.deploy.finished"
  source: string; // "pad" | "rules" | "agent:<runtimeId>" | embedder-defined
  subject?: string; // usually a task or run id
  correlationId?: string; // root job id: threads a whole multi-agent job
  causationId?: string; // the event that caused this one
  dedupeKey?: string; // emit-time idempotency
  data: TData;
  metadata?: JsonObject;
  time: string; // ISO 8601, assigned by emit
};

You emit a NewBusEvent, which is the same without id and time.

An EventFilter matches on type (exact), types (any of), source, subject, correlationId and since (inclusive, on time), and can set a limit. Giving both type and types is a BusValidationError.

Event types

The EventTypeRegistry decides what may be emitted:

  • Type names: lowercase, dot-separated, with at least one dot.
  • registerSystem(type, schema?): registers one exact type. Registering it again with the same schema does nothing. A different schema is an error.
  • allowNamespace("custom."): admits every type under the prefix, without a schema.
  • registerCustom(type, schema?): gives a type under an allowed namespace a schema.

Schemas are JSON Schema (draft-07), compiled to Effect Schema when they’re registered. Payloads are checked against them on every emit. An extra property is rejected when the schema sets additionalProperties: false. A schema that uses a keyword Effect can’t enforce (format, if/then, not, contains, dependentRequired and a few others) is rejected at registration. For format, use pattern instead.

registerAgentEventTypes(registry) registers the 16 agent.* types, including AGENT_RUN_STARTED and AGENT_TOOL_CALL_COMPLETED.

The Effect API

import { NodeFileSystem } from "@effect/platform-node-shared";
import { Effect, Layer } from "effect";
import { EventBus, EventStore, EventTypeRegistry } from "@techsavvyash/bus";
 
const registry = new EventTypeRegistry();
const program = Effect.gen(function* () {
  yield* registry.registerSystem("pad.task.created", { type: "object", required: ["title"] });
  yield* registry.allowNamespace("custom.");
 
  const bus = yield* EventBus;
  yield* bus.subscribe((event) => Effect.log(event.type), { types: ["pad.task.created"] });
  yield* bus.emit({ type: "pad.task.created", source: "pad", data: { title: "Ship the bus" } });
  return yield* bus.list({ source: "pad" });
});
 
await Effect.runPromise(
  program.pipe(
    Effect.provide(EventBus.layer({ registry })),
    Effect.provide(EventStore.jsonl(".bus").pipe(Layer.provide(NodeFileSystem.layer)))
  )
);
  • EventBus.layer(options): a bus over the EventStore in context. makeEventBus({ store, registry, … }) builds one directly.
  • Options: registry, clock (defaults to Effect’s Clock, so TestClock drives it), idGenerator, and onSubscriberError(error, event).
  • Store layers: EventStore.memory, or EventStore.jsonl(directory), which needs FileSystem.
  • Subscribers: a subscriber returns an Effect. Its failures and defects go to onSubscriberError. If onSubscriberError itself throws, that’s ignored, and delivery carries on.
  • Errors: BusValidationError, DuplicateEventError (with dedupeKey and existingEventId) and StoreError. Each has a _tag and a readable message.

The Promise facade

The facade keeps clanker’s class names and signatures. Registry methods and subscribe throw, and everything else returns a Promise:

import { EventBus, EventTypeRegistry, JsonlEventStore } from "@techsavvyash/bus/promise";
 
const registry = new EventTypeRegistry();
registry.allowNamespace("custom.");
const bus = new EventBus({ store: new JsonlEventStore(".bus"), registry });
bus.subscribe(async (event) => console.log(event.type), { source: "deployer" });
await bus.emit({ type: "custom.deploy.finished", source: "deployer", data: { ok: true } });

The facade throws and rejects with the same error classes, so instanceof DuplicateEventError works. bus.effect and registry.effect expose the wrapped Effect objects, so a NestJS service and Effect code can share one bus. fromPromiseEventStore and toPromiseEventStore convert a store between the two styles.

The agent sink

makeBusEventSink(bus, options) (or createBusEventSink in the facade) returns a sink for an agent run’s events. Each event becomes agent.<type> (agent.call.* types are left as they are), with the run id as subject and the whole event as data.

You can stamp each republished event with a source (default "clanker-runtime"), a correlationId and a causationId. The sink never fails. Bus problems go to onPublishError, and a throwing onPublishError is ignored, because a bus problem must never fail an agent run.