Engine
Core simulation engine and supporting components.
Simulation Engine
Simulation engine for the Babylon game loop.
ADR032: Materialist Causality System Order
The step() function is the core of Phase 2. It takes a WorldState and SimulationConfig and returns a new WorldState representing one tick of simulation time.
The step function is: - Pure: No side effects, no mutation of inputs - Deterministic: Same inputs always produce same outputs - Transparent: Order of operations encodes historical materialism
Turn Order (materialist causality - base before superstructure): 1. Vitality - Biological cost + death (dead entities don’t work) 2. Territory - Land state updates (land conditions affect production) 3. Production - Value creation (value must exist before extraction) 4. Solidarity - Organization (affects bargaining power) 5. Imperial Rent - Value extraction (landlord eats after harvest) 6. Decomposition - LA decomposes on super-wage crisis (Terminal Crisis) 7. Control Ratio - Guard:prisoner ratio + terminal decision (Terminal Crisis) 8. Metabolism - Environmental degradation (ecological residue of production) 9. Survival - Risk assessment (P(S|A), P(S|R) from material state) 10. Struggle - Action/Revolt (agency responds to survival odds) 11. Consciousness - Ideological drift (ideology responds to material) 12. Contradiction - Tension aggregation (final systemic accounting)
Phase 2.1: Refactored to modular System architecture. Phase 4a: Refactored to use ServiceContainer for dependency injection. ADR032: Reordered systems for materialist causality.
- class babylon.engine.simulation_engine.SimulationEngine(systems)[source]
Bases:
objectModular engine that advances the simulation by iterating through Systems.
The engine holds a list of systems and executes them in sequence. Order encodes materialist causality (ADR032): 1. Vitality (death check) 2. Territory (land state) 3. Production (value creation) 4. Solidarity (organization) 5. Imperial Rent (extraction) 6. Decomposition (LA crisis) 7. Control Ratio (terminal decision) 8. Metabolism (ecology) 9. Survival (risk assessment) 10. Struggle (agency) 11. Consciousness (ideology) 12. Contradiction (tension) 13. ContradictionField (field computation) - Feature 002 14. FieldDerivative (derivatives + principal) - Feature 002 15. EdgeTransition (predicates + state machine) - Feature 002
- run_tick(graph, services, context)[source]
Execute all systems in order for one tick.
All logs emitted during this method are automatically tagged with tick number and a unique correlation_id (UUID) for tracing.
- Parameters:
graph – NetworkX graph (mutated in place by systems)
services – ServiceContainer with config, formulas, event_bus, database, metrics
context – TickContext or dict passed to all systems
Spec 008: Logs within run_tick() include tick and correlation_id.
- babylon.engine.simulation_engine.step(state, config, persistent_context=None, defines=None, calculator_overrides=None)[source]
Advance simulation by one tick using the modular engine.
This is the heart of Phase 2. It transforms a WorldState through one tick of simulated time by applying the MLM-TW formulas.
- Parameters:
state (
WorldState) – Current world state (immutable)config (
SimulationConfig) – Simulation configuration with formula coefficientspersistent_context (
dict[str,Any] |None) – Optional context dict that persists across ticks. Used by systems that need to track state between ticks (e.g., ConsciousnessSystem’s previous_wages for bifurcation mechanic).defines (
GameDefines|None) – Optional custom GameDefines. If None, loads from default defines.yaml location. Use this for scenario-specific calibration.calculator_overrides (
dict[str,Any] |None) – Optional dict of calculator instances to inject into ServiceContainer (e.g., melt_calculator, tensor_registry).
- Return type:
- Returns:
New WorldState at tick + 1
- Order encodes historical materialism:
Economic base (value extraction)
Consciousness (responds to material conditions)
Survival calculus (probability updates)
Contradictions (tension from all above)
Event capture (log significant changes)
Simulation
Simulation facade class for running multi-tick simulations.
This module provides a Simulation class that wraps the ServiceContainer and step() function, providing a convenient API for: - Running simulations over multiple ticks - Preserving history of all WorldState snapshots - Maintaining a persistent ServiceContainer across ticks - Observer pattern for AI narrative generation (Sprint 3.1)
Observer Pattern Integration (Sprint 3.1): - Observers are registered via constructor or add_observer() - Notifications occur AFTER state reconstruction (per design decision) - Observer errors are logged but don’t halt simulation (ADR003) - Lifecycle hooks: on_simulation_start, on_tick, on_simulation_end
MVP Simulation Engine (001-mvp-sim-engine): - Implements SimulationState and SimulationControl protocols - Tracks territory profit_rate for GUI visualization - Provides get_snapshot(), get_territory_state(), reset() methods - Deterministic profit_rate decay toward territory-specific equilibrium
- class babylon.engine.simulation.Simulation(initial_state, config, observers=None, defines=None, tensor_registry=None, calculator_overrides=None)[source]
Bases:
objectFacade class for running multi-tick simulations with history preservation.
The Simulation class provides a stateful wrapper around the pure step() function, managing: - Current WorldState - History of all previous states - Persistent ServiceContainer for dependency injection - Observer notifications for AI/narrative components (Sprint 3.1)
Example
>>> from babylon.engine.factories import create_proletariat, create_bourgeoisie >>> from babylon.models import WorldState, SimulationConfig, Relationship, EdgeType >>> >>> worker = create_proletariat() >>> owner = create_bourgeoisie() >>> exploitation = Relationship( ... source_id=worker.id, target_id=owner.id, ... edge_type=EdgeType.EXPLOITATION ... ) >>> state = WorldState(entities={worker.id: worker, owner.id: owner}, ... relationships=[exploitation]) >>> config = SimulationConfig() >>> >>> sim = Simulation(state, config) >>> sim.run(100) >>> print(f"Worker wealth after 100 ticks: {sim.current_state.entities[worker.id].wealth}")
- With observers:
>>> from babylon.ai import NarrativeDirector >>> director = NarrativeDirector() >>> sim = Simulation(state, config, observers=[director]) >>> sim.run(10) >>> sim.end() # Triggers on_simulation_end
- Parameters:
initial_state (WorldState)
config (SimulationConfig)
observers (list[SimulationObserver] | None)
defines (GameDefines | None)
tensor_registry (TensorRegistry | None)
- __init__(initial_state, config, observers=None, defines=None, tensor_registry=None, calculator_overrides=None)[source]
Initialize simulation with initial state and configuration.
- Parameters:
initial_state (
WorldState) – Starting WorldState at tick 0config (
SimulationConfig) – Simulation configuration with formula coefficientsobservers (
list[SimulationObserver] |None) – Optional list of SimulationObserver instances to notifydefines (
GameDefines|None) – Optional custom GameDefines for scenario-specific coefficients. If None, loads from default defines.yaml location.tensor_registry (
TensorRegistry|None) – Optional TensorRegistry for cached tensor data access. If None, tensor data is not available. If provided, it should be pre-hydrated with the relevant counties and years.calculator_overrides (
dict[str,Any] |None) – Optional dict of calculator instances to inject into ServiceContainer on each tick (Feature 020).
- Return type:
None
- classmethod from_sqlite(fips_codes, year=2022, observers=None, defines=None, years=None)[source]
Create simulation initialized from SQLite reference database.
This is the main entry point for the MVP simulation engine. It hydrates territories from the reference database with profit_rate computed from QCEW/BEA data.
- Parameters:
fips_codes (
list[str]) – List of 5-digit FIPS codes for counties to simulate. Example: [“26163”, “26125”] for Wayne and Oakland counties.year (
int) – Data year for QCEW/BEA data (default 2022).observers (
list[SimulationObserver] |None) – Optional list of SimulationObserver instances.defines (
GameDefines|None) – Optional custom GameDefines for scenario-specific coefficients.years (
Sequence[int] |None) – Optional sequence of years for multi-year time series. When provided, tensor data is hydrated for all specified years and the economics calculator factory is wired automatically.
- Return type:
- Returns:
Initialized Simulation with territories hydrated from database.
- Raises:
ValueError – If fips_codes is empty, contains duplicates that reduce to fewer unique codes, or any county is not found in database.
Example
>>> sim = Simulation.from_sqlite( ... fips_codes=["26163", "26125"], # Detroit metro ... year=2022 ... ) >>> snapshot = sim.get_snapshot() >>> wayne = snapshot.territories["26163"] >>> print(f"Wayne County profit rate: {wayne.profit_rate}")
See also
plan.md#Hydration Flow
quickstart.md
- property config: SimulationConfig
Return the simulation configuration.
- property defines: GameDefines
Return the game defines.
- property services: ServiceContainer
Return the persistent ServiceContainer.
- property tensor_registry: TensorRegistry | None
Return the TensorRegistry for cached economic data access.
- Returns:
TensorRegistry if initialized, None otherwise.
- property current_state: WorldState
Return the current WorldState.
- property observers: list[SimulationObserver]
Return copy of registered observers.
Returns a copy to preserve encapsulation - modifying the returned list does not affect the internal observer list.
- Returns:
A copy of the list of registered observers.
- add_observer(observer)[source]
Register an observer for simulation notifications.
Observers added after simulation has started will not receive on_simulation_start, but will receive on_tick and on_simulation_end notifications.
- Parameters:
observer (
SimulationObserver) – Observer implementing SimulationObserver protocol.- Return type:
- remove_observer(observer)[source]
Remove an observer. No-op if observer not present.
- Parameters:
observer (
SimulationObserver) – Observer to remove from notifications.- Return type:
- register_observer(callback)[source]
Register a GUI callback for tick notifications.
Implements SimulationControl protocol.
- Thread Safety:
Callbacks receive a frozen SimulationSnapshot, not a live reference to mutable simulation state. The ProtocolObserverAdapter creates the snapshot BEFORE iterating callbacks, ensuring: - All callbacks see the same consistent state - GUI code cannot race with engine mutations - Callback processing time does not affect snapshot consistency
Callbacks are invoked in registration order. Duplicate registration is idempotent (callback invoked once per tick).
- Parameters:
callback (
Callable[[int,SimulationSnapshot],None]) – Function to call after each tick. Signature: (tick: int, snapshot: SimulationSnapshot) -> None- Return type:
- unregister_observer(callback)[source]
Remove a previously registered GUI callback.
Implements SimulationControl protocol.
If the callback was not registered, this is a no-op (no error raised).
- Parameters:
callback (
Callable[[int,SimulationSnapshot],None]) – The callback function to remove.- Return type:
- step(n=1)[source]
Advance simulation by n ticks.
Implements SimulationControl protocol’s step(n) method.
Applies the step() function to transform the current state, records the new state in history, updates current_state, and notifies registered observers.
On first step, observers receive on_simulation_start before on_tick.
The persistent context is passed to step() to maintain state across ticks (e.g., previous_wages for bifurcation mechanic).
- Parameters:
n (
int) – Number of ticks to advance. Must be positive. Defaults to 1 for backward compatibility.- Return type:
- Returns:
The new WorldState after all ticks complete.
- Raises:
ValueError – If n <= 0.
- run(ticks)[source]
Run simulation for N ticks.
- Parameters:
ticks (
int) – Number of ticks to advance the simulation- Return type:
- Returns:
The final WorldState after all ticks complete.
- Raises:
ValueError – If ticks is negative or zero
- get_history()[source]
Return all WorldState snapshots from initial to current.
The history includes: - Index 0: Initial state (tick 0) - Index N: State after N steps (tick N)
- Return type:
- Returns:
List of WorldState snapshots in chronological order.
- get_time_series()[source]
Extract time series records from completed simulation.
Reads accumulated tick dynamics snapshots stored in persistent_context by the step() function at each year boundary. Each snapshot contains county-level economic state computed by TickDynamicsSystem.
- Returns:
year, fips, class distribution shares (bourgeoisie_share, petit_bourgeoisie_share, la_share, proletariat_share, lumpen_share), profit_rate, phi_hour, throughput_position, data_source, and Vol I/II/III fields: capital_stock, median_wage, employment (Vol I); circuit_money, circuit_productive, circuit_commodity, liquidity_ratio, realization_crisis (Vol II); surplus_total, interest_payments, ground_rent, profit_of_enterprise, financialization_share, overaccumulation, profit_squeeze (Vol III).
- Return type:
List of dicts with keys
Example
>>> sim = Simulation.from_sqlite(["26163"], year=2022, years=[2022]) >>> sim.run(52) >>> ts = sim.get_time_series() >>> for record in ts: ... print(f"{record['year']} {record['fips']}: LA={record['la_share']:.2f}")
- update_state(new_state)[source]
Update the current state mid-simulation.
This allows modifying the simulation state (e.g., changing relationships) while preserving the persistent context across ticks. Useful for testing scenarios like wage cuts where the previous_wages context must be preserved.
- Parameters:
new_state (
WorldState) – New WorldState to use as current state. The tick should match the expected continuation tick.- Return type:
Note
This does NOT add the new state to history - history reflects actual simulation progression, not manual state updates.
- end()[source]
Signal simulation end and notify observers.
Calls on_simulation_end on all registered observers with the current (final) state.
No-op if simulation has not started (no step() calls made). Can be called multiple times, but only the first call after step() will notify observers.
- Return type:
- get_outcome()[source]
Return current game outcome from EndgameDetector if present.
Searches registered observers for an EndgameDetector and returns its current outcome. If no EndgameDetector is registered, returns IN_PROGRESS.
- Return type:
- Returns:
GameOutcome enum value indicating current game state.
Example
>>> from babylon.engine.observers import EndgameDetector >>> detector = EndgameDetector() >>> sim = Simulation(state, config, observers=[detector]) >>> sim.get_outcome() <GameOutcome.IN_PROGRESS: 'in_progress'>
- get_current_tick()[source]
Return the current tick number.
Implements SimulationState protocol.
- Return type:
- Returns:
Non-negative integer representing the current simulation tick. Tick 0 is the initial state before any step() calls.
- get_snapshot()[source]
Return a complete snapshot of the current simulation state.
Implements SimulationState protocol.
The snapshot is immutable - modifying the returned object does not affect the simulation. The tensor_registry reference allows cached tensor data access without database queries.
- Return type:
- Returns:
SimulationSnapshot containing all state at the current tick.
- get_territory_state(territory_id)[source]
Return the state of a specific territory.
Implements SimulationState protocol.
- Parameters:
territory_id (
str) – Unique identifier for the territory (FIPS code for counties).- Return type:
- Returns:
TerritoryState if the territory exists, None otherwise.
- get_hexes_for_territory(territory_id)[source]
Return the H3 indices claimed by a territory.
Implements SimulationState protocol.
- get_node_by_spatial_index(h3_index)[source]
Return the territory that claims a specific H3 hex (T027).
Implements SimulationState protocol.
This method bridges the spatial representation (H3 hexes used by map visualization like pydeck) to the simulation’s territory model.
- Parameters:
h3_index (
str) – H3 cell index (15-character lowercase hex string).- Return type:
- Returns:
TerritoryState if a territory claims this hex, None otherwise.
- Raises:
ValueError – If h3_index is not a valid H3 cell index.
- reset()[source]
Reset simulation to initial state (tick 0).
Implements SimulationControl protocol.
Restores the simulation to its state immediately after initialization: - tick = 0 - All territory states reset to initial values - profit_rate returns to initial computed values - WorldState reset to initial state - History cleared
Implementation note: reset() restores CACHED initial state.
- Return type:
- run_until_endgame(max_ticks=1000)[source]
Run simulation until an endgame condition is met or max_ticks reached.
This method runs the simulation step by step, checking after each tick whether the EndgameDetector has detected a game ending condition. It terminates early if an endgame is reached.
- Parameters:
max_ticks (
int) – Maximum number of ticks to run before returning. Defaults to 1000 to prevent infinite loops.- Returns:
final_state: The WorldState when simulation stopped
outcome: GameOutcome indicating why simulation stopped (may be IN_PROGRESS if max_ticks reached without endgame)
- Return type:
Tuple of (final_state, outcome)
- Raises:
ValueError – If max_ticks is negative.
Example
>>> from babylon.engine.observers import EndgameDetector >>> detector = EndgameDetector() >>> sim = Simulation(state, config, observers=[detector]) >>> final_state, outcome = sim.run_until_endgame(max_ticks=100) >>> if outcome == GameOutcome.REVOLUTIONARY_VICTORY: ... print("The workers have won!")
Services
Service container for dependency injection.
This module provides a ServiceContainer dataclass that aggregates all dependencies needed by the simulation engine, enabling clean injection for testing and configuration.
Sprint 3: Central Committee (Dependency Injection) Paradox Refactor: Added GameDefines for centralized coefficients. Spec 008: Added metrics field for dependency-injected telemetry.
- class babylon.engine.services.ServiceContainer(config, database, event_bus, formulas, defines, metrics, field_registry=None, reserve_army_data_source=None, dispossession_data_source=None, productivity_data_source=None, melt_calculator=None, basket_calculator=None, gamma_calculator=None, capital_calculator=None, throughput_calculator=None, transition_engine=None, tensor_registry=None, community_hypergraph=None, turnover_profile_source=None, inventory_data_source=None, depreciation_data_source=None, hex_grid=None, persistence=None, tracer=None, distribution_calculator=None, interest_calculator=None, credit_cycle_detector=None, fictitious_capital_calculator=None, rent_calculator=None, housing_calculator=None, counter_tendency_calculator=None, value_basis_converter=None, financial_crisis_assessor=None, z1_source=None, housing_data_source=None)[source]
Bases:
objectContainer for all simulation services.
Aggregates the six core services needed by the simulation, plus optional economics calculator services for tick dynamics (Feature 017):
- Core:
config: Immutable simulation parameters
database: Database connection for persistence
event_bus: Publish/subscribe communication
formulas: Registry of mathematical formulas
defines: Centralized game coefficients (Paradox Refactor)
metrics: Telemetry collector for observability (Spec 008)
- Field Topology (Feature 002, optional for backward compatibility):
field_registry: Contradiction field computation registry
- Economics (Feature 017, all optional for backward compatibility):
melt_calculator: National MELT computation (Feature 013)
basket_calculator: Basket visibility computation (Feature 013)
gamma_calculator: Reproductive visibility computation (Feature 015)
capital_calculator: Capital stock computation (Feature 012)
throughput_calculator: Throughput position computation (Feature 014)
transition_engine: Class transition engine (Feature 016)
tensor_registry: Cached economic tensor data (Feature 011)
Example
>>> container = ServiceContainer.create() >>> rent = container.formulas.get("imperial_rent") >>> container.event_bus.publish(Event(...)) >>> with container.database.session() as session: ... # do database work >>> container.database.close() >>> default_org = container.defines.DEFAULT_ORGANIZATION >>> container.metrics.increment("ticks_processed")
- Parameters:
config (SimulationConfig)
database (DatabaseConnection)
event_bus (EventBus)
formulas (FormulaRegistry)
defines (GameDefines)
metrics (MetricsCollectorProtocol)
field_registry (Any)
reserve_army_data_source (Any)
dispossession_data_source (Any)
productivity_data_source (Any)
melt_calculator (Any)
basket_calculator (Any)
gamma_calculator (Any)
capital_calculator (Any)
throughput_calculator (Any)
transition_engine (Any)
tensor_registry (Any)
community_hypergraph (Any)
turnover_profile_source (Any)
inventory_data_source (Any)
depreciation_data_source (Any)
hex_grid (Any)
persistence (Any)
tracer (Any)
distribution_calculator (Any)
interest_calculator (Any)
credit_cycle_detector (Any)
fictitious_capital_calculator (Any)
rent_calculator (Any)
housing_calculator (Any)
counter_tendency_calculator (Any)
value_basis_converter (Any)
financial_crisis_assessor (Any)
z1_source (Any)
housing_data_source (Any)
-
config:
SimulationConfig
-
database:
DatabaseConnection
-
formulas:
FormulaRegistry
-
defines:
GameDefines
-
metrics:
MetricsCollectorProtocol
- classmethod create(config=None, defines=None, metrics=None, *, hex_grid=None, persistence=None, tracer=None, reserve_army_data_source=None, dispossession_data_source=None, productivity_data_source=None, field_registry=None, melt_calculator=None, basket_calculator=None, gamma_calculator=None, capital_calculator=None, throughput_calculator=None, transition_engine=None, tensor_registry=None, community_hypergraph=None, turnover_profile_source=None, inventory_data_source=None, depreciation_data_source=None, distribution_calculator=None, interest_calculator=None, credit_cycle_detector=None, fictitious_capital_calculator=None, rent_calculator=None, housing_calculator=None, counter_tendency_calculator=None, value_basis_converter=None, financial_crisis_assessor=None, z1_source=None, housing_data_source=None)[source]
Factory method to create a fully-initialized container.
Creates all services with sensible defaults. Uses in-memory SQLite for database isolation in tests.
- Parameters:
config (
SimulationConfig|None) – Optional custom config. If None, uses default SimulationConfig.defines (
GameDefines|None) – Optional custom defines. If None, uses default GameDefines.metrics (
MetricsCollectorProtocol|None) – Optional custom metrics collector. If None, creates a new MetricsCollector instance. Pass a mock for testing.field_registry (
Any) – Optional FieldRegistry for contradiction fields (Feature 002).melt_calculator (
Any) – Optional MELTCalculator (Feature 013).basket_calculator (
Any) – Optional BasketVisibilityCalculator (Feature 013).gamma_calculator (
Any) – Optional GammaIIICalculator (Feature 015).capital_calculator (
Any) – Optional CapitalStockCalculator (Feature 012).throughput_calculator (
Any) – Optional ThroughputCalculator (Feature 014).transition_engine (
Any) – Optional ClassTransitionEngine (Feature 016).tensor_registry (
Any) – Optional TensorRegistry for cached tensor data (Feature 011).community_hypergraph (
Any) – Optional XGI Hypergraph for community membership (Feature 022).hex_grid (Any)
persistence (Any)
tracer (Any)
reserve_army_data_source (Any)
dispossession_data_source (Any)
productivity_data_source (Any)
turnover_profile_source (Any)
inventory_data_source (Any)
depreciation_data_source (Any)
distribution_calculator (Any)
interest_calculator (Any)
credit_cycle_detector (Any)
fictitious_capital_calculator (Any)
rent_calculator (Any)
housing_calculator (Any)
counter_tendency_calculator (Any)
value_basis_converter (Any)
financial_crisis_assessor (Any)
z1_source (Any)
housing_data_source (Any)
- Return type:
- Returns:
ServiceContainer with all services initialized
- __init__(config, database, event_bus, formulas, defines, metrics, field_registry=None, reserve_army_data_source=None, dispossession_data_source=None, productivity_data_source=None, melt_calculator=None, basket_calculator=None, gamma_calculator=None, capital_calculator=None, throughput_calculator=None, transition_engine=None, tensor_registry=None, community_hypergraph=None, turnover_profile_source=None, inventory_data_source=None, depreciation_data_source=None, hex_grid=None, persistence=None, tracer=None, distribution_calculator=None, interest_calculator=None, credit_cycle_detector=None, fictitious_capital_calculator=None, rent_calculator=None, housing_calculator=None, counter_tendency_calculator=None, value_basis_converter=None, financial_crisis_assessor=None, z1_source=None, housing_data_source=None)
- Parameters:
config (SimulationConfig)
database (DatabaseConnection)
event_bus (EventBus)
formulas (FormulaRegistry)
defines (GameDefines)
metrics (MetricsCollectorProtocol)
field_registry (Any)
reserve_army_data_source (Any)
dispossession_data_source (Any)
productivity_data_source (Any)
melt_calculator (Any)
basket_calculator (Any)
gamma_calculator (Any)
capital_calculator (Any)
throughput_calculator (Any)
transition_engine (Any)
tensor_registry (Any)
community_hypergraph (Any)
turnover_profile_source (Any)
inventory_data_source (Any)
depreciation_data_source (Any)
hex_grid (Any)
persistence (Any)
tracer (Any)
distribution_calculator (Any)
interest_calculator (Any)
credit_cycle_detector (Any)
fictitious_capital_calculator (Any)
rent_calculator (Any)
housing_calculator (Any)
counter_tendency_calculator (Any)
value_basis_converter (Any)
financial_crisis_assessor (Any)
z1_source (Any)
housing_data_source (Any)
- Return type:
None
Event Bus
Event system for decoupled communication in the simulation.
This module provides a publish/subscribe event bus that enables loose coupling between simulation components. Events are immutable data objects that carry information about state changes.
Sprint 3: Central Committee (Dependency Injection) Epoch 1→2 Bridge: Added EventInterceptor pattern for adversarial mechanics.
- class babylon.engine.event_bus.Event(type, tick, payload, timestamp=<factory>)[source]
Bases:
objectImmutable event representing a simulation occurrence.
Events are frozen dataclasses to ensure they cannot be modified after creation, maintaining integrity of the event history.
- type
Event type identifier (e.g., “tick”, “rupture”, “synthesis”)
- tick
Simulation tick when the event occurred
- payload
Event-specific data dictionary
- timestamp
Wall-clock time when event was created
- class babylon.engine.event_bus.EventBus[source]
Bases:
objectPublish/subscribe event bus for simulation components.
The EventBus enables decoupled communication between systems. Components can subscribe to specific event types and will be notified when events of that type are published.
All published events are stored in history for replay/debugging.
Epoch 1→2 Bridge: Supports optional interceptor chain for adversarial mechanics. If no interceptors are registered, events flow through with zero overhead (backwards compatible).
The interceptor chain processes events before emission: - Interceptors are sorted by priority (higher runs first) - Each interceptor can ALLOW, BLOCK, or MODIFY the event - If blocked, the event is logged and not emitted - If modified, the modified event continues through the chain
Example
>>> bus = EventBus() >>> def on_tick(event: Event) -> None: ... print(f"Tick {event.tick}: {event.payload}") >>> bus.subscribe("tick", on_tick) >>> bus.publish(Event(type="tick", tick=1, payload={"value": 42})) Tick 1: {'value': 42}
- register_interceptor(interceptor)[source]
Register an interceptor to process events before emission.
Interceptors are sorted by priority (higher first) each time an event is published. Multiple interceptors with the same priority execute in registration order.
- Parameters:
interceptor (
EventInterceptor) – The interceptor to register.- Return type:
Example
>>> from babylon.engine.interceptor import EventInterceptor >>> bus = EventBus() >>> bus.register_interceptor(my_security_interceptor)
- unregister_interceptor(interceptor)[source]
Remove an interceptor from the chain.
- Parameters:
interceptor (
EventInterceptor) – The interceptor to remove.- Raises:
ValueError – If the interceptor is not registered.
- Return type:
- publish(event, context=None)[source]
Publish an event to all subscribed handlers.
If interceptors are registered, the event passes through the interceptor chain first. If any interceptor blocks the event, it is logged to the blocked events audit channel and not emitted.
The event is stored in history only if it passes all interceptors.
- Parameters:
event (
Event) – The event to publish.context (
WorldContext|None) – Optional world context for interceptors. Required for Epoch 2 adversarial mechanics.
- Return type:
Formula Registry
Formula registry for hot-swappable mathematical functions.
This module provides a FormulaRegistry class that stores named callables, enabling runtime replacement of formulas for testing and modding.
Sprint 3: Central Committee (Dependency Injection)
- class babylon.engine.formula_registry.FormulaRegistry[source]
Bases:
objectRegistry for named mathematical formulas.
Provides a central lookup for all simulation formulas, enabling: - Hot-swapping formulas for testing with mocks - Modding support for custom formula implementations - Centralized formula management
Example
>>> registry = FormulaRegistry.default() >>> la = registry.get("labor_aristocracy_ratio") >>> result = la(core_wages=120.0, value_produced=100.0)
- classmethod default()[source]
Create a registry pre-populated with all standard formulas.
Registers formulas from babylon.formulas: - labor_aristocracy_ratio - is_labor_aristocracy - consciousness_drift - acquiescence_probability - revolution_probability - crossover_threshold - loss_aversion - exchange_ratio - exploitation_rate - value_transfer - prebisch_singer
- Return type:
- Returns:
FormulaRegistry with all standard formulas registered
Factories
Factory functions for creating simulation entities.
These functions provide convenient ways to create SocialClass entities with sensible defaults for class simulation. Each factory encapsulates the defaults appropriate for a specific social class.
Factories support the **kwargs pattern for extensibility while
maintaining type safety through Pydantic validation.
Sprint 3.4.3 (George Jackson Refactor): ideology parameter accepts both float (legacy) and IdeologicalProfile (new format). Float values are automatically converted to IdeologicalProfile by the SocialClass validator.
- babylon.engine.factories.create_proletariat(id=PERIPHERY_WORKER_ID, name='Proletariat', wealth=0.5, ideology=-0.3, organization=0.1, repression_faced=0.5, subsistence_threshold=0.3, p_acquiescence=0.0, p_revolution=0.0, description='Exploited working class', effective_wealth=0.0, unearned_increment=0.0, ppp_multiplier=1.0)[source]
Create a proletariat (exploited class) social class.
The proletariat is defined by: - PERIPHERY_PROLETARIAT role (exploited in the world system) - Low default wealth (0.5) - Slightly revolutionary ideology (-0.3) - Low organization (0.1 = 10%) - Moderate repression faced (0.5)
- Parameters:
id (
str) – Unique identifier matching ^C[0-9]{3}$ pattern (default: “C001”)name (
str) – Human-readable name (default: “Proletariat”)wealth (
float) – Economic resources (default: 0.5)ideology (
float|IdeologicalProfile) – Ideological position, -1=revolutionary to +1=reactionary (default: -0.3)organization (
float) – Collective cohesion (default: 0.1)repression_faced (
float) – State violence level (default: 0.5)subsistence_threshold (
float) – Minimum wealth for survival (default: 0.3)p_acquiescence (
float) – P(S|A) - survival through acquiescence (default: 0.0, calculated by engine)p_revolution (
float) – P(S|R) - survival through revolution (default: 0.0, calculated by engine)description (
str) – Optional description (default: “Exploited working class”)effective_wealth (
float) – PPP-adjusted wealth (default: 0.0, calculated by engine)unearned_increment (
float) – PPP bonus (default: 0.0, calculated by engine)ppp_multiplier (
float) – PPP multiplier applied to wages (default: 1.0)
- Return type:
- Returns:
SocialClass configured as proletariat
Example
>>> worker = create_proletariat() >>> worker.role <SocialRole.PERIPHERY_PROLETARIAT: 'periphery_proletariat'> >>> worker.wealth 0.5
- babylon.engine.factories.create_bourgeoisie(id=COMPRADOR_ID, name='Bourgeoisie', wealth=10.0, ideology=0.8, organization=0.7, repression_faced=0.1, subsistence_threshold=0.1, p_acquiescence=0.0, p_revolution=0.0, description='Capital-owning exploiter class', effective_wealth=0.0, unearned_increment=0.0, ppp_multiplier=1.0)[source]
Create a bourgeoisie (exploiter class) social class.
The bourgeoisie is defined by: - CORE_BOURGEOISIE role (exploiter in the world system) - High default wealth (10.0) - Reactionary ideology (0.8) - High organization (0.7 = 70%) - Low repression faced (0.1 - protected by state)
- Parameters:
id (
str) – Unique identifier matching ^C[0-9]{3}$ pattern (default: “C002”)name (
str) – Human-readable name (default: “Bourgeoisie”)wealth (
float) – Economic resources (default: 10.0)ideology (
float|IdeologicalProfile) – Ideological position, -1=revolutionary to +1=reactionary (default: 0.8)organization (
float) – Collective cohesion (default: 0.7)repression_faced (
float) – State violence level (default: 0.1)subsistence_threshold (
float) – Minimum wealth for survival (default: 0.1)p_acquiescence (
float) – P(S|A) - survival through acquiescence (default: 0.0, calculated by engine)p_revolution (
float) – P(S|R) - survival through revolution (default: 0.0, calculated by engine)description (
str) – Optional description (default: “Capital-owning exploiter class”)effective_wealth (
float) – PPP-adjusted wealth (default: 0.0, calculated by engine)unearned_increment (
float) – PPP bonus (default: 0.0, calculated by engine)ppp_multiplier (
float) – PPP multiplier applied to wages (default: 1.0)
- Return type:
- Returns:
SocialClass configured as bourgeoisie
Example
>>> owner = create_bourgeoisie() >>> owner.role <SocialRole.CORE_BOURGEOISIE: 'core_bourgeoisie'> >>> owner.wealth 10.0