Advanced โฑ 8 min read ๐Ÿ“ examples/29_workflow_contract.mjs

Lesson 39: The workflow contract: one repo-owned file, from config to prompt

a repo-owned WORKFLOW.md: strict parsing, the template subset, last-known-good reload

1. Why This Feature Exists

A temporary repository owns a single WORKFLOW.md. You follow that file through every stage a host needs, in order:

1 โ€” WRITE AND LOAD : new WorkflowPolicy({ path }) then start() resolves the YAML front matter and the prompt body into one frozen config. snapshot() is the operator view: health, watch state, prompt size, redacted provider. 2 โ€” RENDER : renderPrompt({ issue, attempt }) fills the body from variables, createPolicyRenderer({ policy }) maps a scheduler attempt onto the template (1 โ†’ first run, N โ†’ retry N-1), an unknown variable is a PolicyError, and the parser/loader/renderer work with no policy at all. 3 โ€” MAP : policyToSchedulerOptions({ policy }) turns the config into live scheduler options and preflight; policyToWorkspaceOptions({ policy }) yields the workspace strategy and hooks. 4 โ€” BREAK : an edit with invalid YAML is refused. The last known good prompt still renders, and both preflight paths name the reason. 5 โ€” RELOAD : the repaired file is applied, and a real model answers the newly rendered prompt โ€” the prompt you saw is the prompt that ran.

Public API: WorkflowPolicy, createPolicyRenderer, parseWorkflowMarkdown, loadWorkflowFile, renderPromptTemplate, policyToSchedulerOptions, policyToWorkspaceOptions, WORKFLOW_POLICY_REASONS, PolicyError, MerciEventEmitter, EVENTS.

Run: node examples/29_workflow_contract.mjs Needs: PROVIDER + provider credentials in secret.env.

@version 0.27.0

2. What the Example Demonstrates

examples/29_workflow_contract.mjs
a repo-owned WORKFLOW.md: strict parsing, the template subset, last-known-good reload
imported from 'merci-sdk'
EVENTS, MerciEventEmitter, PolicyError, initializeClient, loadCredentials, log

3. How the Core Call Works

The entry point, condensed. The full runnable file is linked below.

import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { EVENTS, MerciEventEmitter, PolicyError, initializeClient, loadCredentials, log } from 'merci-sdk';
import {
    WORKFLOW_POLICY_REASONS,
    WorkflowPolicy, createPolicyRenderer, loadWorkflowFile, parseWorkflowMarkdown,
    policyToSchedulerOptions, policyToWorkspaceOptions, renderPromptTemplate,
} from 'merci-sdk/workflow-policy';
import { reportUsage, resolveExample, runMain, trackUsage, warnEmptyAnswer, wireClient } from './_shared/example-utils.mjs';

/** Lives only in `tracker.provider`; the operator view and the event trail must never show it. */
const SECRET = 'SECRET_workflow_provider_do_not_leak';

/** The issue view the direct renderer calls receive. */
const ISSUE = {
    identifier: 'MER-29',
    labels: ['agent'],
    description: 'Add a repo-owned workflow contract.',
};

/** The scheduler work item `createPolicyRenderer` maps into that issue view. */
const ITEM = {
    id: 'wi-31',
    identifier: 'MER-29',
    title: 'Ship the workflow contract',
    state: 'In Progress',
    url: null,
    lane: 'in progress',
    dispatchable: true,
    description: 'Add a repo-owned workflow contract.',
    priority: 1,
    labels: ['agent'],
};

/** What the repaired template asks the model to answer, exactly. */
const MARKER = 'CONTRACT-MER-29';

/**
 * The file a repository owns: policy front matter, then the prompt body.
 * @param {{root: string}} arg0
 * @returns {string}

Read the full example: examples/29_workflow_contract.mjs โ†’

4. Behavior the Code Fixes

Public API: WorkflowPolicy, createPolicyRenderer, parseWorkflowMarkdown, loadWorkflowFile, renderPromptTemplate, policyToSchedulerOptions, policyToWorkspaceOptions, WORKFLOW_POLICY_REASONS, PolicyError, MerciEventEmitter, EVENTS.

5. Run the Example

Execute the runnable example from the repository root:

node examples/29_workflow_contract.mjs
What it does: a repo-owned WORKFLOW.md: strict parsing, the template subset, last-known-good reload