agentleFS
dbtinoy-dbtinoy-/lexigram-framework-skills/events-and-messaging/SKILL.md

events-and-messaging

Skill1 starsChanged 3 months agoMITView on GitHub

Use when implementing event-driven patterns, CQRS, or message queue integration in the Lexigram framework

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Not calling publisher.stop() in provider shutdown — polling task leaks

    line 106

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Events and Messaging

Overview

Three messaging layers: commands (one handler), events (many handlers), and queue topics (async pub/sub). Contracts live in lexigram.contracts.events / lexigram.contracts.queue; implementations in lexigram-events (CommandBusImpl, EventBusImpl) and lexigram-queue.

Commands (CQRS)

from lexigram.events import Command, command_handler  # or implement CommandHandlerProtocol

class CreateOrder(Command):
    user_id: str
    items: list[str]

class CreateOrderHandler:
    async def handle(self, cmd: CreateOrder) -> Any:   # CommandHandlerProtocol shape
        ...

Dispatch through the bus:

from lexigram.contracts.events import CommandBusProtocol

bus = await container.resolve(CommandBusProtocol)      # CommandBusImpl bound by module
result = await bus.dispatch(CreateOrder(user_id="u1", items=[...]))

Events (Pub/Sub)

Events subclass DomainEvent from lexigram.contracts.domain — a dataclass with event_id, occurred_at, schema_version, event-sourcing fields built in:

from lexigram.contracts.domain import DomainEvent
from lexigram.events import event_handler, EventBusProtocol

class OrderCreated(DomainEvent):
    order_id: str
    user_id: str

@event_handler(OrderCreated, priority=10)
async def send_confirmation(event: OrderCreated) -> None:
    await mailer.send(event.user_id, "Order confirmed")   # resolve deps via DI, not handler args

bus = await container.resolve(EventBusProtocol)
await bus.publish(OrderCreated(order_id="o1", user_id="u1"))  # Result[None, EventError]

Handlers receive only the event. bus.subscribe(event_type, handler) registers objects implementing EventHandlerProtocol.handle(event).

Message Queues

from lexigram.contracts.queue.protocols import QueueProtocol
from lexigram.queue.consumers.consumer import MessageConsumer
from lexigram.contracts.queue.types import BusMessage

queue = await container.resolve(QueueProtocol)
await queue.publish("orders.new", BusMessage(topic="orders.new", payload={"id": "o1"}))

class OrderConsumer(MessageConsumer):
    topic = "orders.new"
    def __init__(self, queue: QueueProtocol):
        super().__init__(queue)

    async def handle(self, msg: BusMessage) -> None:
        await self.process(msg.payload["id"])

# consumer.start() subscribes; consumer.stop() unsubscribes

Transactional Outbox

The outbox lives in lexigram-sql: SQLOutboxStore.append_batch(events) writes pending events inside your DB transaction; a background OutboxPublisher(store, event_bus) polls and publishes:

from lexigram.sql.outbox import OutboxPublisher, SQLOutboxStore

store = SQLOutboxStore(provider)
await store.append_batch([OrderCreated(...)])          # same transaction as state change
publisher = OutboxPublisher(store, event_bus, poll_interval=5.0)
await publisher.start()                                 # stop() to shut down

Quick Reference

PatternClassBus
Command (1 handler)Command, CommandHandlerProtocolCommandBusProtocol.dispatch()
Event (N handlers)DomainEvent + @event_handlerEventBusProtocol.publish()
Queue pub/subQueueProtocol, MessageConsumerqueue.publish()/subscribe()
OutboxSQLOutboxStore + OutboxPublisherappend_batch() / start()

Common Mistakes

  • Using events where commands fit — commands have one handler, events have many
  • Writing @event_handler(OrderCreated)(fn) with extra injected params — handlers take the event only; resolve services inside or via closure
  • Forgetting consumer.start() — subscribing happens there, not in the constructor
  • Publishing outside a DB transaction with the outbox — events may be lost on rollback
  • Not calling publisher.stop() in provider shutdown — polling task leaks

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.