agent-zero icon indicating copy to clipboard operation
agent-zero copied to clipboard

Agent Zero needs a STOP button

Open rcimedia opened this issue 9 months ago • 8 comments

agent zero desperately needs a "STOP WHAT YOU ARE DOING RIGHT NOW" button .. my AI (frontier models ie Gemini) gets into a loops constantly and wont listen to stop commands before it redoes the similar operations many times .. sometimes causing damage that needs to be redone.

rcimedia avatar Feb 21 '26 22:02 rcimedia

1000% !!!!!

nigelp avatar Feb 22 '26 20:02 nigelp

I've found that "Pause Agent" -> Send Message -> Nudge -> Send Now works about 50% of the time. Usually I have to do it twice to get the agent to respond. VERY frustrating.

If I could have a Stop Button, I would be SO happy!

jayma777 avatar Feb 24 '26 20:02 jayma777

yes please

programmer-no19 avatar Feb 25 '26 11:02 programmer-no19

I hit the same problem — the agent gets stuck in a reasoning loop where it keeps saying "I'll check the memory tools" or "I need to verify this" without ever actually calling a tool. It runs forever because the framework has no iteration limit and the only existing guard is an exact string match check (agent.py line 474) which misses near-duplicate output.

I wrote a circuit breaker extension that catches three failure modes:

  1. Hard iteration limit (25 iterations, configurable)
  2. No-tool-call streak — if the agent goes 5 consecutive iterations without calling any tool, it's stuck in a planning loop
  3. Near-duplicate detection — uses word-level Jaccard similarity to catch responses that are ~85%+ identical but not byte-for-byte equal (which is why the built-in repeat check doesn't fire)

When it trips, it either injects a forceful "call the response tool NOW" warning (soft trip) or kills the loop entirely (hard trip at max iterations).

Install location:

Drop this file at usr/extensions/message_loop_end/_80_circuit_breaker.py — it uses the existing extension system so no changes to agent.py are needed, and it survives in the user data volume across updates.

Note: if your usr/ directory has a gitignore rule blocking it (usr/**), you may need to create the extensions/message_loop_end/ directory manually.

The code:

from python.helpers.extension import Extension
from agent import Agent, LoopData, HandledException
from python.helpers.print_style import PrintStyle
import python.helpers.log as Log

# --- Configuration ---
MAX_ITERATIONS = 25
MAX_NO_TOOL_STREAK = 5
SIMILARITY_THRESHOLD = 0.85
MAX_SIMILAR_STREAK = 3

DATA_NO_TOOL_STREAK = "_cb_no_tool_streak"
DATA_SIMILAR_STREAK = "_cb_similar_streak"
DATA_LAST_RESPONSES = "_cb_last_responses"


class CircuitBreaker(Extension):
    async def execute(self, loop_data: LoopData = LoopData(), **kwargs):
        iteration = loop_data.iteration

        if iteration >= MAX_ITERATIONS:
            await self._trip(
                f"Circuit breaker: hit {MAX_ITERATIONS} iterations without completing. "
                f"Terminating to prevent infinite loop.",
                force_kill=True,
            )

        if loop_data.current_tool is None:
            streak = (self.agent.get_data(DATA_NO_TOOL_STREAK) or 0) + 1
            self.agent.set_data(DATA_NO_TOOL_STREAK, streak)
            if streak >= MAX_NO_TOOL_STREAK:
                await self._trip(
                    f"Circuit breaker: {MAX_NO_TOOL_STREAK} consecutive iterations "
                    f"without a tool call. Agent appears stuck in a reasoning loop."
                )
        else:
            self.agent.set_data(DATA_NO_TOOL_STREAK, 0)

        current = loop_data.last_response
        if current:
            last_responses: list[str] = self.agent.get_data(DATA_LAST_RESPONSES) or []
            similar_streak = self.agent.get_data(DATA_SIMILAR_STREAK) or 0

            if last_responses and self._is_similar(current, last_responses[-1]):
                similar_streak += 1
            else:
                similar_streak = 0

            self.agent.set_data(DATA_SIMILAR_STREAK, similar_streak)
            last_responses.append(current)
            if len(last_responses) > 5:
                last_responses.pop(0)
            self.agent.set_data(DATA_LAST_RESPONSES, last_responses)

            if similar_streak >= MAX_SIMILAR_STREAK:
                await self._trip(
                    f"Circuit breaker: {MAX_SIMILAR_STREAK} consecutive near-duplicate "
                    f"responses detected. Agent is generating repetitive content."
                )

    def _is_similar(self, a: str, b: str) -> bool:
        if not a or not b:
            return False
        a_norm = " ".join(a.lower().split())
        b_norm = " ".join(b.lower().split())
        if len(a_norm) > 2 * len(b_norm) or len(b_norm) > 2 * len(a_norm):
            return False
        words_a = set(a_norm.split())
        words_b = set(b_norm.split())
        if not words_a or not words_b:
            return False
        intersection = len(words_a & words_b)
        union = len(words_a | words_b)
        return (intersection / union) >= SIMILARITY_THRESHOLD

    async def _trip(self, reason: str, force_kill: bool = False):
        PrintStyle(font_color="red", bold=True, padding=True).print(reason)
        self.agent.context.log.log(
            type="warning", heading="Circuit Breaker", content=reason
        )

        if force_kill:
            error_response = (
                "I was unable to complete this task. My execution loop hit "
                "the safety limit. Here is what I gathered before stopping:\n\n"
                f"Iterations completed: {self.agent.loop_data.iteration}\n"
                f"Reason: {reason}"
            )
            self.agent.hist_add_ai_response(error_response)
            self.agent.context.log.log(
                type="response",
                heading=f"{self.agent.agent_name}: Circuit breaker forced stop",
                content=error_response,
            )
            raise HandledException(reason)
        else:
            bail_message = (
                "SYSTEM OVERRIDE: You have been running in a loop without producing "
                "a result. You must call the 'response' tool NOW with whatever output "
                "you have so far. Do not plan, do not describe what you will do. Call "
                "the response tool immediately with a summary of what you accomplished "
                "and what remains incomplete."
            )
            self.agent.hist_add_warning(bail_message)

This won't replace a proper stop button in the UI, but it prevents the runaway loop problem. The thresholds are configurable at the top of the file — I'd suggest starting with the defaults and adjusting if you get false positives on legitimate multi-step tasks.

jgrooms avatar Feb 25 '26 17:02 jgrooms

Thanks. What are the main threshold tweaks you recommend in case of problems?

nigelp avatar Feb 25 '26 18:02 nigelp

It will likely depend on the model you are are using so just experiment. If it just doesn't work you can just remove it. It's a hack workaround so at least the agent doesn't get stuck until a better solution comes along.

jgrooms avatar Feb 25 '26 18:02 jgrooms

A stop button would do wonders for testing. I am creating skills from scratch within AgentZero. Sometimes it fails to read the skill.md and begins to do the default actions. It may take several minutes before it gives up and realizes it was doing things wrong.

Could save so much time in developing skills if I just had a true abort button to stop everything.

cloakeditwizard avatar Mar 01 '26 10:03 cloakeditwizard

Closing due to inactivity of 90 days.

github-actions[bot] avatar May 31 '26 04:05 github-actions[bot]