Advanced Orchestration Engines ⏱ 10 min read 📁 examples/16_plan_and_execute.mjs

Lesson 21: Plan-and-Execute Architecture

Decouple strategy from execution: two-phase agents utilizing withPlan for strategy formulation, human approval gates, and withExecute for implementation.

1. Why This Feature Exists

Combining planning and execution in one monolithic prompt leads to hasty execution before strategic flaws are caught. Splitting the lifecycle ensures the plan is reviewed before actions are taken.

2. Key Symbols and Exports

withPlan() & withExecute()
Dedicated session modes tailored for planning vs execution.
Human Approval Checkpoint
Allows developers/users to review and modify proposed plans before destructive execution.
Session Store Handoff
Hands off structured plan state between distinct planning and execution sessions.

3. Implementation Walkthrough

import {
    ProviderType,
    resolveProvider,
    initializeClient,
    loadCredentials,
    getDefaultModel
} from 'merci-sdk';

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

// Phase 1: Planning Session
const planSession = client.session(MODEL)
    .withSystemMessage('You are a strategic software architect. Output step-by-step implementation plans.');

const planText = await planSession.run('Plan migration from monolithic database to read/write replicas.');
console.log('Proposed Plan:\n', planText);

// Phase 2: User Approval Gate (Simulation)
console.log('\n[APPROVAL GATE] Plan approved by user.');

// Phase 3: Execution Session
const execSession = client.session(MODEL)
    .withSystemMessage('You are a senior DevOps engineer. Execute the approved migration plan step by step.');

const output = await execSession.run(`Execute the following plan: ${planText}`);
console.log('Execution Result:\n', output);

4. Wire and Telemetry Mechanics

Planning session generates structured milestones. Execution session receives validated plan as immutable input context.

5. Run the Example

Execute the runnable example directly from the repository root:

node examples/16_plan_and_execute.mjs

Read the full example: examples/16_plan_and_execute.mjs →

Expected Output: Generates multi-phase migration plan, pauses at approval checkpoint, and simulates execution of verified milestones.