Orchestrator Flow¶
How the orchestrator manages the full lifecycle of a workflow instance — from case.created through to case.workflow.completed.
Concepts¶
- WorkflowDefinition — a versioned, org- and product-scoped graph of steps. Steps reference a
kind(e.g.gate_on_consent,run_ai_review,request_human_review,emit_final). Definitions are authored as drafts, validated, then published. Only one definition per org+product can beactiveat a time. - WorkflowInstance — a running snapshot of a definition for a specific case. The definition is snapshotted at creation so in-flight instances are not affected by definition changes.
- WorkflowStep — one executed step within an instance. Records the step's
kind, dispatchedcorrelationId, status, and context snapshot. - WorkflowEvent — an idempotency inbox. Every consumed completion event is written here before processing; duplicate events are silently dropped.
Lifecycle steps¶
-
Trigger —
case.createdarrives on the Redis Stream. TheCaseCreatedConsumerwrites aWorkflowEventrow (idempotency), then callsDefinitionsService.findActiveFor(orgId, productId). -
Instantiation —
InstancesService.createcreates aWorkflowInstancewith a snapshot of the definition.TimeoutSchedulerServiceschedules a BullMQ deadline job using the definition'sworkflowTimeoutSeconds. If the instance mode isactive,InstancesService.startimmediately dispatches the first step. -
Dispatch —
DispatcherService.dispatchmaps a step'skindto an event type (e.g.gate_on_consent→consent.check.requested) and callspublishEventfrom@sa-platform/events. AcorrelationId(UUID) is generated and stored with theWorkflowSteprow so the completion event can be matched back. -
Completion —
CompletionEventsConsumerreads from all completion streams (consent.check.completed,ai_review.completed,ai_review.failed,human_review.completed,human_review.failed, etc.) in a single loop. It resolves thecorrelationIdto aWorkflowInstance+WorkflowStep, determines the outcome (on_pass,on_fail,on_complete,on_failure), and callsInstancesService.handleCompletion. -
Transition —
handleCompletionevaluates the definition's transition expression for the current step and outcome. If more steps remain, it dispatches the next. If the resolved step kind isemit_final, the dispatcher publishescase.workflow.completedand the instance is markedcompleted. -
Manual interventions — Operators can halt, resume, cancel, supersede, or retry-step an instance via the
workflow-instancesREST endpoints. Halt and resume transitions are recorded inWorkflowInterventionwith an optionalReasonCode.
Step kinds and dispatched events¶
| Step kind | Dispatched event | Completion event(s) |
|---|---|---|
gate_on_consent |
consent.check.requested |
consent.check.completed |
require_images |
case.images_check.requested |
case.images_check.completed |
run_ai_review |
ai_review.requested |
ai_review.completed, ai_review.failed |
request_human_review |
human_review.requested |
human_review.completed, human_review.failed |
set_case_status |
case.set_status.requested |
case.set_status.completed |
emit_webhook |
workflow.webhook_emit.requested |
workflow.webhook_emit.completed |
emit_final |
case.workflow.completed |
(terminal) |
Code paths¶
case.createdconsumer:services/orchestrator/src/consumers/case-created.consumer.ts- Completion events consumer:
services/orchestrator/src/consumers/completion-events.consumer.ts - Dispatcher:
services/orchestrator/src/dispatcher/dispatcher.service.ts - Instance lifecycle:
services/orchestrator/src/instances/instances.service.ts - Manual interventions:
services/orchestrator/src/instances/interventions.service.ts - Timeout scheduler:
services/orchestrator/src/scheduler/timeout-scheduler.service.ts
Related diagrams¶
The Mermaid sequence source is at docs/diagrams/orchestrator-sequence.mmd.