Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Workflows API

Canonical reference: docs/api-reference.md (generated endpoint directory) — this page is a task-oriented summary.

Sol’s workflow engine orchestrates multi-step agent tasks using gen_statem instances with compensation, branching, and tree operations. This API covers the full workflow lifecycle.

Create Workflow

POST /api/v1/workflows/start

Start a new workflow from a named definition and payload.

curl -X POST https://sol.example.com/api/v1/workflows/start \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "code-review",
    "payload": {},
    "objective": "Review the diff for bugs",
    "token_budget": 50000,
    "time_budget_ms": 600000
  }'

Response:

{
  "workflow_id": "wf-1744730000000",
  "status": "started"
}

400 if name is missing; 500 on internal failure.

List Workflows

GET /api/v1/workflows

List all workflows with their current status.

curl -H "Authorization: Bearer $TOKEN" \
  https://sol.example.com/api/v1/workflows

Get Workflow

GET /api/v1/workflows/:id

Get workflow details including step statuses and results.

curl -H "Authorization: Bearer $TOKEN" \
  https://sol.example.com/api/v1/workflows/wf-1744730000000

Response:

{
  "id": "wf-1744730000000",
  "name": "code-review",
  "status": "running",
  "steps": [
    {"name": "analyze", "status": "completed", "result": "..."},
    {"name": "fix", "status": "running"},
    {"name": "verify", "status": "pending"}
  ]
}

Cancel Workflow

POST /api/v1/workflows/:id/cancel

Cancel a running workflow. Triggers compensation for completed steps. 409 if already completed; 403 if not the owner.

Branch Workflow

POST /api/v1/workflows/:id/branch

Create a child workflow branching from an existing one. Body fields: branch_point_idx, objective. Returns {ok: true, child_workflow_id, parent_workflow_id}.

Workflow Tree

GET /api/v1/workflows/:id/tree

Returns the full tree of the workflow and its descendants.

Workflow Lineage

GET /api/v1/workflows/:id/lineage

Returns the root-to-node lineage path. Response: {ok: true, lineage: [...]}.

Pause Workflow

POST /api/v1/workflows/:id/pause

Pause a running workflow. {status: "paused"}. 409 if already completed.

Resume Workflow

POST /api/v1/workflows/:id/resume

Resume a paused workflow (or a budget-parked one with token_budget / time_budget_ms raised). {status: "resumed"}. 409 if already completed or the budget is still exhausted.

Retry Pending Compensation

POST /api/v1/workflows/:id/compensation/retry

Re-runs the LIFO pending-unwind driver for a failed workflow whose last unwind did not complete. Confirmed handlers clear from the durable pending set, after which /resume is allowed. 202 with the pending set; 409 when nothing is pending or the workflow is still running.

Stream Workflow Events

GET /api/v1/workflows/:id/stream

Subscribe to Server-Sent Events for this workflow’s state changes (owner-checked).

Workflow Lifecycle

Workflows use gen_statem for state management:

pending -> running -> completed
                 \-> failed
                 \-> cancelled
StateDescription
pendingCreated, not yet started
runningExecuting steps
completedAll steps finished successfully
failedA step failed (compensation triggered)
cancelledUser cancelled (compensation triggered)

Step Lifecycle

Each step within a workflow follows its own state machine:

pending -> running -> completed
                 \-> failed
                 \-> skipped

Multi-Step Execution

Workflows execute steps sequentially by default. Each step receives:

The original prompt Results from previous steps Step-specific configuration (mode, tools, timeout)

The user_uuid is propagated from the HTTP request through each step dispatch, ensuring multi-tenant isolation.

Step Configuration

FieldTypeDescription
namestringStep identifier
promptstringTask prompt for the step
modestringAgent mode (build/plan/explore/review/general/coordinate)
toolsarrayRestricted tool set (optional)
timeoutnumberStep timeout in seconds (optional)
conditionstringExecution condition (optional)

Compensation Events

When a step fails or the workflow is cancelled, the workflow engine triggers compensation events:

  1. Completed steps receive a compensate event in reverse order
  2. Each step can define a compensation action (rollback, cleanup, notification)
  3. Compensation failures are logged but do not block other compensations
  4. The workflow transitions to failed or cancelled after all compensations complete

This ensures resources are cleaned up even on partial failures.

Branching

Workflows support branching for conditional execution paths:

{
  "name": "conditional-review",
  "steps": [
    {"name": "check", "prompt": "Check if tests pass", "mode": "build"},
    {
      "name": "branch",
      "type": "branch",
      "conditions": {
        "pass": {"match": "{{check.result}}", "pattern": ".*passed.*", "then": "deploy"},
        "fail": {"then": "fix"}
      }
    },
    {"name": "deploy", "prompt": "Deploy to production", "mode": "build"},
    {"name": "fix", "prompt": "Fix the failing tests", "mode": "build"}
  ]
}

Tree Operations

Workflows can be organized as trees for complex orchestration:

Parent-child relationships between workflow instances (see Branch Workflow and Workflow Tree) Spawn child workflows from a parent step Aggregate results from child workflows Tree-level cancellation propagates to all children

Streaming Workflow Events

Workflow state changes for a specific workflow are streamed via SSE:

curl -N https://sol.example.com/api/v1/workflows/wf-1744730000000/stream \
  -H "Authorization: Bearer $TOKEN"

Event types:

EventDescription
workflow:startedWorkflow execution began
workflow:step:startedA step began executing
workflow:step:completedA step completed
workflow:step:failedA step failed
workflow:completedWorkflow finished
workflow:failedWorkflow failed
workflow:cancelledWorkflow was cancelled
workflow:compensatingCompensation in progress

Event Store Integration

Workflow events are stored in the Mnesia-backed event store (102 event types). Pre- and post-hooks enable:

Audit logging of all workflow mutations Real-time notifications via the PUB/SUB event bridge Plugin reactions to workflow state changes Historical replay for debugging

The workflow bridge subscribes to the event store and dispatches step tasks to workers via ZMQ, propagating user_uuid for multi-tenant isolation.