ClawMart AI
← All issuesClaw Mart Daily
Issue #344August 24, 2026

Our coding agent optimized the same function for 6 hours because it couldn't tell done from perfect

Our coding agent was stuck in a 6-hour loop yesterday. Not an infinite loop — those are easy to catch. A productive loop. It kept finding "issues" to fix, writing code, running tests, finding new "issues," and starting over.

The tests were passing. The feature worked. But the agent couldn't tell the difference between "done" and "could be better."

This is the hidden cost of persistent agents. They don't just complete tasks — they optimize tasks. Forever. Until you stop them or your API budget dies.

Here's the completion gate that fixed it:

COMPLETION_CRITERIA = {
    "required": [
        "All tests pass",
        "Feature requirements met", 
        "No syntax errors"
    ],
    "stop_conditions": [
        "3 consecutive test passes",
        "No code changes in last 2 iterations",
        "Manual approval received"
    ],
    "time_limits": {
        "max_session": "2 hours",
        "max_iteration": "15 minutes"
    }
}

The key insight: agents need explicit stopping conditions, not just success conditions. Success keeps them running. Completion stops them.

We added a simple gate that checks three things:

  • Convergence: Has the code stopped changing?
  • Stability: Have tests passed multiple times in a row?
  • Time bounds: Are we approaching the session limit?

If any completion condition triggers, the agent writes a handoff document and exits cleanly. No more 6-hour "optimization" sessions that produce worse code than iteration 3.

Pro tip: Set your iteration time limit to 15 minutes. If an agent can't make meaningful progress in 15 minutes, it's stuck or overthinking. Force a checkpoint.

The handoff document captures what the agent accomplished, what it was trying to do next, and why it stopped. When you (or another agent) pick up the work, you're not starting from scratch.

We've been running this for two weeks. Our average coding session dropped from 4.2 hours to 47 minutes. Success rate went from 31% to 78%. The agent ships working code instead of "perfect" code.

Turns out the enemy of done isn't perfect. It's "could be slightly better."

Paste into your agent's workspace

Claw Mart Daily

Get tips like this every morning

One actionable AI agent tip, delivered free to your inbox every day.