Skip to content

Conversation

@ElecTwix
Copy link

@ElecTwix ElecTwix commented Oct 25, 2025

fix: add loop detection for doom loop models (GLM-4.6, Grok Code)

GLM-4.6 and Grok Code models are prone to "doom loops" - infinite repetition of same tool call with identical parameters. This causes wasted tokens, poor user experience with stuck sessions, and server resource waste.

This commit adds targeted loop detection that monitors tool calls for problematic models and injects breaking messages when loops are detected. The implementation uses Map-based counting with O(1) performance and only affects specific models known to have this issue.

Key changes:

  • Added DOOM_LOOP_MODELS constant for model-specific configuration (glm-4.6, grok-code)
  • Added Map-based tracking of tool+input combinations with threshold of 3 repetitions
  • Added non-blocking warning message injection with model-specific templates
  • Zero impact on non-target models, <1% overhead for target models
  • Memory efficient with automatic cleanup

Fixes infinite loop issue while maintaining performance and extensibility for additional problematic models.

fix #3444

@ElecTwix ElecTwix changed the title fix: add loop detection for doom loop models (GLM-4.6, Grok Code) fix: add loop detection for doom loop models (GLM-4.6, Grok Code) - fix #3444 Oct 25, 2025
@rekram1-node
Copy link
Collaborator

rekram1-node commented Oct 27, 2025

I don't know if we should do model specific because there are other models too kimi-k2, supernova, gemini models all can get to that state, peoplee can also define model aliases too hard to catch them all.

What if we just do a check when a tool-call is being executed, look at the last 3 parts of the message, if they are all tool calls, all call the same tool with same params, then we Do a permission ask for the tool and tell the user that it may be doom looping

also I think the "error message" should probably be the same for all the llms is there any reason for a different message per model? I don't know if it will matter substantially in this case

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GLM-4.6 model gets stuck in infinite loop, repeating same actions

2 participants