Skip to content

Conversation

@TooAngel
Copy link
Owner

@TooAngel TooAngel commented Nov 1, 2025

Summary

This PR fixes two critical infinite loop issues that were wasting CPU every tick:

  1. Mineral Creep Infinite Loop (continuation of PR fix: resolve mineral infinite loops, spawning limits, and trapped detection #745)
  2. Squad Bouncing at Room Borders (new issue discovered during investigation)

Problem 1: Mineral Creep Infinite Loop

Issue

After PR #745, mineral creeps were still experiencing infinite loops when:

  • Terminal has 1-4 units of a required mineral
  • setState validation checks for === 0 (passes with 1-4 units)
  • Withdrawal attempt fails because amount < LAB_REACTION_AMOUNT (5)
  • State gets cleared but immediately re-set on the same tick
  • Loop continues wasting CPU every tick

Example log spam:

66153220 E17S52 mineral-121727 [room E17S52 pos 23,11] setStatePrepareReactionLab1WithResource
66153220 E17S52 mineral-121727 [room E17S52 pos 23,11] No resource available from terminal, clearing state

Root Cause

  1. Inconsistent validation: setState functions checked for === 0 while room cleanup logic checked for < LAB_REACTION_AMOUNT
  2. No reaction cleanup on failure: When withdrawal failed, only the creep state was cleared but not the room's reaction memory
  3. Same-tick re-setting: After state clearing, the state machine would immediately try to set the same state again

Changes (role_mineral.js)

  1. Fix validation thresholds in setStatePrepareReactionLab1WithResource() and setStatePrepareReactionLab2WithResource():

    • Changed check from === 0 to < LAB_REACTION_AMOUNT
    • Now consistent with room cleanup logic
  2. Add self-healing cleanup in handleWithdrawFromSource():

    • When resource is unavailable or insufficient, also delete room.memory.reaction
    • Prevents immediate re-setting of the same failed state
    • Enhanced validation to check resource amount against LAB_REACTION_AMOUNT

Problem 2: Squad Bouncing at Room Borders

Issue

Squad creeps (both squadsiege and squadheal) would bounce infinitely between room exits:

  • Creeps: squadsiege-244091, squadsiege-755626
  • Positions: E17S52(17,49) ↔ E17S53(17,0)
  • Logging "Reached end of handling() why?" every tick
  • routing.reached = true but they keep moving back and forth
  • No damage taken (hitsLost: 0)

Root Cause - The Bouncing Cycle

  1. Creep enters target room E17S53 at position (17,0) → sets routing.reached = true
  2. Creep moves back to E17S52 at border position (17,49) due to border tile instability
  3. Action function checks: "Am I in target room?" → NO
  4. Deletes routing.reached flag
  5. Routing system restarts: "You need to go to E17S53!"
  6. Creep moves forward to E17S53 again
  7. Repeat forever → infinite CPU waste

The bug was in the action functions:

// OLD BROKEN CODE
if (creep.room.name !== creep.memory.routing.targetRoom) {
  delete creep.memory.routing.reached; // ← Causes restart loop at borders
}
return creep.siege(); // ← Always executes, even when traveling

Problems:

  • No explicit handling for being IN the target room
  • Deletes routing flag when at border tiles (causes restart loop)
  • Always calls siege() regardless of room location
  • Unlike heal role which had better if/else logic

Changes (role_squadsiege.js and role_squadheal.js)

  1. Add explicit if/else logic to separate traveling vs in-target-room behavior:

    • When not in target room: handle traveling, don't execute combat actions
    • When in target room: execute combat actions
  2. Add border protection using isBorder() check:

    • Only reset routing.reached if not at border tile
    • Prevents bouncing at room edges
    • Stable transition between rooms
  3. Don't execute siege/heal when traveling:

    • Return early when not in target room
    • Only execute combat logic when actually in target room

Impact

Before Fix

  • Mineral creeps: Spamming logs every tick, wasting CPU on repeated state setting
  • Squad creeps: Bouncing at borders, never reaching attack objective, wasting spawn resources

After Fix

  • Mineral creeps: Stop cleanly when resources depleted, self-healing reaction cleanup
  • Squad creeps: Properly enter target room and begin attack, no more bouncing
  • CPU savings: Eliminated continuous routing recalculations and state resets
  • Consistent validation: All code paths use same thresholds and logic

Testing

  • Verified mineral creep stops spamming logs when resources are depleted
  • Confirmed reaction gets properly cleared when resources insufficient
  • Normal mineral operations continue working when resources available
  • Squad creeps properly transition into target room without bouncing
  • Border protection prevents routing reset at room edges
  • Both siege and heal roles fixed for consistency

Files Changed

  • src/role_mineral.js - Mineral creep validation and cleanup
  • src/role_squadsiege.js - Squad siege action logic with border protection
  • src/role_squadheal.js - Squad heal action logic with border protection

@worlddriven
Copy link
Contributor

worlddriven bot commented Nov 1, 2025

🤖 Worlddriven Status

📊 Live Status Dashboard

🗓️ Merge Date: 2025-11-04 at 15:23:33 UTC (today)
📅 Started: 2025-11-01 at 17:22:30 UTC
Speed Factor: 0.42 (58% faster due to reviews)
Positive votes: 466/799 contribution weight (coefficient: 0.58)
📈 Base Merge Time: 7 days → Current: 3 days

🎯 Want to influence when this merges?

Your review matters! As a contributor to this project, your voice helps determine the merge timeline.

How to review:

  1. Check the changes
    Files changed

  2. Leave your review
    Review changes

Your options:

  • ✅ Agree & Speed Up: Approve Approving makes this merge faster
  • ❌ Disagree & Slow Down: Request changes Requesting changes delays the merge

💡 Pro tip: The more contributors who agree, the faster this gets merged!

📊 View detailed stats on the dashboard

📋 Recent Activity

2025-11-04, 15:51:08 - Pull request merged by worlddriven ✅


This comment is automatically updated by worlddriven

@TooAngel TooAngel force-pushed the fix/mineral-infinite-loop-complete branch from e47aba8 to ee79bc3 Compare November 1, 2025 16:02
@TooAngel TooAngel changed the title fix: prevent mineral creep infinite loop on insufficient resources fix: prevent mineral and squad infinite loops Nov 1, 2025
@TooAngel TooAngel force-pushed the fix/mineral-infinite-loop-complete branch from ee79bc3 to 4ca71d2 Compare November 1, 2025 16:54
This fix addresses two critical infinite loop issues that waste CPU:

1. Mineral Creep Infinite Loop
----------------------------
Mineral creeps would infinitely loop trying to transfer resources from
the terminal when insufficient amounts (1-4 units) were available.

Changes:
- Update validation threshold from === 0 to < LAB_REACTION_AMOUNT (5)
  in both setStatePrepareReactionLab1WithResource and
  setStatePrepareReactionLab2WithResource to match room cleanup logic
- Add reaction cleanup in handleWithdrawFromSource to delete
  room.memory.reaction when resources are insufficient, preventing
  immediate re-setting of the same failed state
- Enhance validation check to also verify resource amount meets
  LAB_REACTION_AMOUNT threshold during withdrawal

2. Squad Bouncing at Room Borders
--------------------------------
Squad creeps (both siege and heal) would bounce infinitely between
room exits, logging "Reached end of handling() why?" every tick.

Root cause: When creeps reached target room border, they would:
- Enter target room -> set routing.reached = true
- Move back across border due to border tile instability
- Delete routing.reached flag (not in target room anymore)
- Routing system restarts -> creep moves forward again
- Infinite loop continues

Changes:
- Add explicit if/else logic in squadsiege.action to separate
  traveling vs in-target-room behavior
- Add border protection: only reset routing.reached if not at border
  tile (using isBorder check) to prevent bouncing
- Don't execute siege() when traveling (not in target room)
- Apply same fix to squadheal.action for consistency

Both fixes ensure consistent validation across code paths and make
the systems self-healing by clearing invalid states immediately.
@TooAngel TooAngel force-pushed the fix/mineral-infinite-loop-complete branch from 4ca71d2 to ce5141b Compare November 1, 2025 17:22
@worlddriven worlddriven bot merged commit 6143f84 into master Nov 4, 2025
2 checks passed
@worlddriven worlddriven bot deleted the fix/mineral-infinite-loop-complete branch November 4, 2025 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants