Release 0.137.15: Code Quality Through Strategic Refactoring

Introduction

Release 0.137.15 represents a significant milestone in our ongoing effort to improve code maintainability and eliminate technical debt. This release focuses on extracting hardcoded “magic numbers” from the BehaviorController into a centralized, well-documented constants module—a critical refactoring that sets the stage for more sophisticated AI behavior systems without introducing any functional changes to the bot.

Key Features

Centralized Spawn Threshold Constants

The primary achievement of this release is the extraction of spawn threshold constants from the BehaviorController into a dedicated constants module:

  • New Constants Module: Created packages/bot/src/runtime/behavior/constants.ts with a SPAWN_THRESHOLDS export that consolidates all spawn-related configuration values
  • Comprehensive Documentation: Each constant includes TSDoc comments explaining its purpose and rationale
  • Type Safety: All constants are properly typed and exported for reuse across the codebase
  • Regression Coverage: Added test suite validating constant values remain stable

Technical Details

Why This Refactoring Matters

Before this release, spawn thresholds were scattered throughout BehaviorController.ts as literal numeric values—commonly known as “magic numbers.” This pattern created several problems:

  1. Readability: Values like 0.85, 0.2, and 50 appeared without context, making code difficult to understand
  2. Maintainability: Changing thresholds required hunting through the codebase to find all occurrences
  3. Reusability: Other modules couldn’t reference these values without duplicating them
  4. Testing: Verifying threshold behavior required inspecting implementation details

Design Rationale

The refactoring follows established software engineering principles:

Single Source of Truth: All spawn-related thresholds now live in packages/bot/src/runtime/behavior/constants.ts. This module serves as the canonical reference for spawn behavior configuration, ensuring consistency across the codebase.

Self-Documenting Code: Each constant includes comprehensive TSDoc comments explaining:

  • What the value controls
  • Why this specific value was chosen
  • How it impacts bot behavior
  • When it should be adjusted

For example:

1
2
3
4
5
6
7
/**
* CPU safety margin (85%) - Maximum CPU utilization before spawn processing is throttled
*
* This threshold prevents CPU timeouts by halting expensive spawn operations when
* CPU usage approaches the limit. Set at 85% to provide a 15% safety buffer.
*/
CPU_SAFETY_MARGIN: 0.85

Backward Compatibility: All existing values remain functionally identical. The refactoring changes where values are defined, not what they are. This ensures zero behavioral changes to the production bot.

Architectural Foundation: By centralizing these constants, we’ve created infrastructure for future enhancements. The constants can now be:

  • Referenced by state machine implementations (addressing issue #1267)
  • Modified dynamically based on room conditions
  • Overridden per-room for specialized strategies
  • Validated against performance metrics

Implementation Details

The refactoring touched two primary files:

packages/bot/src/runtime/behavior/constants.ts (new file):

  • Exports SPAWN_THRESHOLDS object containing all spawn-related configuration
  • Includes constants for CPU safety margins, energy thresholds, and storage ratios
  • Fully documented with rationale for each value

packages/bot/src/runtime/behavior/BehaviorController.ts (updated):

  • Replaced all hardcoded threshold values with references to SPAWN_THRESHOLDS
  • Removed inline comments explaining threshold values (now documented at the source)
  • Improved code clarity by replacing numeric literals with named constants

Extracted Constants

The following constants were extracted and documented:

Constant Value Purpose
CPU_SAFETY_MARGIN 0.85 Maximum CPU utilization before throttling spawn operations
ENERGY_RESERVE_RATIO_LOW 0.2 Minimum energy reserve (20%) before spawn prioritization
ENERGY_RESERVE_ABSOLUTE_MIN 50 Absolute minimum energy units to maintain
STORAGE_THRESHOLD_HIGH 0.5 Storage fullness (50%) triggering increased upgrader spawning
STORAGE_THRESHOLD_LOW 0.3 Storage fullness (30%) reducing upgrader priority
ENERGY_THRESHOLD_RCL1_2 0.8 Energy capacity (80%) for dynamic upgrader scaling at RCL 1-2
ENERGY_THRESHOLD_RCL3_PLUS 0.75 Energy capacity (75%) for upgrader scaling at RCL 3+
ENERGY_THRESHOLD_HIGH 0.9 High energy (90%) triggering aggressive upgrader spawning

Testing Strategy

To ensure this refactoring doesn’t introduce regressions, we implemented a comprehensive test suite:

Constant Value Validation: Tests verify that all constants maintain their expected values. If a constant changes, tests fail and require explicit acknowledgment of the behavioral impact.

Import Verification: Tests confirm that BehaviorController correctly imports and uses constants from the new module.

Type Safety: TypeScript compilation ensures all constant references are type-safe and autocomplete-friendly.

This approach catches accidental modifications while still allowing intentional changes when needed.

Bug Fixes

No bug fixes in this release—this is a pure refactoring effort focused on code quality.

Breaking Changes

None. This release maintains complete backward compatibility. All spawn thresholds retain their original values, ensuring the bot behaves identically to version 0.137.9.

Impact

Developer Experience

This refactoring significantly improves developer experience:

  1. Easier Onboarding: New contributors can understand spawn behavior by reading constants.ts instead of reverse-engineering the logic
  2. Faster Iteration: Adjusting spawn thresholds now requires editing a single file with clear documentation
  3. Reduced Errors: Type-safe constants prevent typos and invalid values

Future Capabilities

This release unblocks issue #1267 (state machine migration) by providing:

  • Reusable Configuration: State machines can reference the same thresholds without duplication
  • Consistent Behavior: All spawn-related systems use identical threshold values
  • Testability: State machine tests can mock or override constants for specific scenarios

Code Quality Metrics

Before this refactoring:

  • 7 hardcoded numeric literals in BehaviorController.ts
  • 0 TSDoc comments explaining threshold rationale
  • Constants duplicated across potential future implementations

After this refactoring:

  • 0 hardcoded threshold values (all replaced with named constants)
  • 8 comprehensive TSDoc comments explaining each constant
  • Single source of truth enabling consistent behavior

What’s Next

This refactoring lays groundwork for several exciting developments:

State Machine Migration (Issue #1267): With centralized constants, we can now implement sophisticated state machine-based spawn logic that references the same threshold values. This enables more complex decision-making while maintaining consistency.

Dynamic Threshold Adjustment: Future releases can introduce systems that adjust thresholds based on room conditions. For example, increasing CPU_SAFETY_MARGIN during combat or lowering ENERGY_THRESHOLD_RCL1_2 when bootstrapping new rooms.

Per-Room Configuration: The constants module can evolve to support room-specific overrides, allowing specialized strategies for different room types (main bases, remote mining outposts, defensive positions).

Performance Tuning: With documented thresholds, we can systematically experiment with different values and measure their impact on bot performance metrics like energy efficiency and RCL progression speed.


This release demonstrates our commitment to code quality and technical excellence. While these changes aren’t visible in bot behavior, they significantly improve the codebase’s maintainability and set the stage for more sophisticated features in future releases.

For technical discussions or questions about this release, please refer to the CHANGELOG.md or open an issue on GitHub.