a few protections from infinite loops

This commit is contained in:
MaxRobinsonTheGreat 2025-08-15 16:14:27 -05:00
parent ccbaca6ecb
commit 61e1010e49
3 changed files with 29 additions and 2 deletions

View file

@ -7,6 +7,8 @@ export class ActionManager {
this.timedout = false; this.timedout = false;
this.resume_func = null; this.resume_func = null;
this.resume_name = ''; this.resume_name = '';
this.last_action_time = 0;
this.recent_action_counter = 0;
} }
async resumeAction(actionFn, timeout) { async resumeAction(actionFn, timeout) {
@ -59,6 +61,25 @@ export class ActionManager {
async _executeAction(actionLabel, actionFn, timeout = 10) { async _executeAction(actionLabel, actionFn, timeout = 10) {
let TIMEOUT; let TIMEOUT;
try { try {
if (this.last_action_time > 0) {
let time_diff = Date.now() - this.last_action_time;
if (time_diff < 20) {
this.recent_action_counter++;
}
else {
this.recent_action_counter = 0;
}
if (this.recent_action_counter > 2) {
console.warn('Fast action loop detected, cancelling resume.');
this.cancelResume(); // likely cause of repetition
}
if (this.recent_action_counter > 5) {
console.error('Infinite action loop detected, shutting down.');
this.agent.cleanKill('Infinite action loop detected, shutting down.');
return { success: false, message: 'Infinite action loop detected, shutting down.', interrupted: false, timedout: false };
}
}
this.last_action_time = Date.now();
console.log('executing code...\n'); console.log('executing code...\n');
// await current action to finish (executing=false), with 10 seconds timeout // await current action to finish (executing=false), with 10 seconds timeout

View file

@ -439,7 +439,11 @@ export class Agent {
this.bot.clearControlStates(); this.bot.clearControlStates();
this.bot.pathfinder.stop(); // clear any lingering pathfinder this.bot.pathfinder.stop(); // clear any lingering pathfinder
this.bot.modes.unPauseAll(); this.bot.modes.unPauseAll();
this.actions.resumeAction(); setTimeout(() => {
if (this.isIdle()) {
this.actions.resumeAction();
}
}, 1000);
}); });
// Init NPC controller // Init NPC controller

View file

@ -1084,13 +1084,15 @@ function startDoorInterval(bot) {
// shuffle positions so we're not always opening the same door // shuffle positions so we're not always opening the same door
const positions = [ const positions = [
bot.entity.position.clone(), bot.entity.position.clone(),
bot.entity.position.offset(0, 0, 1), // north bot.entity.position.offset(0, 0, 1),
bot.entity.position.offset(0, 0, -1), bot.entity.position.offset(0, 0, -1),
bot.entity.position.offset(1, 0, 0), bot.entity.position.offset(1, 0, 0),
bot.entity.position.offset(-1, 0, 0), bot.entity.position.offset(-1, 0, 0),
] ]
let elevated_positions = positions.map(position => position.offset(0, 1, 0)); let elevated_positions = positions.map(position => position.offset(0, 1, 0));
positions.push(...elevated_positions); positions.push(...elevated_positions);
positions.push(bot.entity.position.offset(0, 2, 0)); // above head
positions.push(bot.entity.position.offset(0, -1, 0)); // below feet
let currentIndex = positions.length; let currentIndex = positions.length;
while (currentIndex != 0) { while (currentIndex != 0) {