[PR #640] serve: the loop guard across turns - an agent's tool call made again and again with the same result gets a message to try something else #644

Open
opened 2026-10-29 21:27:48 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/640
Author: @merbanan
Created: 10/28/2026
Status: 🔄 Open

Base: main ← Head: pr/loop-guard-tools


📝 Commits (1)

  • fc5db3e serve: the loop guard across turns - an agent's tool call made again and again with the same arguments and result gets a message after its 3rd result: nothing new, try a different approach or answer (GLM-5.3 over opencode ran two commands in turn 25 times, every answer short and fine; loop_guard.tool_repeat, maya:loop_guard_tool_repeats_total)

📊 Changes

5 files changed (+160 additions, -11 deletions)

View changed files

📝 docs/SETTINGS.md (+8 -1)
📝 serve/loop_guard.py (+56 -0)
📝 serve/server.py (+29 -7)
📝 serve/test_loop_guard.py (+63 -1)
📝 serve/web/monitor.js (+4 -2)

📄 Description

What

The loop guard catches a loop inside one answer: a block of tokens repeated back to back. An agent can also loop across turns, where every answer is short and fine on its own:

  1. it runs a tool call;
  2. it gets the same result as last time;
  3. it thinks the same thing and runs the same call again.

The guard never sees this, because each answer is a new request.

Seen on the live service. GLM-5.3-Flash (EXL3 2.05bpw) was working over opencode in a 65K conversation.

  • It ran two bash commands in turn about 25 times in three minutes: an nmcli … | grep pmf and a grep … src/uplink.cpp.
  • Each time it got the same output and wrote the same reasoning ("So uplink retryIn:0 means…").
  • The server log showed only normal answers of 46, 224 or 286 tokens, each ending in a tool call. The loop guard's counters didn't move.
  • It ended when the user typed "stop repeating and try a different approach". The next answer moved on at once and found the cause.

The change

When a request ends with tool results, the server looks at the tool calls since the user's last message (loop_guard.tool_repeat).

When it counts as a loop: the last call's name, arguments and result are all the same as at least 2 others among the last 12 calls. That makes this call the 3rd of its kind.

What happens then: a short user message follows that result, marked as the server's:

[The server's loop guard, not the user: bash has now run 3 times since the user's last message with these same arguments and given this same result. Running it again will not show anything new. Do not repeat it: use what you already have, try a different approach, or answer the user.]

  • Only that prompt changes. The client's conversation stays as it sent it.
  • Its next request matches the kept one up to that result, so only the tail is read again.
  • The model goes on; nothing is stopped.

What doesn't count:

  • A command run again after an edit (make test with another result each time).
  • A poll whose output changes.
  • Anything before the user's last message.

Reporting:

  • The server window gets [maya] loop guard: bash 3 times with the same arguments and result: a note after it.
  • /metrics loop_guard.tool_repeats and maya:loop_guard_tool_repeats_total count these notes, and the Monitor's totals line includes them.

Settings: the same as the loop guard's (on by default; MAYA_LOOP_GUARD=0, "loop_guard": false in the config or in a request). It covers Chat Completions, Anthropic Messages, Responses and the web app's MCP rounds. docs/SETTINGS.md has a bullet under "The loop guard".

Why a message and not text in the tool's result. I tried both on the live model with the same conversation: three identical nmcli rounds, temperature 0.

the next call
no note ("loop_guard": false) the same nmcli command a 4th time
the note appended to the tool's result the same nmcli command a 4th time
the note as a message after the result (this PR) a different command (key-mgmt; in a second run the band, channel and logs), with reasoning such as "a different command (allowed — different approach)"

Tests

serve/test_loop_guard.py::ToolRepeat (2 tests). They cover:

  • the opencode pattern (two calls in turn), which gets the note at the 3rd repeat but not at the 2nd;
  • a command whose result changes, which doesn't count;
  • a user message in between, which starts the count over;
  • a request that doesn't end with a tool result, which gets nothing;
  • the note itself, which doesn't count as the user speaking;
  • the rendered prompt, which gets the note while the request's messages stay unchanged;
  • "loop_guard": false, which turns it off;
  • the counters in /metrics and Prometheus.

All 24 serve/test_*.py suites pass on v1.1.36. The change has been running on the live V100 GLM service.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/mw00/project-maya/pull/640 **Author:** [@merbanan](https://github.com/merbanan) **Created:** 10/28/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr/loop-guard-tools` --- ### 📝 Commits (1) - [`fc5db3e`](https://github.com/mw00/project-maya/commit/fc5db3e104412a2e930c583dc31b6ff2cb9b6ef3) serve: the loop guard across turns - an agent's tool call made again and again with the same arguments and result gets a message after its 3rd result: nothing new, try a different approach or answer (GLM-5.3 over opencode ran two commands in turn 25 times, every answer short and fine; loop_guard.tool_repeat, maya:loop_guard_tool_repeats_total) ### 📊 Changes **5 files changed** (+160 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `docs/SETTINGS.md` (+8 -1) 📝 `serve/loop_guard.py` (+56 -0) 📝 `serve/server.py` (+29 -7) 📝 `serve/test_loop_guard.py` (+63 -1) 📝 `serve/web/monitor.js` (+4 -2) </details> ### 📄 Description ## What The loop guard catches a loop inside one answer: a block of tokens repeated back to back. An agent can also loop across turns, where every answer is short and fine on its own: 1. it runs a tool call; 2. it gets the same result as last time; 3. it thinks the same thing and runs the same call again. The guard never sees this, because each answer is a new request. **Seen on the live service.** GLM-5.3-Flash (EXL3 2.05bpw) was working over opencode in a 65K conversation. - It ran two bash commands in turn about 25 times in three minutes: an `nmcli … | grep pmf` and a `grep … src/uplink.cpp`. - Each time it got the same output and wrote the same reasoning ("So uplink retryIn:0 means…"). - The server log showed only normal answers of 46, 224 or 286 tokens, each ending in a tool call. The loop guard's counters didn't move. - It ended when the user typed "stop repeating and try a different approach". The next answer moved on at once and found the cause. ## The change When a request ends with tool results, the server looks at the tool calls since the user's last message (`loop_guard.tool_repeat`). **When it counts as a loop:** the last call's name, arguments and result are all the same as at least 2 others among the last 12 calls. That makes this call the 3rd of its kind. **What happens then:** a short user message follows that result, marked as the server's: > [The server's loop guard, not the user: `bash` has now run 3 times since the user's last message with these same arguments and given this same result. Running it again will not show anything new. Do not repeat it: use what you already have, try a different approach, or answer the user.] - Only that prompt changes. The client's conversation stays as it sent it. - Its next request matches the kept one up to that result, so only the tail is read again. - The model goes on; nothing is stopped. **What doesn't count:** - A command run again after an edit (`make test` with another result each time). - A poll whose output changes. - Anything before the user's last message. **Reporting:** - The server window gets `[maya] loop guard: `bash` 3 times with the same arguments and result: a note after it`. - `/metrics` `loop_guard.tool_repeats` and `maya:loop_guard_tool_repeats_total` count these notes, and the Monitor's totals line includes them. **Settings:** the same as the loop guard's (on by default; `MAYA_LOOP_GUARD=0`, `"loop_guard": false` in the config or in a request). It covers Chat Completions, Anthropic Messages, Responses and the web app's MCP rounds. `docs/SETTINGS.md` has a bullet under "The loop guard". **Why a message and not text in the tool's result.** I tried both on the live model with the same conversation: three identical `nmcli` rounds, temperature 0. | | the next call | |---|---| | no note (`"loop_guard": false`) | the same `nmcli` command a 4th time | | the note appended to the tool's result | the same `nmcli` command a 4th time | | the note as a message after the result (this PR) | a different command (key-mgmt; in a second run the band, channel and logs), with reasoning such as "a different command (allowed — different approach)" | ## Tests `serve/test_loop_guard.py::ToolRepeat` (2 tests). They cover: - the opencode pattern (two calls in turn), which gets the note at the 3rd repeat but not at the 2nd; - a command whose result changes, which doesn't count; - a user message in between, which starts the count over; - a request that doesn't end with a tool result, which gets nothing; - the note itself, which doesn't count as the user speaking; - the rendered prompt, which gets the note while the request's messages stay unchanged; - `"loop_guard": false`, which turns it off; - the counters in `/metrics` and Prometheus. All 24 `serve/test_*.py` suites pass on v1.1.36. The change has been running on the live V100 GLM service. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mw00/project-maya#644
No description provided.