An agent is very good at continuing and much less good at knowing when it's finished. The fix isn't a smarter agent. It's a unit of work that carries its own goal, its own check and its own reason to stop.
The last post split the system around a coding agent into four parts: context, loops, evidence and procedure. This one is about the second part, and the most practical one: how to hand an agent work it can finish.
Two ways an agent fails to finish
Watch enough agent sessions and the failures sort into two piles.
It stops too early. The agent makes the change, runs the one test it thinks is relevant, sees it pass and reports success. The other test file it didn't think of is red. The feature works on the page it looked at and not on the one next to it. From the agent's side nothing went wrong: it had no way to know there was more.
It doesn't stop. The agent fixes the bug, then tidies the function, then renames a variable in three other files, then improves an error message nobody asked about. Each step is reasonable. Together they turn a five-line fix into a forty-file diff that's hard to review and harder to ship.
Both failures have the same cause. The agent was given a task (a description of what to do) and no stop condition (a description of when it's done). So it guessed. Sometimes the guess is early, sometimes late, and it's rarely right on.
A loop has three parts
A task becomes a loop when it carries three things:
| Part | The question it answers | Weak | Strong |
|---|---|---|---|
| Goal | What should be true afterwards? | "Fix the freeze" | "The window stays responsive with 500 artifacts in the folder" |
| Check | How do you find out, without asking a person? | "Make sure it works" | "The UI test that opens the Artifacts tab finishes in under a second" |
| Stop condition | When does work end, whether or not it succeeded? | (none) | "Stop when the check passes and the suite is green, or after the third failed approach; then report" |
The goal is written as an end state, not an activity. "Refactor the loader" is an activity: you can do it forever. "The loader reads the folder once per open, not once per redraw" is an end state: it's either true or not.
The check is something the agent can run. If the only way to know whether the work is done is for me to look at it, the agent can't close the loop alone and will either stop and ask or, more often, stop and assume.
The stop condition covers both outcomes. Stopping on success is the obvious half. Stopping on failure is the half people leave out, and it's the one that saves the most time: three honest attempts and a clear report beat thirty minutes of an agent trying variations of the same wrong idea.
The done-when line
In practice all three parts collapse into a single line on the issue, which I call the done-when:
Done when: the Artifacts tab opens in under a second with 500 files present, the redraw no longer touches disk (covered by a test), and the full suite passes in the worktree.
That line is the most valuable sentence on the issue. It's short enough to write when the issue is filed, often by voice from a phone. And it's specific enough that an agent can work toward it, test it and stop.
One Roof enforces it. Its issue picker marks an issue not ready when it's missing what an agent needs to finish without asking: steps to reproduce, expected against actual, where it was seen, and a done-when. The gap is named on the issue itself, so it gets filled before the session starts rather than discovered halfway through.
From an idea to a loop an agent can finish
- Filed as it's felt Most issues start as a sentence: dictated into a session, or sent as a Slack DM from a walk. At this point it's a task, not a loop.
- Made ready Before an agent picks it up, the issue gains its done-when: the end state, the check that proves it and the point at which to stop. One Roof won't call an issue ready without it.
- The agent loops Change, run the check, read the result, change again. The check is what turns 'I think this works' into a result the agent can see.
- It stops On a pass the agent moves the issue to In Review with the evidence. On repeated failure it stops and says what it tried and what it learned. Both are finished loops.
lit = this step · tap a box · ← → or Play · Interactive version in the original post
When the check fails
A loop isn't defined by its happy path. What the agent does after a red check matters more than what it does after a green one. The rule I've settled on is simple:
- Read the failure before changing anything. The output usually says what's wrong. Agents under pressure to finish skip this and start guessing.
- Change one thing per attempt. If three things change and the check passes, nobody knows which one fixed it, and the other two are now unreviewed edits.
- Count attempts at the approach, not at the edit. Tweaking the same idea five times is one approach. After about three genuinely different approaches, stop.
- Report what you learned. "The freeze isn't in the loader; it reproduces with an empty folder; the redraw is triggered by the sidebar" is a successful outcome for a loop that didn't fix the bug. The next loop starts from there.
The last point is the one that surprises people. A loop that stops with a precise failure report hasn't wasted its time. It has narrowed the problem for whoever picks it up next, person or agent. A loop that keeps going without new information is the only real waste.
Loops nest
Once you see work as loops, they turn out to nest, and each level has its own check and its own stop.
| Level | Goal | Check | Stop |
|---|---|---|---|
| Edit | this function returns the right value | its unit test | test green |
| Issue | the done-when is true | the issue's tests, in the worktree | done-when met, or report after ~3 approaches |
| Landing | the work joins live safely | the full suite after a rebase, inside the worktree | suite green, then squash and tag |
| Ship | staging and production run this commit | health endpoints report the same commit hash | verification green in Kargo |
| Release | the new build is what's installed | the seven-check release runbook | all seven green |
| Milestone | a round of the project is complete | every issue in the round is in review or done | release cut, next round opens |
Each level is closed by the one above it. An edit loop finishes inside an issue loop. The issue loop finishes when its work lands. Landing finishes when the ship verifies. That nesting is why a busy day of releases doesn't need a round of hand-checking for each one: every level has already proved its own part.
Milestones are loops too
The One Roof v0.50 project was cut into rounds, and each round ended in a full release with its own version number (0.5.1, 0.5.2, …). Fix builds inside a round got a fourth number (0.5.1.1); One Roof now uses plain x.y.z versions (2.0.0, 2.0.1). The version says which loop you're in, and a round isn't over until its release is installed and verified.
Loops that run without me
The strongest test of a loop definition is whether it can run with nobody watching. One Roof has a Loops place for exactly that: work that runs on its own, inside fences, and reports back.
Unattended loops need two extra things that an attended session gets for free:
- A budget. A maximum number of attempts, a time limit, or both. A loop with no person watching must end by itself.
- Fences. Unattended loops can't push to protected branches and can't mark their own work Done. They land on live and move issues to In Review, and a person closes them. The loop can do the work; it can't sign it off.
Inside those fences the loops approve their own steps, because asking a question of an empty room only stalls the work. The difference isn't trust in the model. It's whether the loop's fences make a wrong step cheap to undo.
What to take from this
- Hand an agent a loop, not a task: a goal written as an end state, a check it can run, and a stop condition for both success and failure.
- Write the three as one done-when line on the issue, and don't start work until it's there.
- Treat a precise failure report as a finished loop.
- Notice how loops nest, and let each level's check close the level below.
The next post is about the check itself: what counts as evidence that work is done, and where agent work most often claims more than it did.
Links
- Series start: Agentic engineering: the work around the agent
- One Roof: project page and download · source
Originally published at https://agentair.ai/blog/loops-with-a-stop-condition.
Top comments (0)