Private preview. Daggry is not publicly released.Roadmap
Skip to content

Add a loop ​

An agent that repeats work can run all night. A loop repeats steps once for each plan item, checks a gate after every iteration and stops at a limit you set. Read this page when the same steps repeat for each item of an approved plan. It builds on Write a workflow.

The example is a software one: write a failing test, make it pass, validate. A loop for other work has the same keys and other steps. The file is illustrative.

agentPlana list of plan items
youYou approvethe exact plan
repeat for each plan item · up to the loop limit
agentWrite the proofa test for the next plan item
codeProof shown to failbefore the work
agentDo the workmake the proof pass
codeRun again on a clean copyevery validator
↺ proof still fails? another iteration · at the loop limit: the run fails or waits for you
codeDeliver the resultan effect, for your review
A workflow that delivers a change: a plan, your approval, a loop with a proof for each plan item that fails first, then passes, and an effect that delivers the result. When the loop reaches its limit, the run fails or waits for you. In software, the proof is a test and the effect opens a pull request.

The diagram shows the workflow this page builds: a plan, your approval, a loop of proof, work and validation for each plan item, and an effect, the step that delivers the result (here, it opens a pull request).

Add the loop ​

Use a loop when the same steps repeat for each plan item. A plan item is one entry of the approved plan. The files call it a criterion.

  1. Add a loop entry after the approval. A loop entry has an id and a loop key, and no kind.

    yaml
    format: daggry/workflow@1
    steps:
      - id: plan
        kind: agent
        engine: <engine>
        prompt: Write a plan with checkable criteria.
        outputs: [plan]
        scope: nothing
      - id: approve-plan
        kind: human
        waiting_for: approval
      - id: red-green
        loop:
          over: plan
          as: criterion
          until: verdict.all_covered
          max: 10
          on_exhausted: ask
          steps:
            - id: proof
              kind: agent
              engine: <engine>
              prompt: Write a failing test for the criterion.
              outputs: [change]
              freeze: ["tests/**"]
              scope: ["tests/**"]
            - id: work
              kind: agent
              engine: <engine>
              prompt: Make the test pass.
              outputs: [change]
              scope: ["src/**", "tests/**"]
            - id: check
              kind: verify
              validators: [project, daggry/criterion-coverage]
      - id: ask
        kind: human
        waiting_for: input-needed
      - id: pr
        kind: effect
        do: open-pr
  2. Set the loop keys. Every key is required.

    KeyMeaning
    overThe list to loop over. Use plan, the items of the approved plan.
    asThe name of the current item.
    untilThe gate: the rule that decides whether the loop can stop. Daggry reads it at the end of each iteration.
    maxThe loop limit: the most iterations the loop can run. It counts the whole loop, not each item.
    on_exhaustedWhat happens at the limit: fail, or the id of a top-level human step.
    stepsThe steps of one iteration.
  3. Set max greater than the number of plan items you expect, or the loop cannot finish. The max of 10 here assumes a plan of fewer than 10 items.

The gate verdict.all_covered holds when every plan item has passed. The built-in validator daggry/criterion-coverage works that out, so the verify step names it. A loop cannot contain another loop.

How an iteration ends ​

You need this to read a run's status. An iteration either moves on or repeats, and the verify step decides which.

One iteration for each plan item, up to the loop limit

  1. agentWrite the proofa test for the next plan item
  2. codeShown to failbefore the work
  3. agentDo the workchange the paths in scope
  4. codeRun again on a clean copyevery validator

Plan items left, or this one failed: another iteration

Gatethe rule that lets the loop stop
  • every plan item coveredNext step
  • loop limit reachedFails, or a person decides
Each iteration takes one plan item through four steps, two by the agent and two by code: the agent writes a proof, code shows that the proof fails before the work, the agent does the work, and code runs every validator on a clean copy. The gate lets the run go on only when every plan item is covered. At the loop limit, the run fails, or it waits for you at the human step the workflow names. In software, the proof is a test.

The diagram shows one iteration, the gate after it and the exit at the loop limit. When the limit is reached, the run fails or a person decides.

  • The verify step passes. The loop moves to the next plan item.
  • The verify step fails. The same plan item repeats. The proof step does not run again.
  • Any other step fails. The run fails.

Each repeat spends one iteration of max.

Prove each item first ​

A test that already passes says nothing about the work. So the first step of the iteration writes a proof: a test that fails first, then passes. Software teams call this red, then green.

The freeze key marks the proof step. Each iteration then runs in this order.

  1. The proof step writes a test for the next plan item.
  2. Daggry runs the project's main validator on the version of the workspace the run started from, with the new test added. The proof counts only if the test fails there.
  3. Daggry seals the change and freezes the paths the proof step wrote, matched by the freeze globs. No later step can change a frozen path, whatever its scope.
  4. The agent does the work that makes the test pass.
  5. The validators check the result on a clean copy of the workspace.

A loop with no proof step, such as a document review, starts at step 4. For frozen paths, see What an agent cannot change.

When the limit is reached ​

Decide this before the run, because it is what stops an agent that is stuck. on_exhausted says what happens after max iterations.

  • With fail, the run fails.
  • With the id of a human step, the run stops there for your approval. Daggry skips that step unless the loop reaches its limit.

At that approval you have two choices.

  1. To give up the unfinished plan item and continue, run daggry approve <run-id>.
  2. To retry it with one frozen path open to change, run daggry approve <run-id> --unfreeze <path>.

Related:

Sponsored by