Coding agent loops
Agent loops that end on your tests, not the agent's word.
A coding agent stops when the work looks done to it. Dagu takes that decision back: it repeats Claude Code, Codex, or any of 16 built-in agent CLIs until a command you choose passes, hands each attempt the last failure, and sends passing work to a person who can push it back with feedback. Every attempt stays on record, in one self-hosted binary.
# fix-and-review.yaml
working_dir: /srv/app
steps:
- id: branch
run: git switch -c agent/task-${context.run.id} main
- id: implement
depends: [branch]
action: harness.run
with:
provider: codex
sandbox: workspace-write
prompt: |
Implement TASK.md. The work is done only when `make test` passes.
If ${context.paths.artifacts_dir}/test.log exists, it holds the last
failed test run. Read it first and fix what it reports, even if
TASK.md does not mention it.
repeat_policy:
repeat: until
condition: make test > "${context.paths.artifacts_dir}/test.log" 2>&1
limit: 5
- id: tests_pass
run: make test
depends: [implement]
- id: review
action: human.task
with:
prompt: Review the change, or request changes.
artifacts: [test.log]
push_back:
rewind_to: implement
form:
type: object
properties:
feedback: { type: string }
required: [feedback]
depends: [tests_pass]
- id: open_pr
run: |
git add -A
git diff --cached --quiet || git commit -m "Implement TASK.md"
git push -u origin HEAD
gh pr create --fill
depends: [review]
Execution order
The loop ends when your command exits 0
Each attempt gets the last failure
Push-back reruns the agent with the reviewer's notes
16 built-in agent CLIs, mixed in one run
At a glance
Dagu loops vs. a shell loop or an in-agent goal
A command you choose, run by Dagu after every attempt.
A promise string the agent prints, or a model reading the transcript.
The check's output goes to the next attempt.
Whatever the agent happens to remember.
A typed review that pushes work back with feedback, at any hour.
Someone watches the terminal, or starts over.
A waiting run holds no process and survives restarts.
Ends with the terminal, the laptop lid, or the session.
Every attempt, check output, and review in the run history.
Scrollback.
Any of 16 built-in CLIs, mixed in one run, with fallbacks.
One vendor's agent.
In depth
How Dagu does it
The agent does not decide when it is done
Models that judge their own work without outside feedback do not reliably improve it. A Dagu loop runs your check after every attempt and stops only when it passes.
- `repeat_policy` reruns the agent until a command such as `make test` exits 0.
- The check's output lands in a file the next attempt reads, so the agent fixes what failed instead of guessing.
- Each attempt starts a fresh agent process, so long sessions do not rot.
People review work that already passes
Human attention is the scarce part of the loop. Spend it on work that machines have checked, with the evidence attached.
- A `human.task` shows the diff and test output, and waits for days without holding a process.
- Request changes, and the agent reruns with your feedback in its prompt, then the checks run again.
- Approve the plan before any code is written, or take over only when the agent runs out of attempts.
Leave it running overnight
A loop nobody watches needs its limits as fields, not as hopes written into a prompt.
- Cron schedules, webhooks, and watched folders start runs; git worktrees keep each run off your checkout.
- Attempt limits, per-attempt timeouts, and run timeouts bound every loop.
- Container sandboxes give the agent explicit mounts, network rules, and only the credentials it needs.
Different models check each other
Model judges favour their own model's output. Put a different vendor on review, and make its verdict machine-readable.
- Codex implements and Claude Code reviews, or any pairing of the 16 built-in providers.
- `output_schema` validates the reviewer's verdict before the workflow acts on it.
- Provider fallback moves an attempt to another CLI when one hits a rate limit.
FAQ
Practical questions before adopting Dagu
Is this a Ralph loop?
It is the same idea with the parts a bash loop leaves out. Like Ralph, each repeat starts the agent fresh and lets tests push back. Dagu adds an exit condition the agent cannot fake, an attempt limit, the failure output handed to the next attempt, a person who can send work back, and a record of every attempt.
How is this different from Claude Code's /goal?
/goal runs inside one Claude Code session, and its evaluator judges what the agent surfaced in the conversation. A Dagu loop runs your own command after every attempt, works with any supported agent CLI, and outlives the session: it can wait days for a review or run on a schedule.
What stops a runaway loop?
Fields, not prompts: `repeat_policy.limit` caps attempts, `timeout_sec` bounds each attempt and the whole run, and provider flags such as Claude Code's `max_budget_usd` cap spend per attempt. When the attempts run out, the workflow fails or hands the work to a person.
Can the agent game the tests?
It can try, so do not rely on the prompt. The check runs as a workflow step the agent does not write, a later step can fail the run when test files changed, and a container sandbox can mount tests read-only. Giving the agent a sanctioned way to ask a person for help also reduces cheating.
Does it need a cloud service?
No. Dagu is one binary with file-backed state. Agents run on your machines or in containers you control, with your own credentials.
Next step
Start with one workflow.
Install Dagu, move one fragile script or agent task into YAML, and decide from a real run history.