coder-agent 2.7.2 → 2.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent.js +72 -0
- package/package.json +1 -1
package/dist/agent.js
CHANGED
|
@@ -180,6 +180,62 @@ function formatResponseText(text) {
|
|
|
180
180
|
return chalk.white(part);
|
|
181
181
|
}).join('');
|
|
182
182
|
}
|
|
183
|
+
function normalizeContentForLoopCheck(content) {
|
|
184
|
+
if (!content)
|
|
185
|
+
return "";
|
|
186
|
+
return content
|
|
187
|
+
.toLowerCase()
|
|
188
|
+
.replace(/\s+/g, " ")
|
|
189
|
+
.trim();
|
|
190
|
+
}
|
|
191
|
+
function normalizeToolCallsForLoopCheck(toolCalls) {
|
|
192
|
+
if (!toolCalls || toolCalls.length === 0)
|
|
193
|
+
return "";
|
|
194
|
+
return toolCalls.map(tc => {
|
|
195
|
+
const name = tc.function?.name || "";
|
|
196
|
+
let argsStr = "";
|
|
197
|
+
try {
|
|
198
|
+
const rawArgs = tc.function?.arguments;
|
|
199
|
+
const args = typeof rawArgs === 'string'
|
|
200
|
+
? JSON.parse(rawArgs)
|
|
201
|
+
: rawArgs;
|
|
202
|
+
if (args && typeof args === 'object') {
|
|
203
|
+
const sortedArgs = {};
|
|
204
|
+
for (const key of Object.keys(args).sort()) {
|
|
205
|
+
sortedArgs[key] = args[key];
|
|
206
|
+
}
|
|
207
|
+
argsStr = JSON.stringify(sortedArgs);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
argsStr = tc.function?.arguments || "";
|
|
212
|
+
}
|
|
213
|
+
return `${name}(${argsStr})`;
|
|
214
|
+
}).join(";");
|
|
215
|
+
}
|
|
216
|
+
function hasRepeatingCycle(history) {
|
|
217
|
+
const n = history.length;
|
|
218
|
+
for (let len = 1; len <= 4; len++) {
|
|
219
|
+
if (n >= len * 2) {
|
|
220
|
+
const minRepeats = len === 1 ? 3 : 2;
|
|
221
|
+
if (n >= len * minRepeats) {
|
|
222
|
+
let isLoop = true;
|
|
223
|
+
const lastBlock = history.slice(n - len);
|
|
224
|
+
for (let r = 1; r < minRepeats; r++) {
|
|
225
|
+
const prevBlock = history.slice(n - len * (r + 1), n - len * r);
|
|
226
|
+
if (JSON.stringify(lastBlock) !== JSON.stringify(prevBlock)) {
|
|
227
|
+
isLoop = false;
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if (isLoop) {
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
183
239
|
// ─── Extract Text Tool Calls ──────────────────────────────────────────────────
|
|
184
240
|
function extractTextToolCalls(content) {
|
|
185
241
|
const calls = [];
|
|
@@ -568,6 +624,7 @@ export class Agent {
|
|
|
568
624
|
const MAX_WAITS = 3;
|
|
569
625
|
const modifiedFiles = new Set();
|
|
570
626
|
let cleanContent = "";
|
|
627
|
+
const stateHistory = [];
|
|
571
628
|
while (true) {
|
|
572
629
|
if (signal?.aborted) {
|
|
573
630
|
const abortErr = new Error("The user aborted a request.");
|
|
@@ -707,6 +764,21 @@ export class Agent {
|
|
|
707
764
|
for (const line of statusLines) {
|
|
708
765
|
console.log(line);
|
|
709
766
|
}
|
|
767
|
+
// Loop detection & intervention
|
|
768
|
+
const currentKey = `${normalizeContentForLoopCheck(msg.content || "")}|${normalizeToolCallsForLoopCheck(toolCalls)}`;
|
|
769
|
+
const tempHistory = [...stateHistory, currentKey];
|
|
770
|
+
if (hasRepeatingCycle(tempHistory)) {
|
|
771
|
+
const warningMessage = `⚠️ [LOOP DETECTED] You are repeating the exact same thoughts or tool calls. Please break out of this loop. Do not repeat the same actions. Re-evaluate your strategy, look at a different file, run a different command, or ask the user for clarification if you cannot proceed.`;
|
|
772
|
+
console.log(chalk.hex('#ff9f0a')('\n⚠ Loop detected! Intervening to break the loop...'));
|
|
773
|
+
this.memory.add({
|
|
774
|
+
role: "user",
|
|
775
|
+
content: warningMessage,
|
|
776
|
+
});
|
|
777
|
+
stateHistory.length = 0; // Reset history to allow a fresh start
|
|
778
|
+
}
|
|
779
|
+
else {
|
|
780
|
+
stateHistory.push(currentKey);
|
|
781
|
+
}
|
|
710
782
|
if (iterations < MAX_ITERATIONS) {
|
|
711
783
|
console.log(chalk.dim('\n' + '─'.repeat(48) + '\n'));
|
|
712
784
|
startSpinner("thinking...");
|