# Agent loop guards

| Stuck in | Guard in `openprogram/runtime/agent/loop/` | Acts | Result |
| --- | --- | --- | --- |
| Repeated text | `guards._RepeatedTextGuard` | Stream consumer, `streaming.py` | Response discarded and asked again |
| Repeated tool rounds | `tool_loop_guard.ToolLoopGuard` | After each tool round, `loop.py` | Turn ends with a notice |
| Tool calls written as text | `text_tool_calls.recover_text_tool_calls` | When a reply ends, `streaming.py` | Written calls become real ones |
| Repeated failing call | `failure_policy` and the tool executor | Each tool call, `tool_dispatch.py` | Third identical failure is not run; a note answers it |
| Long stretch with nothing shown | `progress_note.QuietStretch` | After each tool round of a turn somebody is watching, `loop.py` | After 6 rounds of tool calls only, one note for this turn asks for a few words of progress; nothing ends |

A healthy turn is left alone. Nothing here caps output length, round count or wall time.

## Repeated text

- **Checked on**: A response offered tools; a bounded tail of its text
- **Trigger**: The tail ends in one exact segment of 32–1024 characters, repeated at least 8 times over at least 2 KB
- **Raises**: `RepetitiveOutputError`
- **Loop**: Closes the provider effect, keeps every completed tool result, asks again with a user note to continue without repeating. Draws on the turn's shared recovery allowance.
- **Allowance spent**: The error goes up, non-retryable, so `Runtime.exec` never replays completed tool effects in a new session

## Repeated tool rounds

`ToolLoopGuard` counts consecutive rounds with the same calls, arguments and results. At `TOOL_LOOP_REPEATS` (8) the turn ends before the next request, with an error naming the call. The user's next message carries the conversation on.

```
Stopped: the agent made the same tool call 8 times in a row and got the same result every time …
```

| Compared | How |
| --- | --- |
| Calls | Name and arguments with keys in one order (`failure_policy.repeat_key`); `bash`'s presentation-only `description` is ignored |
| Results | Error flag, text and image content. Text about the run itself is normalized: `elapsed 1.2s`, `Wall time`, `duration_seconds` |
| Resets the count | A changed result; anything the user says mid-turn (steering) |
| Never a loop | A round of only waits: a tool whose name has the word sleep, wait, delay, pause or poll (`clock__sleep`, `waitForCi`), or whose arguments are only a duration |

## Tool calls written as text

Deep into a long conversation a model may write calls in its chat template's shape instead of making them; nothing runs, the turn ends, and on "go on" it reads its own written call back and writes the next one the same way.

When a reply to a request that offered tools ends with no call, `text_tool_calls.read_written_calls` reads it from the first call-shaped tag.

| Model family | Shape read |
| --- | --- |
| Hermes, Qwen | `<tool_call>{"name": …, "arguments": {…}}</tool_call>`; arguments an object or a JSON string |
| GLM | `<tool_call>name<arg_key>k</arg_key><arg_value>v</arg_value></tool_call>` |
| Claude, MiniMax | `<invoke name="…"><parameter name="k">v</parameter></invoke>`, bare or inside `<function_calls>` or `<minimax:tool_call>` |
| DeepSeek DSML | The same invoke with `｜DSML｜` in each tag, one or two pipes of either width |

| Case | Outcome |
| --- | --- |
| Every block names an offered tool, arguments readable | Blocks become `ToolCall`s; the reply ends as a tool-use turn |
| Text before the calls | Kept |
| Text after the calls | Dropped: a made-up result. The real one follows. |
| Parameter value | Taken as JSON when it is JSON, unless DSML marks it `string="true"` |
| Unknown tool, unreadable arguments, a tag in prose | Reply left as it was |

Recovery runs in the stream consumer before the reply is committed, so context, durable record and UI all see the real calls. The tool-loop guard and text-call recovery follow magpie's (yetone/magpie, MIT).
