7.11 Stateful collaborators are built by the composition root and passed in
Rule: §7.11 in the code standards; the root, the orchestrators and the existing cases are in B2.
Why
A collaborator that holds state and serves more than the script that builds it (a tracker, a debugger hook, a monitor, a transport) has one correct instance. When the composition root builds it, one file shows the whole object graph and the order it starts and stops in. When a user builds or defaults its own copy, there are two instances of the same state, nothing keeps them in step, and they disagree without an error.
The collaborator reaches its users in one of three ways:
- an
_initargument; - a
register(...)parameter; - a
set_*/clear_*pair on the user’s instance that the root calls at start-up and undoes in teardown (8.5). A static setter that fills astatic varis a global holder instead (7.13).
The one orchestrator that alone uses a child may build it (7.2): that child serves nobody else. A user with nothing injected reports that state (an empty result, a note, a false) instead of inventing a substitute.
Good
The composer builds the debug-session tracker once, hands the same instance to the debugger bridge that feeds it, and passes both to the command registrars (addons/godot_mcp_toolkit/core/plugin_composer.gd:65-69, compose):
var session_tracker := DebugSessionTracker.new(EditorInterface)
var debug_bridge := DebugBridge.new(session_tracker)
plugin.add_debugger_plugin(debug_bridge)
BuiltinCommandRegistration.register_all(registry, server, debug_bridge, session_tracker)
The playtest registrar receives the tracker as a typed register(...) parameter (addons/godot_mcp_toolkit/commands/playtest/playtest_commands.gd:18-20), and the composer’s teardown undoes the registration through PlaytestCommands.clear_session_tracker() (addons/godot_mcp_toolkit/core/plugin_composer.gd:338). The reader behind that call keeps the tracker in a static var, one of the holders B2 lists under 7.13. Copy the composer’s wiring above, not that storage.
Bad
A user that defaults its own copy. The log reader starts with a tracker it builds itself, and builds a fresh one again when the root clears the injected one (addons/godot_mcp_toolkit/commands/playtest_log_reader.gd:37, :66):
static var _session_tracker: DebugSessionTracker = DebugSessionTracker.new()
static func clear_session_tracker() -> void:
_session_tracker = DebugSessionTracker.new()
_session_tracker_injected = false
The stand-in is deliberate (game.start can mark a playtest before any tracker is injected), so it is one of the existing cases B2 lists rather than a pattern to copy. A new collaborator takes the injected instance and, with none, reports that it has none.
Source
- Seemann, composition root: https://blog.ploeh.dk/2011/07/28/CompositionRoot/
- Fowler, inversion of control containers and the dependency injection pattern (constructor and setter injection; configuration separate from use): https://martinfowler.com/articles/injection.html
- Godot, scene organization (a parent supplies a child’s dependencies): https://docs.godotengine.org/en/stable/tutorials/best_practices/scene_organization.html