Skip to content

Conversation orchestration

Every conversation write — sending, editing, reacting, pinning, group administration, invitations, recovery — and every incoming packet runs as a workflow state machine inside the :feature:conversationorchestration:* modules. :feature:chats only reads and observes data and submits commands.

This page explains the engine and how the modules fit together. The workflow catalog lists every command and every workflow with its step diagram; it is generated from the code by the workflowCatalog Gradle task, so it never has to be updated by hand.

Rules

  1. Chats depends only on :feature:conversationorchestration:api (plus read-only feature dependencies). It never writes conversation data itself.
  2. Every write has exactly one entry: ConversationOrchestrator.execute(command): Flow<ConversationState>.
  3. A workflow owns the sequence, not the functionality. Each step performs one operation by calling a feature use case or repository and reports an Outcome; the transition decides the next step. Business rules (who may do what, validation, building domain objects or packets, conflict, time and versioning rules) live in use cases or policies of the module that owns the concept, never in the workflow. A workflow keeps only private functions that choose the next step or map a result to an Outcome, and no if sits directly in a when branch.
  4. perform(step) never decides what happens next; transition(step, outcome) never has side effects.
  5. Nobody holds a workflow instance. Callers create a command and hand it to the engine, which finds the one workflow registered for that command type.
  6. Modules form a strict line (below); a lower module never depends on a higher one.
  7. The usual layering rules still apply inside the modules: mappers only in mapper packages, repositories never call use cases or other repositories, data sources never call repositories.

Modules

Module Owns Main classes
api the public contract Chats depends on ConversationOrchestrator, awaitResult, ConversationCommand (DirectConversationCommand, GroupMessageCommand, GroupAdministrationCommand, ForwardMessage), ConversationState, ConversationResult, ConversationFailure, ConversationFailureReason, ConversationIndicatorObserver
engine the state-machine runtime WorkflowStateMachine, ConversationWorkflow, WorkflowCommand, WorkflowStep, Transition, WorkflowEngine, WorkflowRegistry, WorkflowLocks, ConversationFailureMapper
delivery outbox and transport progress, sending policy, mailbox and routing 2 workflows, DeliveryCommand, OutboxDeliveryStateRouter, OutgoingPacketSender, OutgoingRecipientRoutingResolver, GroupRoutingResolver, MailboxRuntime, TransportConversationIndicatorObserver
message Direct and Group messages, operations, polls, forwarding 22 workflows, MessageCommand, DirectMessageRepository, GroupMessageRepository, OutgoingMessageContentPolicy, DirectReauthorizationPort, PendingAuthorizationCleanupRunner
groupstate state an admin sets for the whole group: title, description, avatar, pin, expense board 12 workflows, GroupStateCommand, GroupPinRepository, GroupTitleRepository, GroupDescriptionRepository, GroupAvatarRepository, RequireGroupAdminContextUseCase, ClearGroupStateUseCase
membership group administration, joining a group, verification 14 workflows, MembershipCommand, GroupConversationRepository, GroupVerificationRepository, MembershipResultObserver
lifecycle how a direct chat opens, gets authorized, loses and regains its authorization 14 workflows in direct/, result/, recovery/, LifecycleCommand, runtime observers, WorkflowDirectReauthorization
incoming routing every decrypted packet to the workflow that applies it 30 workflows, IncomingCommand, ParkingIncomingEnvelopeProcessor, IncomingTransportMessageHandler, GroupPacketDispatcher, ControlPacketDispatcher, IncomingPacketCommandMapper

All module paths are :feature:conversationorchestration:<module>.

Layering

flowchart BT
    subgraph orchestration[":feature:conversationorchestration"]
        API[api]
        ENGINE[engine]
        DELIVERY[delivery]
        MESSAGE[message]
        GROUPSTATE[groupstate]
        MEMBERSHIP[membership]
        LIFECYCLE[lifecycle]
        INCOMING[incoming]
    end
    CHATS[":feature:chats<br/>(read + submit commands)"]
    SHARED[":shared<br/>(DI, startup tasks)"]
    FEATURES[":feature:identity / invite / membership /<br/>contacts / messaging / attachments / transport ..."]

    ENGINE --> API
    DELIVERY --> ENGINE
    MESSAGE --> DELIVERY
    GROUPSTATE --> MESSAGE
    MEMBERSHIP --> GROUPSTATE
    LIFECYCLE --> MEMBERSHIP
    INCOMING --> LIFECYCLE
    CHATS --> API
    SHARED --> INCOMING
    DELIVERY -. use cases / repositories .-> FEATURES
    MESSAGE -.-> FEATURES
    GROUPSTATE -.-> FEATURES
    MEMBERSHIP -.-> FEATURES
    LIFECYCLE -.-> FEATURES
    INCOMING -.-> FEATURES

