Flow Step Contracts

Stable procedures can opt in to declared successors and typed judgments. Existing

Typed step contracts and runtime routing

Choose a style with the prompt-led orchestration guide.

Stable procedures can opt in to declared successors and typed judgments. Existing FlowPlaybook documents and Mission plans without these fields retain their prior behavior and serialized reports. This adds no executor or result store.

Author `spec.routing: {version: 1, entry: judge, maxTransitions: 20}` in the existing `flow.aiwg.io/v1` (or `workflow.aiwg.io/v1`) playbook. Each step then declares:

contract:
  version: 1
  inputs: {type: object, additionalProperties: false}
  outputs:
    type: object
    required: [approved]
    additionalProperties: false
    properties:
      approved: {type: boolean}
allowed_successors:
  - step: publish
    when: {output: /approved, equals: true}
  - step: reject
    when: {output: /approved, equals: false}

A terminal step declares `allowed_successors: []` and `terminal_outcome: success` or `failure`. All steps default to required; `contract.required: false` marks a branch as optional for completion coverage. Inputs use the existing `inputs: [{name, value}]` or `inputs: [{name, from: step.outputName}]` bindings. Contracts validate the complete bound input object and JSON-decoded output value. JSON Schema draft-07 schemas compile strictly and resolve only local references; remote fetching is unavailable. The contract, routing and report definitions live in the registered `workflow-playbook.schema.json` under `definitions/*V1`.

The host resolves capability, fanout and gate behavior through its existing worker mechanism, then calls `flowPlaybookToMission(playbook, bindStep)` from `src/flow/playbook-contracts` and runs the returned plan with `MissionConductor`. The binder supplies runtime, prompt and existing worker options for each step; the projection retains the authored contracts, IDs and input bindings. Bind fanout as one complete step and retain human gates in the existing host mechanism. An unsupported capability or gate must fail before dispatch. This feature does not turn the MCP launch envelope into a standalone executor.

`flow-run` validates opted-in declarations and returns `ready_for_runtime_execution`, with a required runtime contract. A host must use the projection and conductor; interpreting successors inside a model prompt does not satisfy that contract. Legacy envelopes remain `ready_for_host_execution`.

The conductor snapshots the plan before dispatch. It passes validated bound inputs as `invocation.stepInputs`. A worker returns a JSON string in `output`; its model judges that step only. The conductor evaluates conditions against that validated output using JSON Pointer and JSON equality. Exactly one successor must match. An omitted condition is unconditional, not an ordered fallback. Unknown or duplicate successor IDs, ambiguous conditions and unmatched conditions fail. An optional adapter `nextStep` echo must equal the runtime selection, including its condition; it cannot override routing. A terminal allows no successor echo.

Explicit errors include `FLOW_CONTRACT_INVALID`, `FLOW_INVALID_INPUT`, `FLOW_INVALID_OUTPUT`, `FLOW_DISALLOWED_TRANSITION`, `FLOW_ROUTE_UNRESOLVED`, `FLOW_STEP_FAILED`, `FLOW_TRANSITION_LIMIT` and `FLOW_RESUME_DRIFT`. Failures remain in the existing ledger, with observed spend retained even for invalid outputs. Existing `depends_on` prerequisites must have passed before a selected step can execute. They cannot introduce a detour outside declared successors; an unmet prerequisite fails explicitly. Legacy dependency recovery is unchanged.

Loops remain available by declaring a successor back to an earlier step. Every executed visit consumes the `maxTransitions` budget, including terminal visits. Repeated visits retain separate cycle results. Resume requires the same frozen plan digest, revalidates carried outputs and recomputes successors; it does not trust previously reported completion booleans. File-effect recovery without a retained typed output fails rather than claiming step completion.

The ledger's `flowRouting.completion`, the mission summary's `flowCompletion`, and the playbook status projection `status.stepRouting.completion` distinguish:

  • `terminalOutcome`: `success`, `failure`, or `pending` (including plan-only runs).
  • `requiredStepCoverage`: distinct required steps attempted, total required steps,

and their ratio. An invalid attempt counts as visited, not passed. Planned steps do not count; no required steps gives a ratio of 1.

  • `strictCompletion`: a successful terminal plus every required step passed with

valid output and no failed visit. Reaching success while bypassing a required step gives terminal success and strict completion false. These measures describe runtime observations, not independent acceptance of worker claims.

Evidence and tradeoffs

The issue quotes REF-3290 (STAGE, arXiv v3 preprint, GRADE LOW): typed judgments with runtime-selected successors improved Pass³ in all 18 paired workflow–model rows (+7.5 to +66.0 percentage points; Telecom, Luna 32.5% to 92.5%). Agent cost was higher in 16 of 18 rows, with a median about 4.23×. The quoted model-routing ablation scored 45.0% against 92.5% on Telecom, Luna. These are not AIWG results. Caveats: about 50 proprietary cases per workflow, graph construction withheld, and different baselines between v2 and v3.

REF-1905 is design only; REF-2005 is directional, without measurements here. REF-2231 (Temporal, “fallacy of the graph”) cautions that static graphs handle dynamic control flow, compensation and state poorly. Accordingly this feature is opt in per flow and permits bounded loops. No corpus was available for this implementation; these facts and caveats are limited to the issue's quotations.

Node-local Ralph bodies

A step may declare `loop: {version: 1, criterion: "local tests pass", maxIterations: 3}`. The host binds the step normally and supplies `MissionConductor.verifyLoop`. Only a passing body's final JSON output enters contract validation and successor selection; exhaustion fails that step and prevents later routing. Local iterations consume the body's ceiling, not `spec.routing.maxTransitions`. Dependency-only flows can use the same body without typed routing. See `$AIWG_ROOT/docs/addons/aiwg-utils/mission-recovery.md` for the callback, retained verifier diagnostics, cost and dependency retry semantics.

The loops-inside-graphs evidence is weak: REF-3293 is a practitioner article (grade C, VERY LOW) with no data or measured outcomes. It motivates this opt-in composition without proving a performance advantage.