Advanced Orchestration Engines ⏱ 12 min read 📁 examples/17_dag_workflow.mjs

Lesson 13: Directed Acyclic Graph (DAG) Workflows

Architect resilient multi-step pipelines with WorkflowPlanner, WorkflowExecutor, and WorkflowEvaluator: plan generation, topological execution, and self-healing replanning.

1. Why This Feature Exists

Single-prompt LLM execution fails on complex, multi-stage objectives. The DAG Workflow Engine decomposes high-level goals into structured tasks with explicit dependency tracking and automated self-healing.

2. Key Symbols and Exports

WorkflowPlanner
Translates a natural language objective into a validated Directed Acyclic Graph of execution nodes.
WorkflowExecutor
Executes nodes topologically, propagating outputs from upstream dependencies to downstream prompts.
WorkflowEvaluator & Self-Healing
Evaluates node deliverables against acceptance criteria; triggers automatic replanning if a step fails.
Retry & Timeout Policies
Per-step resilience limits preventing infinite loops or hung network requests.

3. Implementation Walkthrough

import {
    ProviderType,
    resolveProvider,
    initializeClient,
    loadCredentials,
    getDefaultModel
} from 'merci-sdk';
import {
    WorkflowPlanner,
    WorkflowExecutor,
    WorkflowEvaluator
} from 'merci-sdk/planning';

const PROVIDER = resolveProvider();
const { client } = await initializeClient({ provider: PROVIDER, options: loadCredentials({ provider: PROVIDER }) });
const MODEL = getDefaultModel({ provider: PROVIDER });

// 1. Plan workflow graph
const planner = new WorkflowPlanner(client, MODEL);
const plan = await planner.plan('Audit API endpoints, perform latency benchmark, and synthesize markdown report.');

// 2. Execute with self-healing evaluator
const evaluator = new WorkflowEvaluator(client, MODEL);
const executor = new WorkflowExecutor(client, MODEL)
    .withEvaluator(evaluator)
    .withMaxReplanRounds(2);

const result = await executor.execute(plan);
console.log('Workflow Output:', result.output);

4. Wire and Telemetry Mechanics

The planner outputs a validated JSON graph of steps and dependencies. The executor resolves topological order, injects upstream step outputs, runs sessions, and evaluates outputs against node criteria.

5. Run the Example

Execute the runnable example directly from the repository root:

node examples/17_dag_workflow.mjs

Read the full example: examples/17_dag_workflow.mjs →

Expected Output: Spawns local test API server, plans 3-step DAG, executes discovery and benchmark steps, verifies outputs, and generates the final audit report.