Each module depends on the one below through api(...), so every layer sees api, engine and everything below it: delivery → message → groupstate → membership → lifecycle → incoming. Feature modules never depend on orchestration.

When a lower module needs behaviour of a higher one, it declares a port that the higher module implements. There is one: a Direct send without authorization must request a fresh invitation, so message declares DirectReauthorizationPort and lifecycle implements it with WorkflowDirectReauthorization, which submits LifecycleCommand.RequestReauthorization.

Package layout

Every workflow module uses the same layout:

<module>/src/commonMain/kotlin/.../conversationorchestration/<module>/
├── data/            datasource, mapper, model, packet (codec implementations), repository (implementations)
├── di/              <Module>Module.kt — Koin registrations
├── domain/
│   ├── command/     <Module>Command.kt — every internal command of the module (one sealed interface)
│   ├── model/       domain models
│   ├── packet/      packet codec interfaces
│   ├── policy/      pure rules
│   ├── repository/  repository interfaces
│   ├── usecase/     single operations used by workflow steps
│   └── workflow/    <topic>/<Name>Workflow.kt
└── runtime/         observers and long-running runners started by :shared

The engine

classDiagram
    direction LR
    class ConversationOrchestrator {
        <<interface, api>>
        +execute(command: ConversationCommand) Flow~ConversationState~
    }
    class WorkflowEngine {
        +execute(command: ConversationCommand) Flow~ConversationState~
        +run(command: WorkflowCommand, acquireLock) Result~ConversationResult~
        +run(command: ConversationCommand, acquireLock) Result~ConversationResult~
    }
    class WorkflowRegistry {
        +validate()
        +find(command) WorkflowStateMachine?
    }
    class WorkflowLocks {
        +withLock(key, action)
    }
    class WorkflowStateMachine~C, S, O~ {
        <<interface>>
        +commandType: KClass~C~
        +firstStep(command: C) S
        +perform(step: S) O
        +transition(step: S, outcome: O) Transition~S~
        +lockKey(command: C) String?
    }
    class ConversationWorkflow~C, S, O~ {
        <<interface>>
        C : ConversationCommand
    }
    class Transition~S~ {
        <<sealed>>
        Next(step)
        Complete(result)
        Reject(failure)
    }
    class WorkflowCommand {
        <<interface>>
    }
    class WorkflowStep {
        <<interface>>
        +name: String
    }
    ConversationOrchestrator <|.. WorkflowEngine
    WorkflowEngine --> WorkflowRegistry
    WorkflowEngine --> WorkflowLocks
    WorkflowRegistry --> "*" WorkflowStateMachine
    WorkflowStateMachine <|-- ConversationWorkflow
    WorkflowStateMachine ..> Transition
    WorkflowStateMachine ..> WorkflowStep
Class Responsibility
WorkflowStateMachine<C, S, O> One workflow: command type C, sealed Step type S, sealed Outcome type O.
ConversationWorkflow<C, S, O> Marker for workflows whose command is a public ConversationCommand, i.e. that Chats may start.
WorkflowCommand Marker for internal commands. Each module's sealed <Module>Command extends it.
WorkflowStep A step; its name (the class name) is published as ConversationState.Running(step).
Transition Next(step), Complete(result = ConversationResult.Done), Reject(failure); Reject.because(error) maps an exception.
WorkflowRegistry Maps each command type to its workflow. Reads all Koin-registered WorkflowStateMachine<*, *, *> lazily on first use (workflows depend on the engine, the engine on the registry). validate() fails on duplicates.
WorkflowEngine Runs a command's workflow step by step under its lock and publishes the states.
WorkflowLocks One mutex per lock key; workflows without a key run unrestricted. Not reentrant.
ConversationFailureMapper Turns an exception into a ConversationFailure.

The run loop

sequenceDiagram
    participant C as Caller
    participant E as WorkflowEngine
    participant R as WorkflowRegistry
    participant L as WorkflowLocks
    participant W as Workflow

    C->>E: execute(command) / run(command)
    E->>R: find(command)
    alt no workflow
        E-->>C: Failed(UNEXPECTED "No workflow handles ...")
    else workflow found
        E->>L: withLock(workflow.lockKey(command))
        E->>W: firstStep(command)
        loop until Complete or Reject (max 100,000 steps)
            E-->>C: Running(step.name)
            E->>W: perform(step)
            W-->>E: outcome
            E->>W: transition(step, outcome)
            W-->>E: Next(nextStep) | Complete(result) | Reject(failure)
        end
        E-->>C: Completed(result) or Failed(failure)
    end
  • execute(...) returns the Flow<ConversationState> (Running, then Completed or Failed). Chats uses the awaitResult(...) extension, which waits for the final state and returns Result<ConversationResult>.
  • run(...) is for callers inside orchestration. It returns Result<ConversationResult> and keeps the original exception as the failure, so a calling workflow can react to a specific domain error.
  • An exception thrown by perform ends the workflow with Failed(ConversationFailureMapper.toFailure(error)):
