Pattern 2 of 8

Coordinator/dispatcher

One agent decides who handles each request.

A coordinator reads each request and routes it to the specialist that handles that kind: billing for refunds, support for faults. The specialist does the work, and the coordinator moves on to the next request.

complier machine dispatcher · runningcoordinatormay close, routebillingwaitingsupportwaiting
startEach agent shows what the policy lets it do right now.

What complier holds

complier can't tell whether a route was the right one; that's the coordinator's judgement. What it holds is the routing: a specialist acts only on a request routed to it, the other specialists wait, and the coordinator routes the next request only once the current one is handled. The routing call's result names the specialist.

In a spec

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

dispatcher.csp
Policy = Dispatcher(Route, Specialist)

Route is the coordinator's turn: Route(Handle) either hands the request on, by carrying on as Handle(d), or ends. Specialist(d) is the phase of the specialist d names. Once it ends, the coordinator routes again.

The definitions, from patterns.csp
patterns.csp
-- Coordinator/dispatcher: Route is the coordinator's turn. Route(Handle) either hands the request to
-- a specialist, by carrying on as Handle(d), or ends. Specialist(d) is that specialist's phase; once
-- it ends, the coordinator routes the next request.
Dispatcher(Route, Specialist) = Route(\ d @ Specialist(d) ; Dispatcher(Route, Specialist))

The guarantee

Billing acts only after a request was routed to billing, and support only after one was routed to support. 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.

dispatcher.csp
-- A specialist acts only on a request routed to it.
assert Before({result.coordinator.route.to_billing}, {|attempt.billing|}) [T= Policy
assert Before({result.coordinator.route.to_support}, {|attempt.support|}) [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 > dispatcher.csp <<'EOF'
    -- Coordinator/dispatcher: a coordinator routes each request to billing or support, whose specialist
    -- handles it; then the coordinator routes the next one, or closes the desk. The routing call's
    -- result names the specialist.
    include "patterns.csp"
    datatype Role = coordinator | billing | support
    datatype Act = route | refund | troubleshoot | close
    datatype Value = done | failed | to_billing | to_support
    Returns(route) = {to_billing, to_support, failed}
    Returns(_)     = {done, failed}
    
    Route(Handle) = Permit(coordinator, route, \ v @ if v == failed then Route(Handle) else Handle(v))
                 [] Step(coordinator, close)
    
    Specialist(to_billing) = Step(billing, refund)
    Specialist(to_support) = Step(support, troubleshoot)
    
    Policy = Dispatcher(Route, Specialist)
    
    -- A specialist acts only on a request routed to it.
    assert Before({result.coordinator.route.to_billing}, {|attempt.billing|}) [T= Policy
    assert Before({result.coordinator.route.to_support}, {|attempt.support|}) [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 dispatcher --spec dispatcher.csp

    To watch it, run complier machine log dispatcher --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 dispatcher --role billing refund              # refused: the policy doesn't take refund from billing now
    complier machine act dispatcher --role coordinator route           # allowed
    complier machine result dispatcher --role coordinator to_billing
    complier machine act dispatcher --role support troubleshoot        # refused: the policy doesn't take troubleshoot from support now
    complier machine act dispatcher --role billing refund              # allowed
    complier machine result dispatcher --role billing done
    complier machine act dispatcher --role coordinator route           # allowed
    complier machine result dispatcher --role coordinator to_support
    complier machine act dispatcher --role support troubleshoot        # allowed
    complier machine result dispatcher --role support done
    complier machine act dispatcher --role coordinator close           # allowed
    complier machine result dispatcher --role coordinator done
  4. Stop it

    Terminal
    complier machine down dispatcher

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