refactor: enhance state loop error handling with consecutive error tracking for improved resilience

This commit is contained in:
MayaTheShy
2026-02-20 04:03:23 -05:00
parent b34cc8cec0
commit 2c806bf994

View File

@@ -288,9 +288,8 @@ export class Turtle extends EventEmitter {
/**
* Run the current state's act() generator in a loop
*/
async _runStateLoop() {
async _runStateLoop(consecutiveErrors = 0) {
const state = this._state;
let consecutiveErrors = 0;
const MAX_CONSECUTIVE_ERRORS = 5;
try {
@@ -312,10 +311,10 @@ export class Turtle extends EventEmitter {
if (isTimeout && consecutiveErrors < MAX_CONSECUTIVE_ERRORS) {
console.warn(`[Turtle ${this.id}] Timeout in ${state.name} (${consecutiveErrors}/${MAX_CONSECUTIVE_ERRORS}), retrying...`);
// Wait a bit then restart the state loop
// Wait a bit then restart the state loop, passing the error count
await new Promise(r => setTimeout(r, 2000));
if (this._state === state) {
this._runStateLoop(); // Restart the loop with the same state
this._runStateLoop(consecutiveErrors);
}
} else {
console.error(`[Turtle ${this.id}] State error in ${state.name}:`, error.message);