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.
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:
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
-- 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.
-- 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= PolicyTry it
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.Save the spec
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|}>) EOFStart the machine
FDR proves the policy and the pattern's guarantee before anything runs:
complier machine up dispatcher --spec dispatcher.cspTo watch it, run
complier machine log dispatcher --followin a second terminal.Play the flow
Each line is one step of the animation above.
actattempts a step as the agent in that role;resultgives its call's result.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 doneStop it
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.