Engine, approvals & SLA
The runtime lives in Genie.Engine/Features/Workflow. IWorkflowEngine (implemented by
WorkflowEngine) orchestrates the lifecycle; focused collaborators handle approvals
(WorkflowApprovalResolver), guards (WorkflowExpressionEvaluator), and background SLA checks
(WorkflowSlaMonitor + its hosted service). This page describes how a definition actually runs. For
the XML that drives it, see Authoring a workflow.
Instance lifecycle
Section titled “Instance lifecycle”StartAsync(workflowName, entityType, entityId, contextData, companyId?):
- Loads the active definition by
Name, parses its XML, and finds theStartnode. - Pins the version — records the definition’s current
VersionHashon the new instance so later edits never change how this run behaves (minting a first version lazily for pre-versioning definitions). See Drafts, versioning & company scoping. - Serialises
contextDatato the instance’sContextData(JSON) — every@variablethe workflow reads later comes from here. - Stamps the tenant — every instance query (the workflow grids, WF Tasks, pending approvals,
and the entity lookup behind
transitionEntityWorkflow) filtersCompanyId = @SessionCompanyId, so the instance and its state rows carry: the explicitcompanyIdargument when supplied (programmatic hosts pick the tenant in code), else the session company for authenticated starts, else the definition’s company for anonymous starts (e.g. a public-API host callingIWorkflowEngine.StartAsyncdirectly). - Creates the
WorkflowInstance(Status = "Active") and its initialWorkflowInstanceState, then auto-advances fromStartthrough its single outgoing transition.
From there the engine calls AdvanceAsync for each edge it follows. AdvanceAsync:
- closes the current state (
Completed,ExitedAtstamped) and writes aWorkflowTransitionLogrow, - moves
CurrentNodeKeyto the target and opens a new state, - resolves node assignment (from node attributes, else inherited from the instance),
- applies the transition’s sticky
FlowStatuswhen non-empty, - if the target is
End, sets the instanceCompletedand clears assignment, - runs the bound-record update then the transition’s
<Sql>, and saves — see below, - then auto-advances again when the target is a
DecisionorActionnode.
Authored SQL on a transition
Section titled “Authored SQL on a transition”When the Start node declares a <Binding>
or the edge carries a <Sql>, AdvanceAsync runs them in one transaction with the instance save:
- the bound record’s
UPDATE(skipped when the transition carries no status), - the transition’s
<Sql>— so it can extend or override what the binding just wrote, SaveChangesAsync, then commit.
A failure anywhere in that sequence rolls the whole transition back: the instance keeps its
previous node and FlowStatus, no WorkflowTransitionLog row is written, and the exception surfaces
through the standard error envelope. Two consequences worth knowing when authoring:
- Your SQL runs before the instance row is committed, so read the supplied parameters
(
@EntityId,@FlowStatus,@TransitionLabel,@Comment, context variables) rather than queryingWorkflow.Instancesfor the new state. - A transaction is only opened when there is authored SQL to run and none is already ambient — a caller that already owns one (e.g. a form submit whose SQL fired a workflow callback) keeps its own.
A binding whose UPDATE matches no row is logged as a warning, not an error: the flow’s own state
is still valid, and it almost always means the binding points at the wrong entity, primary key, or
tenant. The tenant predicate uses the instance’s own CompanyId, not the ambient session, so
auto-advance and background work (SLA escalation, hosted services) scope correctly with no session. Both this and Action-node ExecuteSql go through the provider-agnostic ADO path, so authored
SQL runs unchanged on SQL Server and PostgreSQL and literal braces need no escaping.
Approval and Standard nodes pause — the instance waits for an external TransitionAsync or
SubmitApprovalAsync call.
TransitionAsync(instanceId, transitionLabel, …) finds the matching outgoing transition (by Label,
falling back to To), evaluates its Expression guard, optionally merges caller data into the
context, and advances. CancelAsync sets the instance Cancelled.
Approvals
Section titled “Approvals”When the engine enters an Approval node it creates one pending WorkflowApproval record per
approver rule:
Role— one record per role; any member can claim and decide it. The instance is also assigned to that role (looked up in[Identity].[Roles]) so it appears in the role’s queue.User— one record for a specific user id.FieldValue— resolves a user id from a@ContextVar.
Approvers act through the built-in Pending Approvals list and the WF_ApprovalForm (the Approval
node defaults its activity to that form) — no custom form required. A decision comes in through
SubmitApprovalAsync(instanceId, nodeKey, userId, decision, comment).
Gate policies (<Type>)
Section titled “Gate policies (<Type>)”After each decision, CheckApprovalCompletionAsync evaluates the node’s ApprovalConfig.Type against
the pending / approved / rejected records created since the node was last entered:
Type |
Approves when… | Rejects when… |
|---|---|---|
Any |
the first approval arrives (remaining pending records are marked Ignored). |
the first rejection arrives. |
Sequential (default) |
all records are approved and none pending. | any rejection arrives (immediately). |
Parallel |
all have decided and none rejected. | all have decided and at least one rejected. |
When the gate is satisfied the engine follows OnApprove / OnReject — matching those values against
the transition Labels leaving the node — and calls AdvanceAsync.
Action handlers
Section titled “Action handlers”Action nodes execute by ActionType (case-insensitive), then auto-advance:
ExecuteSql— the<Payload>SQL runs on the same provider-agnostic path as transition<Sql>, parameterised with@RecordIdand@EntityId(both the entity id),@InstanceId,@FlowStatus, the session parameters, and every context variable (@ContextVar). Context keys that aren’t valid SQL parameter names (e.g. containing spaces) are skipped.AppNotification— parses<To>/<Message>, renders both with Handlebars over the context, resolves recipients to usernames, and insertsNotificationStorerows (Type = App) delivered over SignalR/web-push.SendEmail— parses<To>/<CC>/<Subject>/<MailBody>, renders all four with Handlebars (so{{Email}}-style recipients from context work), then resolves recipient emails and queues aNotificationStorerow (Type = Email) for the background email worker. Delivery requires SMTP configured underGenie:Notifications:Email.
Recipient tokens (Role:Name, @ContextVar, literal email) are described in
Authoring — Action node. See also
Notifications for delivery workers and channels.
Custom action handlers
Section titled “Custom action handlers”Any other ActionType runs a handler the host app registers as a keyed service:
builder.Services.AddGenieWorkflowAction<CreditScoringHandler>("credit-scoring");
public sealed class CreditScoringHandler(IBureauClient bureau) : IWorkflowActionHandler{ public async Task<WorkflowActionResult> ExecuteAsync(WorkflowActionContext context, CancellationToken ct) { var customerId = context.WorkflowContext.Variables["CustomerId"]; var score = await bureau.ScoreAsync(customerId, ct); return WorkflowActionResult.Ok(new Dictionary<string, object?> { ["CreditScore"] = score }); }}<Node Key="Score" Type="Action" Label="Score"> <ActionConfig> <ActionType>credit-scoring</ActionType> <Payload><Threshold>700</Threshold></Payload> </ActionConfig></Node>The key matches case-insensitively. The built-in types above can’t be overridden. The handler’s
WorkflowActionContext contains:
WorkflowContext: the instance id, the workflow name,EntityType/EntityId, the node key, the sessionCompanyIdand user, and the contextVariables.Payload: the node’s raw<Payload>XML.SqlParameters: the same parameter setExecuteSqlgets.
A handler can also route: WorkflowActionResult.Transition names the outgoing transition to take
(by label, then by target node key), and ExceptTransitions lists transitions the default route skips,
such as an error branch.
WorkflowActionResult.Outputs are merged into the instance’s context data, so later nodes, SQL
(@CreditScore), templates ({{CreditScore}}) and decision expressions can read them. An unregistered
ActionType, or a handler that returns Fail(...), throws and fails the transition. It is no longer
silently skipped.
The engine ships two keyed handlers of its own, CallWebhook and Script. Both are described in
Webhooks, scripts & custom actions.
Designer editor services — /api/v1/genie/workflow-designer
Section titled “Designer editor services — /api/v1/genie/workflow-designer”WorkflowDesignerToolsController backs the designer’s code editors. It returns structure and
diagnostics only: no secret values and no record values. The designer loads sample record values
through the object endpoints, under their usual permissions.
| Endpoint | Returns |
|---|---|
POST scope { DefinitionXml, NodeKey } |
What a node can reference: upstream outputs (CallWebhook maps / OnError, Script Outputs["X"]), nearest first; template roots; SQL parameters; secret and connection names; auth types; registered action types; script languages; whether scripting is enabled; allowed hosts. |
POST script/diagnostics { Code } |
Compiler and allowlist diagnostics, with 1-based line/column positions. |
POST script/completions { Code, Line, Column } |
Roslyn completions, filtered by the allowlist, with doc summaries. |
POST script/hover { Code, Line, Column } |
The signature and summary of the symbol under the cursor. |
GET sql/schema |
Tables and columns (SQL Server INFORMATION_SCHEMA / PostgreSQL information_schema), cached for 10 minutes. |
POST sql/validate { Sql } |
A compile-only check that never executes the SQL. SQL Server uses sys.dm_exec_describe_first_result_set (first statement); PostgreSQL runs EXPLAIN inside a rolled-back transaction. |
POST webhook/test { PayloadXml, NodeKey, Variables, RecordId } |
Sends a CallWebhook request for real (same auth, allowlist and retries), then returns the status, headers (minus Set-Cookie), body and mapped outputs. <WriteBack> is not run. |
Expression evaluator
Section titled “Expression evaluator”WorkflowExpressionEvaluator guards transitions (and drives Decision branching). It is a small,
eval-free interpreter — not a SQL or C# engine:
- Operators:
==,!=,>,<,>=,<=,IS NULL,IS NOT NULL. - Boolean composition:
AND,OR(split outside single-quoted literals). - Literals:
true/false; single/double-quoted strings; numbers. - Operands:
@Varor bareVar(both resolved against the context, with and without the@). - Typing: comparisons parse both sides as decimals and compare numerically; equality compares string forms case-insensitively.
- Fail-safe: an empty expression is
true; an expression it can’t parse isfalse.
@FlowStatus is injected into the context for evaluation, so guards can branch on the business status.
Concurrent requests
Section titled “Concurrent requests”Changes to one instance are serialized (Genie:Workflow:Concurrency, on by default):
- Transitions, approvals and cancels take a short lock on the instance. Two people clicking at the same moment, or the same request retried, cannot both advance from one node, run its webhooks or SQL twice, or claim the same role-based approval.
- The second request waits up to
WaitSeconds(5), then runs against the updated state: usually “transition not found from node …” or “no pending approval”, because the first already moved on. It gets 409 Conflict if the first is still running. - A single-instance start locks the record too, so two simultaneous starts create one instance, and
the second reports it as skipped.
allowMultiplestarts are not locked.
The lock goes through Redis when it is configured, so it covers every API process. Turn it off, or tune
LockSeconds for slow webhooks, in
configuration.
SLA monitor
Section titled “SLA monitor”WorkflowSlaMonitorHostedService is a MonitoredBackgroundService that runs every 1 minute and
calls IWorkflowSlaMonitor.CheckAndEscalateAsync. It appears on the
hosted-services dashboard as Workflow SLA Monitor, can be paused there,
waits for model migration before its first sweep, and retries a failed sweep after a minute. It is a
worker loop: it runs only on a Worker-role process that holds the worker lease, and under explicit
AddWorkers(...) only when AddWorkflowSlaWorker() is listed. A host can
supply its own SLA policy by registering an IWorkflowSlaMonitor after AddGenieWorkflow(); the last
registration wins.
The designer API — /api/v1/genie/workflow
Section titled “The designer API — /api/v1/genie/workflow”WorkflowDesignerController ([Authorize], base route /api/v1/genie/workflow) backs the visual
designer and the runtime hooks. At a high level:
Definitions
GET definitions— list active definitions (company-scoped).GET definitions/{key}— a definition by id, name, or slug (includes its XML +VersionHash).POST definitions/PUT definitions/{id}— create / update metadata.POST definitions/{definitionId}/save— save XML (validates a Start node exists; versions the save).
Version history
GET definitions/{definitionId}/versions— every saved version (hash · time · notes ·IsCurrent).GET definitions/{definitionId}/versions/{versionHash}— a single version’s XML.
Instances
POST instances/start,instances/{id}/transition,instances/{id}/approve,instances/{id}/cancel— for programmatic and administrative callers only; the React client has no methods for these, because authored SQL drives workflows through server callbacks instead. The three that act on an existing instance require the caller to be its assignee, a member of its assigned role, a pending approver on it, or holdSystem/Admin; the acting user always comes from the session.GET instances/{id},instances/{id}/audit,instances/{id}/transitions.GET instances/{id}/preview— the graph the instance actually runs (its pinned XML), visited nodes, traversed paths, per-node context snapshots, and available transitions — for the designer’s instance overlay.POST instances/retry— re-attempt a workflow start for a wizard form response that failed to start (rehydrating context from the storedResponseData).
The React designer (components/workflow/GenieWorkflowDesigner.tsx) renders the node/transition graph,
edits action payloads structurally, and overlays live instance state via the preview endpoint.
The designer (UI)
Section titled “The designer (UI)”Property pane
Section titled “Property pane”The right-hand pane is the same property grid as the report designer’s Properties tab. A chip at
the top names the selection — WORKFLOW for the workflow settings (nothing selected), TRANSITION,
the node’s type (START, APPROVAL, …), or 3 NODES for a multi-selection — and below it one
bordered box lists every property as a two-column row under an uppercase category:
- Workflow settings — Identity (Name, Slug, Label), Classification (Entity Type, Category).
- Transition — Identity (Label, Order), Guard (Expression, Require Comment), Status (Flow Status, Entity Status), SQL.
- Node — Identity (Key, read-only; Label; Type; Description), then per type: Activity and SLA (Standard / Approval), Approval (Mode) with an Approvers list, Assignment, Bound record (Start), or Action with its payload rows, recipient lists and the raw-payload disclosure. A Decision lists its Branch conditions (the target above a full-width guard input); other nodes list their Outgoing transitions — click one to select that transition.
Flags are — (unset) / true / false selects. The gating ones — SLA Enabled and Bind Record —
reveal their dependent rows when true; unset or false removes the <Sla> / <Binding> from the XML,
exactly as before. A description footer under the pane explains the focused row (it starts on the
selection’s first property). Delete node / Delete transition / Delete N nodes sits below the
box. While viewing an old version or an instance preview every editor is disabled and the add, remove
and delete buttons are hidden. Drag the splitter to widen the pane.
Code editors, IntelliSense and sample values
Section titled “Code editors, IntelliSense and sample values”Every SQL, C#, JSON and XML field in the designers (workflow, wizard and report) is a Monaco editor, the editor VS Code is built on. It has find/replace, multi-cursor, folding, bracket matching, format document, inline error markers, hover, and the command palette (F1). Monaco loads on first use, in its own chunk, so other pages don’t pay for it.
In the workflow designer’s Action rows the editors know the node’s scope (from
/workflow-designer/scope):
- C#: Roslyn completion on the script globals (
Ctx.,Sql.,Http.…) with doc summaries, hover, and live diagnostics, allowlist violations included. InsideGet<T>("…"),Variables["…"]orOutputs["…"], completion offers the context variable names. - SQL: tables and columns from the database schema (aliases resolved from
FROM/JOIN), the parameters in scope after@, and a compile-only check shown as markers. The wizard and report designers get the schema completion too, without the server check, since their SQL holds tokens that only resolve at run time (the browser-side syntax check below still runs there). {{…}}templates (JSON bodies, auth bodies): variables,Record.*, the built-in roots and secret names.- XML (the workflow and wizard XML views): element names after
<(the open element’s children first), the attributes not yet written inside a tag, and the closing tag after</. Names come from the workflow / wizard grammar plus whatever the document already uses; nothing is offered insideCDATA, so embedded SQL is left alone. - Instant syntax check (every SQL and XML editor, in every designer, no server call): XML is
checked for well-formedness (fragments with several top-level elements are fine). SQL is checked
for unbalanced
( ), unclosed[identifiers],'strings',"identifiers",/* comments */and PostgreSQL$$bodies, with{{tokens}}skipped. Once that structure is sound, each statement is also checked against the SQL grammar: an unknown statement word (UIpdate …), a danglingWHERE,SET a =with no value, a stray word after an expression. The editor doesn’t know which database is active, so a statement is flagged only when both the T-SQL and the PostgreSQL grammar reject it. Statements using constructs the browser parser can’t read (FOR JSON/FOR XML,IF … BEGIN,MERGE,EXEC,TRY/CATCH,PIVOT,WITHIN GROUP,#temptables, DDL) are only checked for their first word. Problems show as red squiggles a moment after you stop typing. Table and column names are not checked; in the workflow designer the compile-only server check above covers that. - Handlebars (an
AppNotificationMessage and aSendEmailBody): HTML and{{…}}highlighting, HTML tag completion, the context variables after{{, and block snippets after{{#(#if,#if … else,#unless,#each,#with, each inserted with its closing tag). After{{/it offers the closing tag of the innermost open block.
Format document (Shift+Alt+F, or the dialog’s toolbar) re-indents the whole editor:
- SQL gets one clause per line with its items indented. Keyword case is kept, and
@Params,[bracketed]names and{{tokens}}are left whole. The dialect (SQL Server or PostgreSQL) is guessed from the text. - XML gets one element per line, nested. An element holding only text or
CDATAstays on one line with its content unchanged, so embedded SQL is never reformatted. - Handlebars puts HTML block elements (
<p>,<div>,<table>,<tr>,<li>…) and{{#if}}/{{else}}/{{/if}}on their own lines, nested; text, inline tags and{{Var}}stay together. A template with no HTML tags, such as a plain-text notification, is left as typed, because its line breaks are part of the message. - JSON uses Monaco’s own formatter.
If the text can’t be parsed (unbalanced tags or blocks, SQL the formatter doesn’t understand), Format leaves it unchanged.
Suggestions open as you type and on Ctrl+Space, Alt+Enter or Ctrl+I; if your OS or a keyboard tool takes Ctrl+Space, use one of the others. SQL keywords and words already in the document are offered in every editor, even one with no scope attached.
Resize and pop out. Drag the grip on an editor’s bottom edge to set its height, like a text area; once dragged, it keeps that height instead of growing with the text. Every editor also has an Open in dialog button in its top-right corner (dimmed until you hover or focus the editor). It opens the same text in a large dialog editor with the same completion. Edits in the dialog land in the field as you type; close it with the ×, Esc, or a click outside.
The dialog has a toolbar (hover a button for its shortcut):
- Undo / Redo.
- Find, Replace and Go to line.
- Toggle comment, Format document (see above), Fold all and Unfold all.
- Toggles for Word wrap, Minimap, Line numbers and Show whitespace. Word wrap starts on for XML and text, off for SQL, C# and JSON.
- Text size − / + (10–22 px).
- Copy all and the Command palette (F1), which lists every other editor command.
Undo/Redo, Replace and Toggle comment are disabled in a read-only editor.
The workflow, wizard and report designers’ XML buttons open this same dialog directly. They show it with a slim title bar and a status bar: the mode, the cursor’s line and column, a hint, and that designer’s actions (Export/Copy and Load). The Edit rbac.xml… item on the authorization board uses it too.
Sample values. A Test data section above Context holds the sample-record picker (a view and a
record id, plus Clear). It is design-time only and never saved to the workflow. It loads that
record through the object view endpoint, under your usual permissions, and its values appear next to
names in completion and hover, and in the Context list. The outputs of the last successful
Test call are added to them, so later nodes show what an earlier call returned. Clicking a Context
entry inserts a reference into the editor you used last, in the form that editor needs: @Var in SQL,
Ctx.Variables["Var"] or Ctx.Record.Get<string>("Field") in C#, {{Var}} in a template. Secrets are
listed by name and never show a value.
CallWebhook editing. The rows cover Name, Connection, Timeout, Auth Type (each type shows only its
own attributes), Method, URL, Body (Template / Sql with SQL Server and PostgreSQL variants / Form),
Retry, Success Status, Write Back and On Error (with a picker over the node’s outgoing transitions).
List sections edit Headers, Query, Form fields and Response maps. Test call → Send sends the request
for real, after a confirm. It shows the status, timing and the response as a tree, and clicking a value
adds a <Map> for its JSONPath.
XML output. Every multi-line body the designers write (SQL, C#, JSON / HTML templates) is saved as a CDATA section, never entity-escaped, so the file reads as the code itself. Short values stay attributes or plain text.
Ctrl+Z steps back through your edits; Ctrl+Shift+Z or Ctrl+Y replays them. The toolbar carries the same two buttons, and each tooltip names what it would reverse. A drag is one step, a run of typing in one property is one step, and loading XML (the XML dialog, or an assistant Apply) is one step. Ctrl+Z inside a text box or the XML editor belongs to that editor. A dot on Save draft marks unsaved changes, and leaving the page with any asks first. Opening an old version from History starts a fresh stack, so an old version can never be undone into the live workflow.
Assistant-assisted authoring
Section titled “Assistant-assisted authoring”When the AI Assistant module is on, attach the workflow from the composer’s + menu (Current workflow) and two tools scoped to this page become available:
workflow_get_definitionreads the saved workflow under your own permissions, together with a condensed reference of the<Workflow>elements and attributes, so the model writes from the contract rather than guessing from what the open workflow happens to use.workflow_apply_changeproposes a complete rewritten<Workflow>document as an Apply card in the chat.
Clicking Apply loads the proposal onto the canvas as one undoable step; Ctrl+Z reverses it. From there it is an ordinary unsaved edit — review it, Save draft, and Publish when it should go live.
Apply keeps your layout. Nodes that already exist stay exactly where they are on your canvas (position and size), and existing transitions keep their label position and connection sides. A new node is placed beside the node its incoming transition comes from, clear of every other node. The flip side: asking the assistant to move existing nodes or tidy the canvas does nothing, so arrange nodes by hand. The XML dialog’s Load is not adjusted; it loads exactly what you paste.
- The assistant never writes. Apply fills the canvas; saving stays your action. Each tool
re-checks
Updateon theWf_DefinitionsViewresource itself. - A proposal is validated before you see it. It runs the same checks Save does (it parses, has a Start node, and its binding validates) plus a few Save cannot catch: exactly one Start node, unique node keys, transitions between existing nodes,
OnApprove/OnRejectnaming a transition that leaves that node, and an unchangedName. When the model gets it wrong, the validator’s message goes back to it and it corrects itself. - There is no data preview. Workflow SQL runs with runtime values substituted in, and some of it
writes, so the model probes tables with
execute_queryinstead of executing your SQL. - Apply needs this page open, and is refused while you are viewing an old version or an instance preview.
- “Reply in chat” — or “don’t change the workflow” — answers in the conversation and withholds the apply tool for that turn.
The assistant reads the saved definition, so Save before asking if the canvas has changes you want it to see.
Copyright © 2026 Orbyn Technologies. All rights reserved.