Pattern 8 of 8

Guard

An agent's call wakes a judge, whose verdict decides it.

An agent drafts and sends emails. It doesn't ask anyone before sending: it just sends. But every send is put to a judge first, which reads what's about to go out and approves it or not.

complier machine guard · runningagentmay draft, sendjudgewaiting
startEach agent shows what the policy lets it do right now.

What complier holds

The agent never talks to the judge. Its send is taken and held, and that attempt is what gives the judge its turn; the judge is shown the call it's judging. An approval lets the send run, and anything else denies it. The agent's other calls, here drafting, aren't judged.

In a spec

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

guard.csp
Policy = Guard(agent, send, Judgement(judge, review, {approve}), \ _ @ Policy, Policy)

Guard(r, x, Judge, K, P) is r's call x put to Judge, a judgement: passed, it's allowed and its result v carries on as K(v); failed, it's denied and the policy carries on as P. It sits wherever Permit would.

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)

-- Guard: r's call x is put to Judge before it runs. If the judge passes it, it's allowed, and its
-- result v carries on as K(v); if not, it's denied, and the policy carries on as P. r never calls
-- the judge: its attempt is what wakes it.
Guard(r, x, Judge, K, P) = attempt.r.x -> Judge(allow.r.x -> result.r.x?v : Returns(x) -> K(v), deny.r.x -> P)

The guarantee

Every send that runs was approved by the judge, each by its own approval. 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.

guard.csp
-- Every send that runs was approved by the judge, each by its own approval.
assert Alternate({result.judge.review.approve}, {allow.agent.send}) [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 > guard.csp <<'EOF'
    -- Guard: the agent drafts emails freely, but every send is put to the judge before it runs. An
    -- approved send goes out; anything else is denied, and the agent carries on. The agent never calls
    -- the judge: its send is what wakes it.
    include "patterns.csp"
    datatype Role = agent | judge
    datatype Act = draft | send | review
    datatype Value = done | failed | approve | reject
    Returns(review) = {approve, reject, failed}
    Returns(_)      = {done, failed}
    
    Policy = Permit(agent, draft, \ _ @ Policy)
          [] Guard(agent, send, Judgement(judge, review, {approve}), \ _ @ Policy, Policy)
    
    -- Every send that runs was approved by the judge, each by its own approval.
    assert Alternate({result.judge.review.approve}, {allow.agent.send}) [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 guard --spec guard.csp

    To watch it, run complier machine log guard --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 guard --role judge review           # refused: the policy doesn't take review from judge now
    complier machine act guard --role agent draft            # allowed
    complier machine result guard --role agent done
    complier machine act guard --role agent send & sleep 1   # held: its verdict prints when it comes
    complier machine act guard --role judge review           # allowed
    complier machine result guard --role judge reject        # agent's send is denied
    complier machine act guard --role agent send & sleep 1   # held: its verdict prints when it comes
    complier machine act guard --role judge review           # allowed
    complier machine result guard --role judge approve       # agent's send is allowed
    complier machine result guard --role agent done
  4. Stop it

    Terminal
    complier machine down guard

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