refactor: improve error handling and retry logic in exploration state for enhanced stability
This commit is contained in:
@@ -26,47 +26,60 @@ export class ExploringState extends BaseState {
|
|||||||
console.log(`[${this.turtle.id}] Starting exploration`);
|
console.log(`[${this.turtle.id}] Starting exploration`);
|
||||||
|
|
||||||
while (!this.cancelled) {
|
while (!this.cancelled) {
|
||||||
// Safety checks
|
try {
|
||||||
const fuel = await this.checkFuel();
|
// Safety checks
|
||||||
if (fuel !== 'unlimited' && fuel < this.minFuel) {
|
const fuel = await this.checkFuel();
|
||||||
const refueled = await this.tryRefuel();
|
if (fuel !== 'unlimited' && fuel < this.minFuel) {
|
||||||
if (!refueled) {
|
const refueled = await this.tryRefuel();
|
||||||
this.turtle.setState('goHome', { reason: 'low_fuel' });
|
if (!refueled) {
|
||||||
|
this.turtle.setState('goHome', { reason: 'low_fuel' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check distance
|
||||||
|
if (this._isTooFar()) {
|
||||||
|
this.turtle.setState('goHome', { reason: 'too_far' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check inventory
|
||||||
|
const isFull = await this.isInventoryFull();
|
||||||
|
if (isFull) {
|
||||||
|
this.turtle.setState('goHome', { reason: 'inventory_full', returnState: 'exploring' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark position
|
||||||
|
const pos = this.turtle.position;
|
||||||
|
if (pos) {
|
||||||
|
this.visitedPositions.add(`${pos.x},${pos.y},${pos.z}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprehensive scan
|
||||||
|
const scanResult = await this.scanSurroundings();
|
||||||
|
if (scanResult) {
|
||||||
|
this.blocksDiscovered += Object.keys(scanResult).length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mine any valuable ores we find
|
||||||
|
yield* this._checkAndMineOres();
|
||||||
|
|
||||||
|
// Exploration movement
|
||||||
|
yield* this._exploreStep();
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
const isTimeout = error.message?.includes('timed out');
|
||||||
|
if (isTimeout) {
|
||||||
|
console.warn(`[${this.turtle.id}] Exploration exec timeout, will retry next iteration`);
|
||||||
|
// Wait longer before retrying after a timeout
|
||||||
|
await this._sleep(3000);
|
||||||
|
} else {
|
||||||
|
// Non-timeout errors still propagate
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check distance
|
|
||||||
if (this._isTooFar()) {
|
|
||||||
this.turtle.setState('goHome', { reason: 'too_far' });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check inventory
|
|
||||||
const isFull = await this.isInventoryFull();
|
|
||||||
if (isFull) {
|
|
||||||
this.turtle.setState('goHome', { reason: 'inventory_full', returnState: 'exploring' });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark position
|
|
||||||
const pos = this.turtle.position;
|
|
||||||
if (pos) {
|
|
||||||
this.visitedPositions.add(`${pos.x},${pos.y},${pos.z}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Comprehensive scan
|
|
||||||
const scanResult = await this.scanSurroundings();
|
|
||||||
if (scanResult) {
|
|
||||||
this.blocksDiscovered += Object.keys(scanResult).length;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mine any valuable ores we find
|
|
||||||
yield* this._checkAndMineOres();
|
|
||||||
|
|
||||||
// Exploration movement
|
|
||||||
yield* this._exploreStep();
|
|
||||||
|
|
||||||
yield;
|
yield;
|
||||||
await this._sleep(300);
|
await this._sleep(300);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user