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
Comment on lines +917 to +918
"glm-4.6": `⚠️ Loop detected! You have been calling "%TOOL%" with the same parameters repeatedly. This is causing an infinite loop. Please STOP calling this tool and try a different approach or modify the parameters.`,
"grok-code": `⚠️ Loop detected! You're stuck repeating "%TOOL%" with identical inputs. Break this pattern and try a different strategy or change the parameters.`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be model specific? Almost seems like it should be model agnostic

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made it model-specific since only GLM-4.6 and Grok Code are known to have this issue. Other models work fine and we don't want to add overhead for them. Could make it agnostic later if more models show this behavior.

Comment on lines +1066 to +1071
const key = `${value.toolName}|${JSON.stringify(value.input)}`
const count = (loopCounts.get(key) || 0) + 1
loopCounts.set(key, count)

if (count >= LOOP_DETECTION_THRESHOLD) {
await Session.updatePart({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this how everyone else does loop detection too?

Copy link
Author

@ElecTwix ElecTwix Oct 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this is how everyone else does it, but this approach should work. Tool+input tracking with thresholds using simple Map-based counting with O(1) performance keeps it lightweight and effective.

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