(recentToolCalls: Array<{ name: string; argsHash: string }>)
| 184 | } |
| 185 | |
| 186 | export 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 | } |
no outgoing calls
no test coverage detected