Exception ConversationFailureReason
ConversationFailureException its own failure
WorkflowEngineException (the engine's own invariants: step limit, a running state taken as final) UNEXPECTED
DirectChatAuthorizationRequiredException AUTHORIZATION_REQUIRED
IllegalArgumentException (require) INVALID_INPUT
IllegalStateException (check, error) NOT_ALLOWED
anything else UNEXPECTED
  • A workflow that exceeds 100,000 steps fails with WorkflowEngineException (UNEXPECTED, not NOT_ALLOWED); this only guards against a transition cycle, because workflows that walk a list (read receipts, queued messages) take a few steps per item. A command no workflow handles fails as UNEXPECTED too. Chats does not branch on the reason; it only shows the message.

Anatomy of a workflow

SendDirectMessageWorkflow (module message, command DirectConversationCommand.SendMessage), shortened:

/**
 * ValidateContent -> CheckBlockedContact -> CheckPendingIdentityChange -> CheckAuthorization
 * -> [InspectInvitation -> RequestReauthorization] -> ResolveConversation
 * -> DeliverMessage | (FindQueueConversation -> LoadQueueRecipient -> StoreWaitingMessage).
 *
 * An authorized contact gets the message immediately. Without authorization the message waits in
 * WAITING_FOR_AUTHORIZATION, and the workflow starts a fresh invitation when none is pending.
 */
class SendDirectMessageWorkflow internal constructor(
    private val engine: WorkflowEngine,
    private val contactBlocklistRepository: ContactBlocklistRepository,
    ...
) : ConversationWorkflow<DirectConversationCommand.SendMessage, SendDirectMessageWorkflow.Step, SendDirectMessageWorkflow.Outcome> {

    override val commandType = DirectConversationCommand.SendMessage::class

    override fun firstStep(command: DirectConversationCommand.SendMessage): Step = Step.ValidateContent(command)

    // exactly one operation per step, reports what happened
    override suspend fun perform(step: Step): Outcome =
        when (step) {
            is Step.CheckBlockedContact ->
                if (contactBlocklistRepository.isBlocked(step.command.contactId)) Outcome.ContactBlocked
                else Outcome.ContactNotBlocked(step.content)
            is Step.DeliverMessage -> {
                engine.run(MessageCommand.DeliverDirectMessage(step.conversationId, step.content, ...)).getOrThrow()
                Outcome.MessageDelivered
            }
            ...
        }

    // pure: decides the next step from the outcome
    override fun transition(step: Step, outcome: Outcome): Transition<Step> =
        when (outcome) {
            Outcome.ContactBlocked -> Transition.Reject(ConversationFailure(AUTHORIZATION_REQUIRED, "..."))
            is Outcome.ContactNotBlocked -> Transition.Next(Step.CheckPendingIdentityChange(outcome.content, step.command))
            Outcome.MessageDelivered -> Transition.Complete(ConversationResult.DirectMessageSent)
            ...
        }

    sealed interface Step : WorkflowStep {
        val command: DirectConversationCommand.SendMessage
        data class ValidateContent(override val command: ...) : Step
        data class CheckBlockedContact(val content: OutgoingMessageContent, override val command: ...) : Step
        ...
    }

    sealed interface Outcome {
        data class ContentValidated(val content: OutgoingMessageContent) : Outcome
        data object ContactBlocked : Outcome
        ...
    }
}
  • Every Step carries the command plus all data the step needs; every Outcome carries the data the next decision needs. There is no mutable workflow state.
  • firstStep may branch on the command. transition may branch on the outcome's data (for example DeliveryPlan.Send vs DeliveryPlan.Queue).
  • The KDoc lists the step chain. The generated diagram of the same workflow:
stateDiagram-v2
    [*] --> ValidateContent
    ValidateContent --> CheckBlockedContact : ContentValidated
    CheckBlockedContact --> Rejected : ContactBlocked
    CheckBlockedContact --> CheckPendingIdentityChange : ContactNotBlocked
    CheckPendingIdentityChange --> ResolveConversation : IdentityChangePending
    CheckPendingIdentityChange --> CheckAuthorization : NoIdentityChangePending
    CheckAuthorization --> InspectInvitation : AuthorizationMissing
    CheckAuthorization --> ResolveConversation : Authorized
    InspectInvitation --> ResolveConversation : InvitationPending
    InspectInvitation --> RequestReauthorization : InvitationRetryable
    InspectInvitation --> Rejected : InvitationUnavailable
    RequestReauthorization --> ResolveConversation : Reauthorized / ReauthorizationFailed
    ResolveConversation --> DeliverMessage : ConversationResolved (Send)
    ResolveConversation --> FindQueueConversation : ConversationResolved (Queue)
    DeliverMessage --> [*] : MessageDelivered
    FindQueueConversation --> LoadQueueRecipient : QueueConversationFound
    LoadQueueRecipient --> StoreWaitingMessage : QueueRecipientLoaded
    StoreWaitingMessage --> [*] : MessageQueued
    Rejected --> [*]

Commands

Public commands (Chats)

Defined in :feature:conversationorchestration:api as one sealed hierarchy under ConversationCommand:

Family Commands
DirectConversationCommand OpenConversation, ReconnectConversation, DeleteConversation, SendMessage, EditMessage, DeleteMessage, ToggleReaction, RetryMessage, MarkConversationRead, SendIndicator
GroupMessageCommand SendMessage, EditMessage, DeleteMessage, ToggleReaction, VotePoll, ClosePoll, RetryMessage, MarkConversationRead, SendIndicator, PinMessage, UnpinMessage, ActivateExpenses, CloseExpenses, CreateExpense
GroupAdministrationCommand CreateGroup, AddMembers, RemoveMember, PromoteMember, TransferAdminAndLeave, LeaveGroup, DeleteGroup, ChangeTitle, ChangeDescription, ChangeAvatar, RemoveAvatar, VerifyMember, SynchronizeVerification
ForwardMessage forwards message parts to ForwardTarget.DirectConversation, GroupConversation or Contact

Internal commands

Each module lists every internal job in one sealed interface in domain/command/. That file is the overview of what the module can be asked to do internally:

Sealed interface Module Commands
DeliveryCommand delivery ApplyDeliveryEvent, RecordPreparedTransport
MessageCommand message DeliverDirectMessage, ActivateAuthorizedConversation, DiscardWaitingDirectMessages, ReleaseWaitingDirectMessages, FlushQueuedGroupMessages
GroupStateCommand groupstate PublishGroupAvatar, SendCurrentGroupMetadata, SendCurrentGroupPin
MembershipCommand membership RefreshOwnedGroupVerification, HandleGroupInvitationResult, HandleMembershipResult, RecoverAcceptedGroupInvitation, ResumeApprovedGroupJoin
LifecycleCommand lifecycle RequestReauthorization, RevokePeerExchange, StartDirectInvitation, HandleContactBlocked, HandleDirectInvitationResult, HandleIdentityResult, RecordIncomingDirectInvitation, RecoverAcceptedDirectInvitation, RecoverAuthorizedConversation, AcceptApprovedIdentityChange, StartRecoveryInvitation
IncomingCommand incoming RouteIncomingPacket, ApplyRemoteMessageOperation, ApplyParkedMessageOperations, CompleteManualIdentity and one Receive* command per packet type (27)

Starting a workflow

// Chats: public command
orchestrator.awaitResult(DirectConversationCommand.SendMessage(contactId, parts))

// runtime observer or another workflow: internal command
engine.run(LifecycleCommand.HandleContactBlocked(contactId))

// a workflow reusing a public workflow (forwarding sends a message)
engine.run(GroupMessageCommand.SendMessage(groupId, parts))

// nested inside a workflow that already holds the same lock
engine.run(MessageCommand.FlushQueuedGroupMessages(groupId), acquireLock = false)

How Chats uses orchestration

sequenceDiagram
    participant VM as Chats ViewModel / controller
    participant ORCH as ConversationOrchestrator
    participant ENG as WorkflowEngine
    participant WF as SendGroupMessageWorkflow
    participant DB as Database
    participant READ as Chats read repository

    VM->>ORCH: awaitResult(GroupMessageCommand.SendMessage(...))
    ORCH->>ENG: execute(command)
    ENG->>WF: steps (validate, store, encrypt, enqueue ...)
    WF->>DB: write through orchestration repositories
    ENG-->>VM: Result<ConversationResult>
    DB-->>READ: Room Flow emits
    READ-->>VM: updated conversation

Chats never gets data back from a command except the small ConversationResult (for example ConversationOpened(conversationId) or GroupCreated(groupId)). The UI updates because Chats observes the database through its own read repositories:

Chats read repository Reads Written by (orchestration)
DirectConversationReadRepository a direct chat, its contact and history message, lifecycle (DirectConversationRepository, DirectMessageRepository)
GroupConversationReadRepository a group chat, members, local membership timeline membership (GroupConversationRepository), message
GroupPinReadRepository the current pin and pinned attachments groupstate (GroupPinRepository)
GroupAvatarReadRepository the group avatar groupstate (GroupAvatarRepository)
GroupDescriptionReadRepository the group description groupstate (GroupDescriptionRepository)
GroupVerificationReadRepository verification rows membership (GroupVerificationRepository)
ConversationOverviewRepository, MessageHistoryRepository chat list, history paging all writers

The read side and the write side use different repositories over the same tables on purpose: Chats cannot write, and orchestration needs no UI models. Typing indicators are transient and never stored, so Chats observes them through ConversationIndicatorObserver (implemented by TransportConversationIndicatorObserver in delivery).

Incoming packets

flowchart LR
    RUN["MailboxPendingSynchronizer<br/>(:feature:conversationorchestration:delivery)"] --> PROC[ParkingIncomingEnvelopeProcessor]
    PROC --> HANDLER["IncomingTransportMessageHandler<br/>decrypt + decode"]
    HANDLER -->|"IncomingCommand.RouteIncomingPacket"| ROUTE[RouteIncomingPacketWorkflow]
    ROUTE -->|ReceiveDirectMessage| DIRECT[ReceiveDirectMessageWorkflow]
    ROUTE -->|ApplyRemoteMessageOperation| OP[ApplyRemoteMessageOperationWorkflow]
    ROUTE -->|ApplyDeliveryEvent| RECEIPT[ApplyDeliveryEventWorkflow]
    ROUTE --> GROUP[GroupPacketDispatcher]
    ROUTE --> CONTROL[ControlPacketDispatcher]
    GROUP -->|"toGroupCommand()"| GW["ReceiveGroup* / ReceiveVerification*<br/>(15 workflows)"]
    CONTROL -->|"toControlCommand()"| CW["ReceiveContact* / ReceiveGroupInvite* /<br/>ReceiveManualIdentity ... (11 workflows)"]
    CONTROL --> RECEIPTS["HandleIdentityVerificationReceiptUseCase"]
    CONTROL --> OTHER["ProtocolPacketHandler<br/>(packets of other features)"]

RouteIncomingPacketWorkflow decides the path:

direct message:  CheckDirectAuthorization -> EnsureDirectConversation -> ReceiveDirectMessage | ApplyDirectOperation
group packet:    CheckGroupAccepted -> CheckGroupReady -> DispatchGroupPacket
receipt:         ApplyReceipt
anything else:   FindControlConversation -> DispatchControlPacket

Rejected packets

A workflow step that finds an incoming packet not allowed or invalid rejects it with rejectUnless, rejectIfNull or rejectInvalid (incoming/domain/error/IncomingRejection.kt). They throw IncomingMessageRejectedException, so the envelope is acknowledged and dropped. Every other failure keeps the envelope for redelivery, so a step must only reject what can never succeed.

Operations that overtake their message

An edit, delete, reaction, poll vote or poll close can arrive before the message it refers to, for example when a reconnect delivers the mailbox out of order. ApplyRemoteMessageOperationWorkflow then parks the operation (FindTarget -> Park) in parked_message_operations and the envelope is acknowledged. Once ReceiveDirectMessageWorkflow or ReceiveGroupMessageWorkflow has stored a message, its last step ApplyParkedOperations runs ApplyParkedMessageOperationsWorkflow, which applies the waiting operations in the order they were sent through the normal operation workflow (so the same sender and admin checks apply) and removes each one. An operation that is not allowed is dropped as it would have been on arrival. Parked operations whose message never arrives are deleted after 7 days. ParkedMessageOperationsTest covers these cases.

Edits are ordered by their editedAtEpochMilliseconds, not by arrival: ReplaceText calls replaceTextIfNewer, which keeps the stored text when its edit time is the same or later (a live copy of edit 2 followed by a mailbox copy of edit 1 keeps edit 2). Local edits always replace the text and record the local time.

Unreadable envelopes

An envelope that cannot be decrypted or decoded is kept as a placeholder ("Unable to decrypt secure message") by StoreUnreadableMessageUseCase, under the same rule as readable direct messages: only a contact with an established exchange (ResolveDirectPeerAccessUseCase returns AUTHORIZED) gets it, and only in its existing direct chat. A group-only member, a pending, unknown or blocked contact gets no new chat; the envelope is dropped and logged (a pending one is not parked, because the key that failed would fail again on replay). The placeholder ID is derived from the SHA-256 of contact and payload, so a redelivered envelope adds no second row.

Packets that arrive too early

Some packets are valid but cannot be handled yet, and failing them would lose them (a live envelope that is not acknowledged is gone) or block a mailbox for 7 days:

  • a group packet whose epoch is not installed yet: CheckGroupReady asks CheckGroupPacketReadyUseCase, which uses GroupPacketEpochPolicy (messages, metadata, pins, activations and acknowledgements need their epoch; verification packets need any installed epoch; welcomes, deletions and removals never wait). This covers a packet of a future epoch and any group packet before the first welcome of a group the user just joined.
  • a direct message or operation of a contact whose invitation is still in flight (sent, or accepted and waiting for READY): ResolveDirectPeerAccessUseCase returns PENDING. Unknown, blocked and revoked contacts stay DROPPED.

RouteIncomingPacketWorkflow then rejects the transition with IncomingPacketDeferredException(wait), where the DeferredPacketWait is GroupEpoch(groupId, epoch) or ContactExchange(contactId). ParkingIncomingEnvelopeProcessor parks the whole envelope in parked_envelopes under the wait's key (deduplicated by envelope ID, entries older than 7 days are purged) and reports it as processed, so every transport acknowledges it. After each envelope that was handled, the processor runs ReplayParkedEnvelopesUseCase: for every key whose wait is met (CheckDeferredPacketReadyUseCase: the epoch or a later one is installed, or the group membership was deleted; the exchange is no longer pending) it runs the parked envelopes through the same processing path in arrival order. A replay that is handled, rejected or whose sender is unknown removes the envelope; one that has to wait again is parked again under its new key and keeps its place; one that fails stays for the next replay. A welcome envelope therefore releases the group's early packets, a ContactInviteAccepted or ContactReady envelope the contact's early messages.

Replays run in the processor, outside every workflow, and one at a time (ReplayParkedEnvelopesUseCase holds its own mutex, never a WorkflowLocks key), so they cannot nest inside the lock of the workflow that met the wait. ParkedEnvelopesTest covers parking, readiness and replay.

Every packet type has its own command, for example IncomingCommand.ReceiveGroupTitle(context: IncomingPacketContext, packet: GroupTitleUpdatedPacket). IncomingPacketCommandMapper (toGroupCommand(), toControlCommand()) maps a ReceivedPacket to that command; the dispatchers only submit it with engine.run(command). Direct and Group packets never share a workflow.

Runtime observers

Durable result streams of other features and of the outbox enter orchestration through runtime classes started by :shared startup tasks. Each observer only builds a command and submits it:

Runtime class Module Listens to Submits
IdentityResultObserver lifecycle identity results LifecycleCommand.HandleIdentityResult, RecoverAuthorizedConversation (on start)
InvitationResultObserver lifecycle invitation results LifecycleCommand.HandleDirectInvitationResult, RecoverAcceptedDirectInvitation, MembershipCommand.HandleGroupInvitationResult, RecoverAcceptedGroupInvitation
ContactBlockObserver lifecycle blocked contacts LifecycleCommand.HandleContactBlocked
ApprovedIdentityReconnectionObserver lifecycle approved identity changes (retried by ApprovedReconnectionRetryWorker) LifecycleCommand.StartRecoveryInvitation
MessagingTransportResultObserver lifecycle durable messaging failures hands invitation packets to InvitationTransportFailureHandler
MembershipResultObserver membership membership results MembershipCommand.HandleMembershipResult
OutboxDeliveryStateRouter delivery outbox state changes DeliveryCommand.RecordPreparedTransport, ApplyDeliveryEvent
PendingAuthorizationCleanupRunner message direct messages waiting for authorization discards expired ones (DiscardDirectMessagesUseCase)

The observers run for the app's lifetime, so a failure must not end them. Each subscribes again after a failed observation with backoff (retryWithBackoff in core.util.flow: 1 s doubling up to one minute). The result observers (IdentityResultObserver, InvitationResultObserver, MembershipResultObserver, ContactBlockObserver) collect with collectUntilHandled: a result counts as seen only once its workflow succeeded, and a snapshot with a failed result is handled again after the backoff or as soon as a newer snapshot arrives; a running workflow is never cancelled by a newer snapshot. What a result present at start triggers (recovery instead of the normal handling) is decided once for that start snapshot and kept for its retries. MessagingTransportResultObserver keeps its own 5-second retry for unacknowledged failures. PendingAuthorizationCleanupRunner logs a failed discard and keeps running.

Startup order (ApplicationStartupTasks): ValidateConversationWorkflowsStartupTask runs right after the crypto runtime and calls WorkflowRegistry.validate(); the Start*ObserverStartupTasks then start the observers once the local identity is ready.

Locking

lockKey(command) returns a key; WorkflowLocks keeps one mutex per key. Workflows sharing a key never run at the same time; workflows without a key run unrestricted.

Key helper Key Serializes
groupSendLockKey group-send:<groupId> handing group messages to the outbox (send, flush), keeps their order; editing and deleting an own group message nobody has received yet (EditUnsentGroupMessage, DeleteUnsentGroupMessage), so a running flush never sends a queued message from before the change
groupAdministrationLockKey (groupstate) group-administration:<groupId> everything an admin publishes to the members: membership changes that rotate the epoch (add, remove, promote, transfer, leave, delete), publishing a locally saved title, description, avatar or pin (PublishGroupTitle, PublishGroupDescription, PublishGroupAvatar, PublishGroupPin), closing the expense board and the replay of the current metadata to a new member
groupVerificationLockKey group-verification:<groupId> verification changes, also from incoming packets
directReleaseLockKey direct-release:<contactId> releasing or discarding a contact's waiting Direct messages, storing a new waiting message (StoreWaitingDirectMessage) and editing or deleting an own Direct message that was never sent (EditOwnDirectMessage, DeleteOwnDirectMessage)
DELIVERY_LOCK_KEY message-delivery all delivery-state updates
ActivateGroupExpensesWorkflow.lockKey group-expense:<groupId> activating group expenses, so a second tap sees the board the first one pinned instead of sending a second board

A lock serializes only the sending part of a user's change, never the local change itself: the user sees the new message, the edit or delete, the new title or pin at once, even while a send uploads attachments under group-send or a membership change holds group-administration. Long operations that hold a key: a group send or flush (attachment uploads) holds group-send; a membership change holds group-administration; a release of waiting Direct messages holds direct-release.

Group state shares the administration key so a metadata packet is always signed for the epoch and the recipients that are current when it is queued: before, a removal could rotate the epoch between AuthorizeAdmin and the enqueue, the packet went out at the old epoch to the removed member and members on the new epoch dropped it. The public ChangeTitle, ChangeDescription, ChangeAvatar, RemoveAvatar, PinMessage and UnpinMessage take no key: they authorize the admin, save the new state locally as unpublished (publishPending) and then nest the Publish* command, which alone holds the key. It loads the unpublished state (nothing to do when it is published or was replaced by a newer remote state), signs it with the current epoch, queues it and marks it published only while it is still the stored version. A failed or interrupted publish leaves the state unpublished; the same change again (the "unchanged" short-cut does not apply to an unpublished state) or UnpublishedGroupStateRunner after the next start publishes it. Because local saves take no lock, every save of a title, description, avatar or pin, local or received, is a compare-and-set (SaveGroupTitleUseCase and the like, GroupTitleRepository.saveIfVersion): it stores the version only while the stored version is still the one it checked as older, and checks again when another save won the race, so a concurrent older save can never overwrite a newer one. A received pin also re-checks the expense slot. Nesting under this key (checked over every engine.run): the admin workflows nest only RefreshOwnedGroupVerification (group-verification); CloseGroupExpensesWorkflow nests SendMessage (group-send, which nests the flush with acquireLock = false and delivery events under message-delivery); SendCurrentGroupMetadataWorkflow (run by ReceiveGroupReadyAcknowledgementWorkflow, which has no key) nests SendCurrentGroupPin/SendCurrentGroupExpenses, which take no key; the four Publish* workflows nest nothing (their enqueue does not record delivery events). The Publish* commands are nested only by the public workflows above and by UnpublishedGroupStateRunner, none of which holds a key; PinMessage itself is nested only by ActivateGroupExpenses, which holds group-expense (taken by nothing else; it nests SendMessage under group-send and PinMessage, whose PublishGroupPin takes group-administration, so the order is group-expense → send or group-expense → administration). No holder of group-send, group-verification or message-delivery runs an administration-keyed workflow, so the keys are only ever taken in the order administration → verification or administration → send → delivery.

Group edits and deletes take the send key only for a message nobody has received yet (no recipient states): the public EditMessage/DeleteMessage take no key and send the operation of a message members already have right away; for an unsent one they nest EditUnsentGroupMessage/DeleteUnsentGroupMessage, which hold group-send, look at the recipients again (a flush may have sent the message meanwhile, then the change is sent like any other) and otherwise change the message locally only, so the flush sends the new text or nothing. A sent message never becomes unsent again, so the unlocked path cannot race a flush. Nesting: the two unsent workflows nest nothing; the public edit and delete are nested by no workflow.

The release key protects waiting Direct messages: before, a send could decide "authorization missing", a release run in between found nothing, and the stored WAITING message stayed until the next identity event; an edit or delete of a WAITING message could land between the release reading the message and sending it, so the old text went out or a deleted message was written back. SendDirectMessageWorkflow takes no key, so a message to an authorized contact is stored and delivered without waiting for an earlier send (attachment upload) or a release. Only the waiting path nests StoreWaitingDirectMessage, which holds the key and checks authorization again with the rule the release uses: if the contact became authorized meanwhile nothing is stored and the send delivers normally, otherwise the WAITING message is stored before any later release can look for it. The public EditMessage/DeleteMessage take no key either: a sent message cannot become waiting again, so its edit or delete is sent right away; only a message that was never sent nests EditOwnDirectMessage/DeleteOwnDirectMessage, which hold the key and read the message again (one released just before gets a normal edit or delete). The release additionally reads each message again and moves it to QUEUED with a conditional update, skipping one that is gone or no longer waiting. Nesting under this key (checked over every engine.run): Release and Discard nest only delivery events (message-delivery); StoreWaitingDirectMessage and the edit and delete workflows nest nothing. SendDirectMessageWorkflow (no key) nests StoreWaitingDirectMessage, DeliverDirectMessage (no key, nests only delivery events) and RequestReauthorization (no key; it nests ReconnectConversation, StartDirectInvitation and RevokePeerExchange, none keyed and none submitting a message command). ActivateAuthorizedConversation, which nests the release, is run only by identity/invitation result handling and recovery, never by a holder of the key; the direct send is nested only by forwarding and auto replies, which hold no key. The order is always direct-release → delivery.

Locks are not reentrant. A workflow that submits a nested command with the same key passes acquireLock = false. The full mapping of workflows to locks is in the catalog.

Dependency injection

Every workflow is registered in its module's Koin module with the same line:

singleOf(::SendDirectMessageWorkflow) { bind<WorkflowStateMachine<*, *, *>>() }

conversationEngineModule provides WorkflowLocks, WorkflowRegistry { getAll<WorkflowStateMachine<*, *, *>>() }, WorkflowEngine (bound to ConversationOrchestrator). ApplicationModules loads conversationEngineModule, conversationDeliveryModule, conversationMessageModule, conversationGroupStateModule, conversationMembershipModule, conversationLifecycleModule and conversationIncomingModule.

Adding a workflow

  1. Pick the lowest module that owns the behaviour (delivery, message, groupstate, membership, lifecycle, incoming). If it needs a higher module, declare a port in the lower one.
  2. Add the command:
  3. user action → a new data class in the matching ConversationCommand family in api;
  4. internal job → a new nested data class in the module's <Module>Command;
  5. new packet type → a new IncomingCommand.Receive* plus a branch in IncomingPacketCommandMapper.
  6. Write <Name>Workflow implementing ConversationWorkflow (public) or WorkflowStateMachine (internal) with nested sealed Step and Outcome. One operation per perform branch, pure transition, step chain in the KDoc. Put any rule the step needs into a use case or policy of the owning module first (rule 3).
  7. Add a lockKey if it must not overlap with other workflows of the same group or contact.
  8. Register it: singleOf(::<Name>Workflow) { bind<WorkflowStateMachine<*, *, *>>() }.
  9. Test the transitions and the workflow end to end through WorkflowEngine (see WorkflowEngineTest).
  10. Commit. The pre-commit hook runs workflowCatalog and stages docs/architecture/workflows; qualityCheck (pre-push and CI) runs verifyWorkflowCatalog and fails if the catalog is out of date.

Workflow catalog

The catalog is generated by ./gradlew workflowCatalog (build-logic: GenerateWorkflowCatalogTask, WorkflowSourceParser, WorkflowCatalogRenderer). It parses every *Workflow.kt of the workflow modules: the supertype gives the command and whether Chats may start it, firstStep/perform/transition give the diagram, the constructor gives the dependencies and the KDoc the step chain. It runs automatically:

When Task Effect
git commit (pre-commit hook) ktlintFormat workflowCatalog regenerates and stages the catalog
./gradlew qualityFix / quality workflowCatalog regenerates the catalog
git push (pre-push hook), CI, ./gradlew check / qualityCheck verifyWorkflowCatalog fails if the catalog does not match the code
scipts/update-docs.bat architectureReport workflowCatalog regenerates all generated docs