Pattern 5 of 8

Generator/critic

One agent drafts, another checks, until it passes.

A writer drafts and a critic checks the draft against its criteria. A failing draft goes back to the writer with the critic's feedback; a passing one moves on, here to publishing.

complier machine generator-critic · runningwritermay draftcriticwaiting
startEach agent shows what the policy lets it do right now.

What complier holds

Publishing is refused until the critic has passed a draft, however the writer phrases it or whatever it's told. The critic's result decides, and a failed check (an error, not just a no) counts as a fail.

In a spec

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

generator-critic.csp
Policy = GeneratorCritic(Step(writer, draft), Judgement(critic, review, {pass})) ; Step(writer, publish)

GeneratorCritic(Generate, Critic) runs the generator's phase, then the critic's judgement: a pass ends it, a fail sends the work back. Judgement(r, x, Passing) is r's call x, passing when its result is in Passing. The pattern ends, so it composes: ; Step(writer, publish) comes after.

The definitions, from patterns.csp
patterns.csp
-- Role r's call x is the judgement: a result in Passing passes, and any other result (a failed call
-- too) fails.
Judgement(r, x, Passing) = \ Pass, Fail @ Permit(r, x, \ v @ if member(v, Passing) then Pass else Fail)

-- Generator/critic: the generator's phase, then the critic's judgement. A pass ends the pattern; a
-- fail sends the work back to the generator.
GeneratorCritic(Generate, Critic) = Generate ; Critic(SKIP, GeneratorCritic(Generate, Critic))

The guarantee

Nothing is published before the critic has passed a draft. 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.

generator-critic.csp
-- Nothing is published before the critic has passed a draft.
assert Before({result.critic.review.pass}, {|attempt.writer.publish|}) [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 > generator-critic.csp <<'EOF'
    -- Generator/critic: the writer drafts and the critic reviews, until the critic passes a draft; only
    -- then may the writer publish.
    include "patterns.csp"
    datatype Role = writer | critic
    datatype Act = draft | review | publish
    datatype Value = done | failed | pass | fail
    Returns(review) = {pass, fail, failed}
    Returns(_)      = {done, failed}
    
    Policy = GeneratorCritic(Step(writer, draft), Judgement(critic, review, {pass})) ; Step(writer, publish)
    
    -- Nothing is published before the critic has passed a draft.
    assert Before({result.critic.review.pass}, {|attempt.writer.publish|}) [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 generator-critic --spec generator-critic.csp

    To watch it, run complier machine log generator-critic --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 generator-critic --role writer publish   # refused: the policy doesn't take publish from writer now
    complier machine act generator-critic --role writer draft     # allowed
    complier machine result generator-critic --role writer done
    complier machine act generator-critic --role critic review    # allowed
    complier machine result generator-critic --role critic fail
    complier machine act generator-critic --role writer publish   # refused: the policy doesn't take publish from writer now
    complier machine act generator-critic --role writer draft     # allowed
    complier machine result generator-critic --role writer done
    complier machine act generator-critic --role critic review    # allowed
    complier machine result generator-critic --role critic pass
    complier machine act generator-critic --role writer publish   # allowed
    complier machine result generator-critic --role writer done
  4. Stop it

    Terminal
    complier machine down generator-critic

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