Build Log / Entry 007 / Platform

A workflow names a purpose, not a model.

Merger Daily’s orchestrator turns domain events into AI calls. Each call names what it’s for, and a table in Core decides which model answers it.

The short version

  • Have workflows request a purpose, not a model; one table in Core maps each purpose to a model.
  • Switching a workflow's model becomes one assignment change, not edits to sixteen flows and a release.
  • Reject a request with no purpose instead of falling back silently to a default model.
  • Detect duplicate events with an insert that returns a row count; a caught constraint violation poisons the transaction.
Published
Read
7 min
Written by
Platform team
Stack
Java 21 · Spring Modulith · Amazon SNS · Amazon SQS · NATS JetStream · PostgreSQL
In this entry · 4 sections
  1. 01Every workflow starts with an event
  2. 02Results come back the other way
  3. 03A workflow asks for a purpose, not a model
  4. 04Where it stands

Merger Daily’s background work runs on the workflow orchestrator we built ourselves (see entry 003). Its flows start from events in the platform. A note is created, a fundamental changes, a company’s record updates: each is an event, and the orchestrator decides what happens next, including which AI call, if any, the event should trigger. This entry follows an event in both directions, and then covers the part that changed most: how a workflow chooses its model.

Every workflow starts with an event

Core, the main Merger Daily service, records each domain event in the same database transaction as the change it describes. Spring Modulith’s event publication table acts as the outbox: the change and the event commit together or not at all, and the event is published to SNS only after the commit. The SNS topic is FIFO, grouped by entity ID, so two updates to the same record are always delivered in order.

Fig. 007.1: An event becomes an AI call that names a purpose, not a vendor or model.

From there the orchestrator pulls from its own SQS FIFO queue. It retries a failing message in-process with exponential backoff; if the message still fails on its third receive, SQS moves it to a dead-letter queue instead of retrying forever. FIFO deduplication only covers a five-minute window, so the orchestrator also keeps its own log of the events it has handled and skips any it has already seen.

Results come back the other way

The orchestrator reports what its flows do (an execution started, a task finished or failed) as workflow events on a NATS JetStream stream. Core consumes them to keep its own view of every flow up to date. Both Core nodes share one durable pull consumer, so each event goes to one node, and each node fetches one message at a time and acknowledges it only after the write succeeds. A crash mid-write means redelivery, not a lost event, so Core has to recognize a repeat.

The usual way to do that is to insert a row for the event and catch the unique-constraint violation on a duplicate. Here, that breaks the consumer. Catching the violation leaves the transaction marked rollback-only, so the commit fails, the message is negatively acknowledged, and JetStream redelivers the same message to the same single-message loop. One duplicate would hold up every other event behind it.

WorkflowDomainEventProcessedRepository.java
@Modifying
@Query(value = "INSERT INTO workflow_domain_event_processed (id, consumer_name, event_id, date_created) "
      + "VALUES (:id, :consumerName, :eventId, :dateCreated) "
      + "ON CONFLICT (consumer_name, event_id) DO NOTHING",
      nativeQuery = true)
int insertIfAbsent(UUID id, String consumerName, String eventId, OffsetDateTime dateCreated);
007.2: an atomic check that returns a row count instead of throwing.Java 21 · Spring Data JPA · NATS JetStream · Postgres

The row count, not an exception, is the signal. Zero rows means an earlier delivery already handled this event, and the transaction stays clean. The event ID comes from a header the orchestrator sets on each message, so it stays the same if the stream is ever recreated. Two more limits keep one bad message from stalling the loop: a payload that can’t be parsed is terminated at once, and anything else gets eight deliveries on a backoff schedule from 5 seconds to 30 minutes before it’s terminated and logged.

A workflow asks for a purpose, not a model

Once the orchestrator decides a note needs, say, AI classification, which model does the call? Sixteen flows made AI calls, 34 calls between them, and each one used to answer that with flow variables: llm_vendor, llm_model, or a global default. Core’s own callers used a single active config, or nothing at all and got the AI service’s default. Changing a model meant editing YAML in each flow, or for Core, a release: two mechanisms, at two speeds.

Now every call names a purpose instead.

POST /api/notes/classification
{
"noteId": "a3e1f9b2-...",
"purpose": "note-classification",
"noteContent": "...",
"correlationId": "c7d4e8a1-..."
}
007.3: the request names what the caller is doing. No vendor, no model.HTTP · merger-daily-ai-llm

Core owns a table that assigns each purpose to a config: provider, model, tuning, and an optional prompt and tools. The AI service keeps an in-memory map of purpose to config, refreshes it from Core on a schedule, and runs whichever config it finds. A request with no purpose, or with a purpose that has no usable config, is rejected; there’s no fallback to a default model. Since callers no longer choose the model, the AI service reports which one it used, and flows record that from the response.

A flow that needs a different model from its siblings gets a more specific purpose, such as note-commentary-aj, assigned to its own config.

Where it stands

Purpose routing is deployed. Moving a workflow to another model is now an assignment change in Core, picked up by the AI service within its refresh interval, not a release across two services and sixteen flow files. A Claude config was seeded enabled but unassigned, so trying it for any purpose is one assignment change. On the event side, each direction has its own guard against repeats: the orchestrator’s event log for domain events, and the Postgres row count for workflow events.

Let's Discuss
Your Ideas

Tell us what you're building. We'll tell you how we'd approach it.

Schedule a call