Pattern 6 of 8

Iterative refinement

Critique and revise, a bounded number of times.

A writer drafts, then a critic and an editor take turns improving it: the critic says what to change, the editor revises. It stops when the critic is satisfied, or after a set number of rounds.

complier machine refinement · runningwritermay draftcriticwaitingeditorwaiting
startEach agent shows what the policy lets it do right now.

What complier holds

The bound is part of the policy, not a counter an agent keeps: after two revisions the work is finished whatever the critic thinks, and a third critique is refused. A good critique ends it early.

In a spec

The example's policy is one line from the library:

refinement.csp
Policy = Refinement(2, Step(writer, draft), Judgement(critic, critique, {good}), Step(editor, revise))

Refinement(n, Generate, Critic, Refine) runs the generator's phase, then at most n rounds: the critic's judgement, and on a fail the refiner's phase. A pass ends it early.

The definitions, from patterns.csp
patterns.csp
-- Iterative refinement: the generator's phase, then at most n rounds of critique. A pass ends it
-- early; otherwise the refiner's phase runs and the next round starts. After n refinements it ends.
Refinement(n, Generate, Critic, Refine) = Generate ; Rounds(n, Critic, Refine)
Rounds(0, _, _) = SKIP
Rounds(n, Critic, Refine) = Critic(SKIP, Refine ; Rounds(n - 1, Critic, Refine))

The guarantee

The editor revises at most twice. The spec states it as its own assertion, and FDR proves it against the most general agents (one ControllableCHAOS per role) before the machine starts. A spec that breaks the pattern isn't admitted.

refinement.csp
-- The editor revises at most twice.
assert AtMost(2, {|attempt.editor.revise|}) [T= Policy

Try it

You need complier with FDR, with complier doctor saying ready (see Install). The pattern library, held calls and agents acting at the same time are new: until a release has them, build complier from a checkout of main. No agents needed: each command acts as an agent would.
  1. Save the spec

    Terminal
    cat > refinement.csp <<'EOF'
    -- Iterative refinement: the writer drafts; then, at most twice, the critic critiques and the editor
    -- revises. A good critique ends it early; after two revisions it ends anyway.
    include "patterns.csp"
    datatype Role = writer | critic | editor
    datatype Act = draft | critique | revise
    datatype Value = done | failed | good | needs_work
    Returns(critique) = {good, needs_work, failed}
    Returns(_)        = {done, failed}
    
    Policy = Refinement(2, Step(writer, draft), Judgement(critic, critique, {good}), Step(editor, revise))
    
    -- The editor revises at most twice.
    assert AtMost(2, {|attempt.editor.revise|}) [T= Policy
    
    Agent  = ||| r : Role @ ControllableCHAOS(r)
    System = prioritise(Agent [| {|attempt, allow, deny, result, refuse|} |] ComplierGuard(Policy),
                        <{|attempt|}, {|refuse|}>)
    EOF
  2. Start the machine

    FDR proves the policy and the pattern's guarantee before anything runs:

    Terminal
    complier machine up refinement --spec refinement.csp

    To watch it, run complier machine log refinement --follow in a second terminal.

  3. Play the flow

    Each line is one step of the animation above. act attempts a step as the agent in that role; result gives its call's result.

    Terminal
    complier machine act refinement --role writer draft           # allowed
    complier machine result refinement --role writer done
    complier machine act refinement --role critic critique        # allowed
    complier machine result refinement --role critic needs_work
    complier machine act refinement --role editor revise          # allowed
    complier machine result refinement --role editor done
    complier machine act refinement --role critic critique        # allowed
    complier machine result refinement --role critic needs_work
    complier machine act refinement --role editor revise          # allowed
    complier machine result refinement --role editor done
    complier machine act refinement --role critic critique        # refused: the machine is finished (the policy terminated)
  4. Stop it

    Terminal
    complier machine down refinement

With real agents, a translation unit names which tool calls are which steps, and each agent starts in its role: COMPLIER_MACHINE=refinement COMPLIER_ROLE=writer claude --plugin-dir plugin. The two-agent guide walks through one.