Lesson 14: Hierarchical Workflows and Dynamic Sub-DAGs
Build nested multi-axis workflows where primary Y-axis milestones dynamically trigger specialized X-axis sub-DAGs with objective threading.
1. Why This Feature Exists
Complex real-world initiatives (like vulnerability remediation or code migration) cannot be fully planned upfront. A hierarchical planner discovers work in macro stages and dynamically spawns micro-DAGs to handle discovered issues.
2. Key Symbols and Exports
3. Implementation Walkthrough
import {
ProviderType,
resolveProvider,
initializeClient,
loadCredentials,
getDefaultModel
} from 'merci-sdk';
import { WorkflowPlanner, WorkflowExecutor } from 'merci-sdk/planning';
const PROVIDER = resolveProvider();
const { client } = await initializeClient({ provider: PROVIDER, options: loadCredentials({ provider: PROVIDER }) });
const MODEL = getDefaultModel({ provider: PROVIDER });
// Macro Y-axis planner
const yPlanner = new WorkflowPlanner({ client, model: MODEL });
const plan = await yPlanner.plan('Security scan repository and trigger remediation sub-DAG for any HIGH severity findings.');
// Y -> X handoff, first-class: the remediation step runs a nested DAG. The
// child executor shares this run's signal/AgentContext/tools/budget, caps
// nesting depth (maxDepth), and propagates a nested failure to this step.
const remediate = plan.steps.find((s) => /remediat/i.test(s.id ?? ''));
if (remediate && findings.length > 0) {
const xPlanner = new WorkflowPlanner({ client, model: MODEL });
const xPlan = await xPlanner.plan(`Remediate vulnerabilities: ${JSON.stringify(findings)}`);
remediate.engine = 'dag'; // run a child DAG
remediate.subSteps = xPlan.steps; // static child plan...
remediate.subObjective = `Remediate vulnerabilities: ${JSON.stringify(findings)}`; // ...or let the executor plan it
}
const executor = new WorkflowExecutor({ client, defaultModel: MODEL, tools, maxDepth: 1 });
executor.setSteps(plan.steps);
await executor.execute('Security audit and remediation.', yPlanner);4. Wire and Telemetry Mechanics
A nested-DAG step builds a child executor that inherits the parent runId tracing, AgentContext direction, cancellation signal and tool belt; HITL gates are forwarded with a namespaced approvalId so the parent resolves them.
5. Run the Example
Execute the runnable example directly from the repository root:
node examples/17_dag_workflow.mjsRead the full example: examples/17_dag_workflow.mjs →