Pattern 4 of 8

Hierarchical decomposition

An agent hands part of its task down, and waits for it.

A writer needs research, so it hands that to an assistant, which hands the searching to a searcher in turn and summarises what comes back. Each level waits for the level below it, and carries on with the result.

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

What complier holds

The writer's research call doesn't return while the work below it happens: complier holds it. The call is taken, and its answer comes only once the assistant has finished, so the writer's tool call returns with the work done. The assistant delegates the same way, and the searcher acts only inside that delegation.

In a spec

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

hierarchy.csp
Policy = Delegate(writer, research, Assistant, \ _ @ Step(writer, write))

Delegate(r, x, Sub, K) holds r's call x while Sub, the agents below it, works; when Sub ends, the call runs and its result v carries on as K(v). Sub may delegate in turn: here Assistant is Delegate(assistant, search, Step(searcher, browse), ...).

The definitions, from patterns.csp
patterns.csp
-- A held call: role r's call x is taken, and answered only once H has ended. Until then r waits (its
-- tool call doesn't return) while H's agents act. Then the call is allowed, and its result v carries
-- on as K(v).
Hold(r, x, H, K) = attempt.r.x -> (H ; allow.r.x -> result.r.x?v : Returns(x) -> K(v))

-- Hierarchical decomposition: r's call x hands a task to Sub, the agents below it, and is held until
-- Sub ends. Sub may delegate in turn.
Delegate(r, x, Sub, K) = Hold(r, x, Sub, K)

The guarantee

The writer's research call runs only once the assistant has summarised, and the assistant's search only once the searcher has browsed. 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.

hierarchy.csp
-- A delegated call runs only once the work below it is done.
assert Before({|result.assistant.summarise|}, {|allow.writer.research|}) [T= Policy
assert Before({|result.searcher.browse|}, {|allow.assistant.search|}) [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 > hierarchy.csp <<'EOF'
    -- Hierarchical decomposition: the writer's research call hands the research to an assistant, and
    -- waits. The assistant hands the search to a searcher in turn, then summarises what it found. Once
    -- the assistant is done, the writer's call returns and the writer writes the report.
    include "patterns.csp"
    datatype Role = writer | assistant | searcher
    datatype Act = research | search | browse | summarise | write
    datatype Value = done | failed
    Returns(_) = {done, failed}
    
    Assistant = Delegate(assistant, search, Step(searcher, browse), \ _ @ Step(assistant, summarise))
    
    Policy = Delegate(writer, research, Assistant, \ _ @ Step(writer, write))
    
    -- A delegated call runs only once the work below it is done.
    assert Before({|result.assistant.summarise|}, {|allow.writer.research|}) [T= Policy
    assert Before({|result.searcher.browse|}, {|allow.assistant.search|}) [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 hierarchy --spec hierarchy.csp

    To watch it, run complier machine log hierarchy --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. A held call waits for its verdict, so it runs in the background; the step that answers it says so, and the held call prints its verdict when it comes.

    Terminal
    complier machine act hierarchy --role writer research & sleep 1    # held: its verdict prints when it comes
    complier machine act hierarchy --role searcher browse              # refused: the policy doesn't take browse from searcher now
    complier machine act hierarchy --role assistant search & sleep 1   # held: its verdict prints when it comes
    complier machine act hierarchy --role searcher browse              # allowed
    complier machine result hierarchy --role searcher done             # assistant's search is allowed
    complier machine result hierarchy --role assistant done
    complier machine act hierarchy --role assistant summarise          # allowed
    complier machine result hierarchy --role assistant done            # writer's research is allowed
    complier machine result hierarchy --role writer done
    complier machine act hierarchy --role writer write                 # allowed
    complier machine result hierarchy --role writer done
  4. Stop it

    Terminal
    complier machine down hierarchy

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