Introduction
Release 0.177.7 delivers a critical fix to the bot’s spawn prioritization system that prevents a catastrophic energy starvation deadlock scenario. When all harvesters die while other creeps remain alive, the system could previously enter a deadlock where non-harvester creeps continuously spawned and consumed energy without generating income, leading to total workforce collapse. This release introduces explicit harvester priority checks and refactored emergency spawn logic to ensure energy production is always prioritized.
Key Features
Critical Harvester Priority System
- Explicit priority check prevents energy starvation deadlock when harvester count drops to zero
- Forces harvester spawn before any other role when harvesters are absent but other creeps exist
- Blocks all non-harvester spawns until at least one harvester is successfully spawned
Refactored Emergency Spawn Logic
- Extracted emergency spawn logic into reusable
attemptEmergencyHarvesterSpawn()helper method - Unified handling for both total workforce collapse (EMERGENCY) and harvester starvation (CRITICAL) scenarios
- Improved error reporting with detailed failure diagnostics
Technical Details
The Energy Starvation Deadlock Problem
Prior to this release, the spawn system had a critical vulnerability in its prioritization logic. The sequence of events leading to deadlock:
- All harvesters die (from hostile action, CPU timeout, or other causes)
- Other creeps (upgraders, builders, etc.) remain alive
- System detects “not emergency” because
totalCreeps > 0 - Normal spawn logic attempts to spawn builders, upgraders, or other roles
- These spawns consume energy without generating any income
- Energy depletes further, preventing harvester spawns
- System enters permanent deadlock state
This scenario was particularly insidious because the bot appeared “alive” with active creeps, but was actually in an unrecoverable state without manual intervention.
Design Rationale
The fix implements a two-tier emergency response system:
Tier 1: Total Workforce Collapse (EMERGENCY)
1 | if (totalCreeps === 0) { |
This handles the complete workforce loss scenario where no creeps exist at all.
Tier 2: Harvester Starvation (CRITICAL)
1 | if (harvesterCount === 0 && totalCreeps > 0) { |
This new check catches the energy starvation deadlock scenario where harvesters are gone but other creeps remain.
Why This Approach?
Alternative approaches were considered but rejected:
- Role Priority Weights: Complex weight system for all roles - rejected as over-engineered
- Energy Threshold Checks: Spawn harvesters when energy is low - reactive rather than preventive
- Role Dependency Graph: Explicit dependencies between roles - too rigid for dynamic gameplay
The chosen approach is surgical and minimal: it adds exactly one conditional check at the critical decision point, preventing the deadlock without restructuring the entire spawn system. This aligns with the repository’s principle of making “the smallest possible changes to achieve the goal.”
Implementation Details
The refactored attemptEmergencyHarvesterSpawn() method in packages/bot/src/runtime/behavior/RoleControllerManager.ts provides:
Explicit Return Contract
1 | { success: boolean; blocked: boolean; error?: string } |
Clear success/failure state with optional error messages for diagnostics.
Comprehensive Error Handling
- No spawn available:
{ success: false, blocked: true, error: "No spawn available" } - Insufficient energy:
{ success: false, blocked: true, error: "Insufficient energy (X) for minimal body (need 200)" } - Spawn failure:
{ success: false, blocked: true, error: "Spawn failed: ERROR_CODE" }
Integration with Existing Systems
- Uses
BodyComposer.generateEmergencyBody()for minimal viable harvester bodies - Respects role controller memory initialization via
controller.createMemory() - Updates spawn tracking arrays and role counts for consistency
File Changes
Modified Files:
packages/bot/src/runtime/behavior/RoleControllerManager.ts- Core spawn prioritization logictests/regression/role-controller-manager-spawning.test.ts- Regression test coverage
Key Code Sections:
- Lines 385-401: New critical harvester priority check
- Lines 837-895: Extracted
attemptEmergencyHarvesterSpawn()helper method
Bug Fixes
Prevented Energy Starvation Deadlock
- Fixed scenario where zero harvesters + existing creeps = permanent deadlock
- System now correctly prioritizes harvesters above all other roles when harvester count drops to zero
- Blocks all spawns until harvester spawn succeeds, preventing energy drain from non-productive creeps
Improved Emergency Spawn Reliability
- Unified emergency spawn logic eliminates code duplication
- Better error diagnostics for spawn failures
- Consistent handling across emergency and critical scenarios
Breaking Changes
None. This is a pure bug fix with no API changes or behavior modifications for normal operation. The spawn system continues to function identically when harvesters are present.
Impact
Immediate Benefits
Autonomous Recovery: The bot can now autonomously recover from harvester loss without manual intervention, even when other creeps are alive.
Diagnostic Clarity: Enhanced logging with [CRITICAL] and [EMERGENCY] prefixes makes it immediately clear which recovery mode is active.
Spawn Efficiency: By blocking wasteful non-harvester spawns during energy starvation, the system conserves energy for the critical harvester spawn.
Strategic Implications
This fix is particularly important for the autonomous AI’s ability to handle unexpected scenarios:
- Combat Resilience: When hostile invaders specifically target harvesters, the bot can recover
- CPU Timeout Recovery: After CPU-induced workforce collapse, harvester priority ensures correct recovery order
- Multi-Room Scaling: As the bot expands to multiple rooms, this pattern prevents cascading failures
Performance Characteristics
CPU Impact: Negligible - adds one additional conditional check per tick
Memory Impact: Zero - no new memory structures
Execution Time: <0.01 CPU per check
What’s Next
This emergency spawn system establishes a foundation for future improvements:
Harvester Redundancy: Consider maintaining minimum 2 harvesters at all times to prevent single-point-of-failure
Role Dependency System: Generalize priority logic to handle other critical role dependencies (e.g., haulers for storage-based economy)
Predictive Spawn Planning: Spawn replacement harvesters before current harvesters die (TTL-based)
The fix directly supports the bot’s Phase 1: Foundation objectives by ensuring stable bootstrap behavior and autonomous recovery from edge cases. Related issues and future work:
- Issue #1490: Energy starvation deadlock (resolved by this release)
- Issue #998: Zero creep population investigation (prevented by this fix)
- Issue #1002: Emergency spawn bootstrap (enhanced by this implementation)
Contributors: Copilot SWE Agent, @ralphschuler
Pull Request: #1490
Deployment: Automated via CI/CD pipeline