Skip to content

Self-improving AI Employees for everyone

refactor(container): service_factories registry closes MapsService + AndroidService top-level imports (Wave 12 C4 #C — final)

trohitg73c5f08b381f
diff --git a/server/core/container.py b/server/core/container.py
index a2746c37..39cee4c2 100644
--- a/server/core/container.py
+++ b/server/core/container.py
@@ -12,6 +12,8 @@ def _clog(msg):
print(f" container: {msg}", flush=True)
+from typing import Any
+
from dependency_injector import containers, providers
from core.config import Settings
@@ -22,12 +24,10 @@ from core.credentials_database import CredentialsDatabase
_clog("core imports done")
from services.ai import AIService
_clog("AIService imported")
-from nodes.location._service import MapsService
from services.workflow import WorkflowService
_clog("WorkflowService imported")
from services.auth import AuthService
from services.text import TextService
-from nodes.android._dispatcher import AndroidService
from services.user_auth import UserAuthService
from services.compaction import init_compaction_service
_clog("all service imports done")
@@ -40,6 +40,48 @@ def _create_temporal_client(server_address: str, namespace: str):
return TemporalClientWrapper(server_address=server_address, namespace=namespace)
+# Wave 12 C4 sub-piece C: plugin-owned service factories.
+# ``MapsService`` (nodes/location) and ``AndroidService`` (nodes/android)
+# used to be imported here at module scope so providers.Factory could
+# reference the classes directly. That made the framework DI container
+# know about specific plugins' implementation classes — direct
+# violation of the "framework knows no plugin names" rule.
+# Now the plugins self-register their service classes via
+# ``services.plugin.service_factories.register_service_factory``;
+# the container's provider declarations reference the helper functions
+# below, which look up the registered factory at instantiation time
+# (first ``container.foo()`` call). Plugin auto-discovery on startup
+# populates the registry before any service is first resolved, so
+# import-ordering is not a constraint.
+
+
+def _build_registered_service(name: str, **kwargs: Any) -> Any:
+ """Look up a plugin-registered service factory and instantiate it.
+
+ Wrapped behind a thin function so the DI container can declare
+ providers without importing the concrete service classes.
+ """
+ from services.plugin.service_factories import get_service_factory
+
+ factory = get_service_factory(name)
+ if factory is None:
+ raise RuntimeError(
+ f"DI container: service factory {name!r} is not registered. "
+ "The owning plugin failed to import (or hasn't shipped a "
+ "register_service_factory call from its __init__.py). Check "
+ "the plugin folder's __init__.py for the registration."
+ )
+ return factory(**kwargs)
+
+
+def _build_maps_service(auth_service, settings):
+ return _build_registered_service("maps", auth_service=auth_service, settings=settings)
+
+
+def _build_android_service():
+ return _build_registered_service("android")
+
+
class Container(containers.DeclarativeContainer):
"""Application dependency injection container."""
@@ -109,7 +151,7 @@ class Container(containers.DeclarativeContainer):
)
maps_service = providers.Factory(
- MapsService,
+ _build_maps_service,
auth_service=auth_service,
settings=settings
)
@@ -119,7 +161,7 @@ class Container(containers.DeclarativeContainer):
)
android_service = providers.Factory(
- AndroidService
+ _build_android_service
)
compaction_service = providers.Singleton(
diff --git a/server/nodes/android/__init__.py b/server/nodes/android/__init__.py
index 309951e6..cf096e5c 100644
--- a/server/nodes/android/__init__.py
+++ b/server/nodes/android/__init__.py
@@ -9,6 +9,7 @@ wifi_automation, ...) live alongside as siblings.
Wiring is body-free; both registrations are idempotent.
"""
+from services.plugin.service_factories import register_service_factory
from services.plugin.shutdown_hooks import register_shutdown_hook
from services.status_broadcaster import register_service_refresh
from services.ws_handler_registry import (
@@ -18,6 +19,7 @@ from services.ws_handler_registry import (
)
from . import _router
+from ._dispatcher import AndroidService
from ._events import broadcast_android_status # noqa: F401 — re-export
from ._handlers import WS_HANDLERS
from ._option_loaders import load_service_actions
@@ -45,3 +47,9 @@ async def _shutdown_android_relay() -> None:
register_shutdown_hook("android_relay", _shutdown_android_relay)
+
+# Wave 12 C4 sub-piece C: publish AndroidService as the "android"
+# service factory so core/container.py can look it up at runtime
+# without a cross-plugin top-level import. See
+# services/plugin/service_factories.py.
+register_service_factory("android", AndroidService)
diff --git a/server/nodes/location/__init__.py b/server/nodes/location/__init__.py
index f851d9ea..d06c26ea 100644
--- a/server/nodes/location/__init__.py
+++ b/server/nodes/location/__init__.py
@@ -1 +1,16 @@
-"""Plugins for the 'location' palette group. See ../__init__.py for the package layout."""
+"""Plugins for the 'location' palette group. See ../__init__.py for the package layout.
+
+Self-registration on import:
+ - Wave 12 C4 sub-piece C: ``MapsService`` is published as the
+ ``"maps"`` service factory via
+ ``services.plugin.service_factories.register_service_factory``.
+ The DI container's ``maps_service`` provider looks up the factory
+ at instantiation time, so the framework no longer carries a
+ top-level ``from nodes.location._service import MapsService``.
+"""
+
+from services.plugin.service_factories import register_service_factory
+
+from ._service import MapsService
+
+register_service_factory("maps", MapsService)
diff --git a/server/services/plugin/service_factories.py b/server/services/plugin/service_factories.py
new file mode 100644
index 00000000..790916de
--- /dev/null
+++ b/server/services/plugin/service_factories.py
@@ -0,0 +1,97 @@
+"""Plugin-owned registry of service-factory callables for the DI container.
+
+Closes the two remaining cross-plugin top-level imports in
+``core/container.py``:
+
+ - ``from nodes.location._service import MapsService`` (line 25)
+ - ``from nodes.android._dispatcher import AndroidService`` (line 30)
+
+Both made the framework's DI container code know about specific
+plugins' implementation classes — direct violation of the
+"framework knows no plugin names" rule.
+
+Pattern (matches the established Wave-11.I plugin-self-registration
+shape; same as ``register_filter_builder`` /
+``register_poll_coroutine_factory`` / ``register_ws_handlers`` /
+``register_canary_trigger_type`` / ``register_social_send_handler`` /
+``register_shutdown_hook``):
+
+1. Plugin's ``__init__.py`` imports its service class from ``_service.py``
+ (intra-plugin, fine) and calls
+ ``register_service_factory("<name>", ServiceClass)``.
+2. The container's provider declaration references a lazy lookup
+ function — :func:`get_service_factory` — instead of the concrete
+ class. ``providers.Factory`` / ``providers.Singleton`` calls the
+ wrapper function with its declared kwargs; the wrapper looks up
+ the registered factory and instantiates.
+3. Lookup happens at **instantiation time** (first ``container.foo()``
+ call), not at class-definition time, so plugin import ordering
+ isn't a constraint — plugin auto-discovery on startup populates
+ the registry before any service is first resolved.
+
+Factory signature
+-----------------
+
+The "factory" is typically the class itself (calling it constructs
+the instance). Any zero-or-more-keyword-arg callable that returns
+the service instance is acceptable.
+
+::
+
+ factory(**kwargs) -> ServiceInstance
+"""
+
+from __future__ import annotations
+
+from typing import Any, Callable, Optional
+
+from services.plugin.registry import IdempotentRegistry
+
+
+ServiceFactory = Callable[..., Any]
+
+
+_REGISTRY: IdempotentRegistry[str, ServiceFactory] = IdempotentRegistry(
+ "service_factory"
+)
+
+
+def register_service_factory(name: str, factory: ServiceFactory) -> None:
+ """Publish a service factory for the DI container.
+
+ Idempotent on re-import (same callable / class for the same name
+ is a no-op). A different factory for an existing name raises
+ ``ValueError`` to surface plugin namespace collisions at import time.
+
+ Args:
+ name: Lower-case service identifier (``"maps"``, ``"android"``,
+ ...). The container's provider declaration references this
+ name via :func:`get_service_factory`.
+ factory: The service class (or any callable taking the
+ provider's declared kwargs) that constructs the service
+ instance.
+ """
+ _REGISTRY.register(name, factory)
+
+
+def get_service_factory(name: str) -> Optional[ServiceFactory]:
+ """Return the factory for ``name``, or ``None`` if unregistered.
+
+ Container providers wrap this in a lazy thunk that surfaces a
+ clear "service not registered" error if a required plugin failed
+ to load, instead of silently producing ``None``.
+ """
+ return _REGISTRY.get(name)
+
+
+def registered_service_names() -> frozenset[str]:
+ """Return an immutable snapshot of registered service identifiers."""
+ return frozenset(_REGISTRY.keys())
+
+
+__all__ = [
+ "register_service_factory",
+ "get_service_factory",
+ "registered_service_names",
+ "ServiceFactory",
+]
diff --git a/server/tests/test_service_factories.py b/server/tests/test_service_factories.py
new file mode 100644
index 00000000..92f55016
--- /dev/null
+++ b/server/tests/test_service_factories.py
@@ -0,0 +1,165 @@
+"""Wave 12 C4 sub-piece C: service-factories registry contract + invariant.
+
+Locks three layers:
+
+1. **Registry surface**: idempotent registration, retrieval, snapshot.
+
+2. **Architectural invariant on core/container.py**: the DI container
+ must NOT carry top-level imports of plugin service classes. The
+ ``from nodes.location._service import MapsService`` /
+ ``from nodes.android._dispatcher import AndroidService`` lines that
+ lived at container.py:25 / :30 are gone — replaced by
+ ``_build_registered_service("<name>", ...)`` lazy thunks that
+ resolve through the registry at provider-instantiation time.
+
+3. **Plugin self-registration smoke**: importing the location and
+ android plugins populates the registry.
+"""
+
+from __future__ import annotations
+
+import re
+import sys
+import types
+from unittest.mock import MagicMock
+
+import pytest
+
+
+if "machina" not in sys.modules:
+ _machina = types.ModuleType("machina")
+ _machina.__path__ = []
+ sys.modules["machina"] = _machina
+ _machina_tcp = types.ModuleType("machina.tcp")
+ _machina_tcp.probe_tcp_port = MagicMock(return_value=False)
+ sys.modules["machina.tcp"] = _machina_tcp
+
+
+def fresh_registry(monkeypatch):
+ """Reset registry's backing dict for test isolation."""
+ from services.plugin import service_factories as sf
+
+ fresh = type(sf._REGISTRY)(sf._REGISTRY._name) # type: ignore[attr-defined]
+ monkeypatch.setattr(sf, "_REGISTRY", fresh)
+ return sf
+
+
+class TestRegistryContract:
+ def test_unregistered_name_returns_none(self, fresh_registry):
+ assert fresh_registry.get_service_factory("maps") is None
+
+ def test_register_then_get_returns_callable(self, fresh_registry):
+ class FakeService:
+ def __init__(self, *, auth_service=None, settings=None):
+ self.auth_service = auth_service
+ self.settings = settings
+
+ fresh_registry.register_service_factory("maps", FakeService)
+ factory = fresh_registry.get_service_factory("maps")
+
+ assert factory is FakeService
+ # Calling it constructs the instance with kwargs.
+ instance = factory(auth_service="A", settings="S")
+ assert instance.auth_service == "A"
+ assert instance.settings == "S"
+
+ def test_idempotent_same_callable(self, fresh_registry):
+ class FakeService:
+ pass
+
+ fresh_registry.register_service_factory("maps", FakeService)
+ fresh_registry.register_service_factory("maps", FakeService)
+ assert fresh_registry.registered_service_names() == frozenset({"maps"})
+
+ def test_conflicting_register_raises(self, fresh_registry):
+ class A:
+ pass
+
+ class B:
+ pass
+
+ fresh_registry.register_service_factory("maps", A)
+ with pytest.raises(ValueError, match="already registered"):
+ fresh_registry.register_service_factory("maps", B)
+
+ def test_multiple_services_coexist(self, fresh_registry):
+ class A:
+ pass
+
+ class B:
+ pass
+
+ fresh_registry.register_service_factory("maps", A)
+ fresh_registry.register_service_factory("android", B)
+ assert fresh_registry.registered_service_names() == frozenset({"maps", "android"})
+
+
+class TestContainerHasNoCrossPluginServiceImports:
+ """The DI container must NOT carry plugin-implementation imports
+ at module scope. Reads container.py as source to bypass its own
+ runtime import requirements.
+ """
+
+ _CONTAINER_PY = "core/container.py"
+
+ _FORBIDDEN_IMPORTS = (
+ ("nodes.location._service", "MapsService"),
+ ("nodes.android._dispatcher", "AndroidService"),
+ )
+
+ def _read_container_source(self) -> str:
+ import pathlib
+
+ path = pathlib.Path(__file__).resolve().parent.parent / self._CONTAINER_PY
+ assert path.exists(), f"Could not locate container.py at {path}"
+ return path.read_text(encoding="utf-8")
+
+ def test_container_drops_legacy_service_imports(self):
+ src = self._read_container_source()
+
+ for module_path, symbol in self._FORBIDDEN_IMPORTS:
+ pattern = re.compile(
+ rf"from\s+{re.escape(module_path)}\s+import\s+{re.escape(symbol)}",
+ re.MULTILINE,
+ )
+ assert pattern.search(src) is None, (
+ f"core/container.py still imports {symbol} from {module_path}. "
+ "Service classes must be looked up via "
+ "services.plugin.service_factories.get_service_factory at "
+ "instantiation time. Plugin should call "
+ "register_service_factory(...) from its own __init__.py."
+ )
+
+ def test_container_uses_lazy_factory_resolver(self):
+ src = self._read_container_source()
+ assert "_build_registered_service" in src, (
+ "core/container.py must declare _build_registered_service "
+ "(or an equivalent lazy lookup) so providers resolve plugin "
+ "factories via the registry instead of importing the classes."
+ )
+
+
+class TestPluginSelfRegistration:
+ """Smoke: importing each plugin populates the registry."""
+
+ @pytest.mark.parametrize("plugin_module,expected_name", [
+ ("nodes.location", "maps"),
+ ("nodes.android", "android"),
+ ])
+ def test_plugin_import_registers_its_factory(self, plugin_module, expected_name):
+ from services.plugin import service_factories as sf
+
+ try:
+ __import__(plugin_module)
+ except ImportError as exc: # pragma: no cover
+ pytest.xfail(f"plugin module {plugin_module} not importable: {exc}")
+
+ factory = sf.get_service_factory(expected_name)
+ assert factory is not None, (
+ f"Importing {plugin_module} should call "
+ f"register_service_factory({expected_name!r}, ...). "
+ f"Current registered names: {sf.registered_service_names()}"
+ )
+ # Sanity: it's a class (or at least callable).
+ assert callable(factory)