Advanced
⏱ 8 min read
📁 examples/31_orchestration.mjs
Lesson 41: One call: policy + scheduler + workspace + operator
one createOrchestration call binding policy, scheduler, workspace and operator; the spend ledger
1. Why This Feature Exists
The primitives have their own examples; this one is the composition root. A repository-owned WORKFLOW.md says how work runs, a three-item board says what work exists, and one createOrchestration call binds the two to a policy, a scheduler, a workspace manager and an operator surface:
2. What the Example Demonstrates
A — one call
policy.snapshot() is started and healthy, the scheduler took its interval, active/terminal states and concurrency from the file, and the workspace manager, ring and HTTP handler exist.B — two items
start() dispatches both Todo items under the file's cap of 2, each in its own workspace directory, and the third item stays in Backlog because the board says it is not active work.C — the operator
GET /api/v1/state is the live counts and totals, /api/v1/items/<id> is one item (status parked — the documented end of a completed attempt when continuations are off), / is the formatStatus page, and POST /api/v1/refresh moves the tick count.D — the ring
createEventRing keeps the projected history. The default DEFAULT_PREFIXES omit session records; passing prefixes: [...DEFAULT_PREFIXES, 'session:'] opts them in. A host mount of createOperatorHttpHandler reads the same system, and its item feed stays item-scoped: a record without an identifier cannot belong to it.E — the spend
one DAG attempt hands
planFor a cap-less ledger. The hook's own session, the engine's objective restatement and the step all fold into totals; a call through the client the host kept stays out. What this shows: - the file owns interval, states, concurrency and the workspace root - the model is passed by the host and never read from the contract - one attempt's spend is a ledger, not an estimate; budget bounds a unit - the operator reports the same snapshot the host can read Run: node examples/31_orchestration.mjs Needs: provider credentials in secret.env (PROVIDER=google_genai). @version 0.27.0imported from 'merci-sdk'
EVENTS, MerciEventEmitter, initializeClient, loadCredentials, log
3. How the Core Call Works
The entry point, condensed. The full runnable file is linked below.
import { createServer } from 'node:http';
import { existsSync, realpathSync } from 'node:fs';
import { mkdir, rm, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { EVENTS, MerciEventEmitter, initializeClient, loadCredentials, log } from 'merci-sdk';
import { LocalFileSessionStore } from 'merci-sdk/sessions';
import {
DEFAULT_PREFIXES, OPERATOR_API_VERSION, OPERATOR_DEFAULTS, createEventRing, createOperatorHttpHandler, formatStatus,
} from 'merci-sdk/operator';
import { createOrchestration } from 'merci-sdk/orchestration';
import { createMemoryWorkSource } from 'merci-sdk/scheduler';
import { SCRATCH_DIR } from './_shared/real-tools.mjs';
import { reportUsage, resolveExample, runMain, trackUsage, wireClient } from './_shared/example-utils.mjs';
const STATE_DIR = join(SCRATCH_DIR, '31-orchestration');
const WORKSPACE_ROOT = join(STATE_DIR, 'workspaces');
const WORKFLOW_FILE = join(STATE_DIR, 'WORKFLOW.md');
const ITEMS = [
{ id: 'i-31a', identifier: 'MER-31A', title: 'Orchestration item A', state: 'Todo', dispatchable: true, priority: 1 },
{ id: 'i-31b', identifier: 'MER-31B', title: 'Orchestration item B', state: 'Todo', dispatchable: true, priority: 2 },
{ id: 'i-31c', identifier: 'MER-31C', title: 'Waiting in the tracker', state: 'Backlog', dispatchable: true, priority: 3 },
];
const ACTIVE = ['MER-31A', 'MER-31B'];
const WAITING = 'MER-31C';
/**
* The repo-owned contract: two active items at a time, no continuations, and
* the workspace root under this run's scratch directory.
* @returns {string}
*/
const contract = () => [
'---',
'tracker:',
' kind: memory',
' active_states: [Todo]',
' terminal_states: [Done]',
'polling:',
' interval_ms: 3600000',
'workspace:',
` root: ${JSON.stringify(WORKSPACE_ROOT)}`,
'agent:',
' max_concurrent_agents: 2',
'merci:',4. Behavior the Code Fixes
one createOrchestration call binding policy, scheduler, workspace and operator; the spend ledger
5. Run the Example
Execute the runnable example from the repository root:
node examples/31_orchestration.mjs
What it does: one
createOrchestration call binding policy, scheduler, workspace and operator; the spend ledger