MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / detectStuckLoop

Function detectStuckLoop

src/agent/recovery.ts:186–203  ·  view source on GitHub ↗
(recentToolCalls: Array<{ name: string; argsHash: string }>)

Source from the content-addressed store, hash-verified

184}
185
186export function detectStuckLoop(recentToolCalls: Array<{ name: string; argsHash: string }>): boolean {
187 const n = recentToolCalls.length;
188 if (n < 3) return false;
189 const keys = recentToolCalls.map(c => `${c.name}|${c.argsHash}`);
190
191 // (a) period-1: last three identical
192 if (keys[n - 1] === keys[n - 2] && keys[n - 2] === keys[n - 3]) return true;
193
194 // (b) period-p: the last 2p calls are two identical halves (a repeated cycle)
195 for (let p = 2; p <= Math.floor(n / 2); p++) {
196 let cycle = true;
197 for (let i = 0; i < p; i++) {
198 if (keys[n - 1 - i] !== keys[n - 1 - i - p]) { cycle = false; break; }
199 }
200 if (cycle) return true;
201 }
202 return false;
203}

Callers 2

runMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected