Skip to content

refactor!: remove all process-wide globals - #28

Merged
abc8747 merged 16 commits into
mainfrom
pr-remove-globals
Jul 28, 2026
Merged

refactor!: remove all process-wide globals#28
abc8747 merged 16 commits into
mainfrom
pr-remove-globals

Conversation

@abc8747

@abc8747 abc8747 commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Following #19 (comment), I was working on #24 but noticed that minisky relies on mutable process-wide state everywhere, for example:

import minisky
minisky.init()
minisky.sim.reset()
minisky.traf.cre("KL315", lat=52.0, lon=4.0, hdg=45, alt=5000, spd=250)

Here, minisky.init() mutates module-level variables such as traf, sim, scr, runner, and navdb, which are then accessed throughout the project by importing minisky. Similar global state exists in Command.cmddict, varexplorer.varlist, simulation hooks, and settings loaded at import time, resulting in implicit dependencies.

These objects have no explicit owner or lifetime, and so tests/downstream applications must reset shared state manually. Initialisation depends on import order, and multiple independent simulators cannot safely exist in one process.

This is particularly problematic for plugins, which need a specific runtime to own their configuration, commands, hooks, resources, and teardown.
This PR fully migrates toward an explicit ownership root, removing virtually all singletons and process-global mutables. While it is a significant breaking change, this change is necessary for long-term health and paves the way for proper plugin development.

To migrate:

module level global mutables removed replacement
minisky.init(...) with MiniSky(settings, scenario=...) as runtime
minisky.load_plugins() runtime.load_plugins()
minisky.traf runtime.traffic
minisky.sim runtime.simulation
minisky.scr runtime.console
minisky.runner runtime.runner
minisky.navdb runtime.navigation
minisky.stack.{stack(...),cmddict} runtime.commands.{stack(...),cmddict}
global plugin state runtime.plugins
varexplorer.varlist runtime.variables.varlist
global area definitions runtime.areas
global replacement definitions runtime.replaceables
INIT, HOLD, OP, END SimulationState.{INIT,HOLD,OP,END enums
prefer_compiled setting removed (see issue #27)

Before:

import minisky

minisky.init("scenario.scn")  # settings are hardcoded to root settings.toml
minisky.load_plugins()

After:

from minisky import MiniSky, MiniSkySettings

settings = MiniSkySettings.from_file("settings.toml")  # explicit

# multiple instances of MiniSky() can now be spawned independently without affecting the other!
with MiniSky(settings, scenario="scenario.scn") as runtime:
    # NOTE: load_plugins() may be absorbed into __enter__ in the future once issue #24 is implemented
    runtime.load_plugins()

Note that multiple outstanding issues, such as sys.path mutation during plugin loading (see issue #24), command-list concurrency bugs, stream event wakeups are inherited and remain unfixed.

abc8747 added 15 commits July 27, 2026 05:34
…, runner

- added await runtime.run();
- injected settings directly into conflict detection and resolution
- also make `Simulation` own explicit traffic, navigation, and console refs
- current command and sender state, scenario name, timestamps and commands
  are also now owned by `Minisky`.
…nav refs

- make `OpenAP` own its traffic reference
- make `Position` require explicit nav and traffic objects
- streaming is now directly driven by the owning `Simulation` after each step
- make `PluginManager` own discovered plugin records, loaded plugin state,
  plugin-created objects, hooks, timers, plugin command registration
- note that we are still using the AST hacks (#24), it will be cleaned up in
  a separate PR
- shared replaceable class catalog contains declaration metadata only, owned
  by the runtime
- significantly pruned earlier escape hatches/compat wrappers
…untime`

- replaces `minisky.{init,traf,sim,scr,runner,navdb,_current,_activate}`
- replaces `minisky.stack.{current(),Stack,stack(),readscn(),showhelp(),`
  `get_scenname(),_active_stack,Command.cmddict}`
- remove temp `minisky.core.varexplorer`, `minisky.tools.areafilter`
- replace module global fastapi router, mutable magnetic declination globals,
  import-time runtime activation, standalone area filter instance, active
  variable explorer, active command stack and registry alias.
- use `IntEnum` for `SimulationState`
…licit

- make `MiniSky` owns independent python/numpy generators, and `SEED` should
  only affect that runtime
- use `typing.Final` for magnetic declination
- suppress openap warnings for now
reverts change in fdb334f

also add some comments on deferred changes
best effort, skipping test_aero.py and test_detection.py for now
@abc8747
abc8747 force-pushed the pr-remove-globals branch from 47a0350 to 14fc7a2 Compare July 28, 2026 14:55
@abc8747
abc8747 merged commit d768c09 into main Jul 28, 2026
1 check passed
@abc8747
abc8747 deleted the pr-remove-globals branch July 28, 2026 15:49
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.

1 participant