Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
refactor(oauth): register_oauth_callback_path registry retires the last reverse import (Wave 11.I, X2)
trohitgf7a4cf8f267b
diff --git a/server/nodes/google/__init__.py b/server/nodes/google/__init__.pyindex e9e970c8..3202267a 100644--- a/server/nodes/google/__init__.py+++ b/server/nodes/google/__init__.py@@ -27,6 +27,7 @@ this module by name.from services.event_waiter import register_filter_builderfrom services.status_broadcaster import register_service_refreshfrom services.ws_handler_registry import (+ register_oauth_callback_path,register_option_loader,register_router,register_ws_handlers,@@ -45,6 +46,7 @@ from ._refresh import refresh_google_statusregister_ws_handlers(WS_HANDLERS)register_router(_router.router, name="google")+register_oauth_callback_path("google", "/api/google/callback")register_service_refresh(refresh_google_status)register_filter_builder("googleGmailReceive", build_gmail_filter)diff --git a/server/nodes/twitter/__init__.py b/server/nodes/twitter/__init__.pyindex 783134cc..e8727331 100644--- a/server/nodes/twitter/__init__.py+++ b/server/nodes/twitter/__init__.py@@ -20,7 +20,11 @@ module by name.from services.event_waiter import register_filter_builderfrom services.status_broadcaster import register_service_refresh-from services.ws_handler_registry import register_router, register_ws_handlers+from services.ws_handler_registry import (+ register_oauth_callback_path,+ register_router,+ register_ws_handlers,+)from . import _routerfrom ._filters import build_filter as build_twitter_filter@@ -34,5 +38,6 @@ from ._refresh import refresh_twitter_status# (milestone K).register_ws_handlers(WS_HANDLERS)register_router(_router.router, name="twitter")+register_oauth_callback_path("twitter", "/api/twitter/callback")register_service_refresh(refresh_twitter_status)register_filter_builder("twitterReceive", build_twitter_filter)diff --git a/server/services/oauth_utils.py b/server/services/oauth_utils.pyindex 4b0ca711..8f95ab49 100644--- a/server/services/oauth_utils.py+++ b/server/services/oauth_utils.py@@ -1,8 +1,11 @@"""Derive OAuth redirect URIs from request context -- no hardcoded ports.WebSocket handlers call get_redirect_uri(websocket, "google") to build the full-callback URL at runtime. The callback *path* comes from google_apis.json; the-base URL (scheme + host + port) comes from the connection itself.+callback URL at runtime. The callback *path* comes from+:func:`services.ws_handler_registry.get_oauth_callback_path` (plugin-registered+via ``register_oauth_callback_path("<provider>", "/api/<provider>/callback")``+from each plugin's ``__init__.py``); the base URL (scheme + host + port) comes+from the connection itself.ws://localhost:3010/ws/status -> http://localhost:3010/api/google/callbackwss://flow.zeenie.xyz/ws/status -> https://flow.zeenie.xyz/api/google/callback@@ -11,7 +14,7 @@ base URL (scheme + host + port) comes from the connection itself.from urllib.parse import urlparse-from nodes.google._oauth import get_callback_paths+from services.ws_handler_registry import get_oauth_callback_pathdef get_base_url(connection) -> str:@@ -31,15 +34,16 @@ def get_base_url(connection) -> str:def get_redirect_uri(connection, provider: str) -> str:- """Build full OAuth redirect URI from request context + JSON config path.+ """Build full OAuth redirect URI from request context + plugin-registered path.Args:connection: Starlette ``WebSocket`` or ``Request`` (anything with ``base_url``).- provider: ``"google"`` or ``"twitter"``.+ provider: Lowercase plugin id (``"google"``, ``"twitter"``, ...).Returns:Full redirect URI, e.g. ``http://localhost:3010/api/google/callback``.+ Falls back to ``/api/<provider>/callback`` if the plugin hasn't+ registered an explicit path (the default for the existing 8+ plugins)."""- paths = get_callback_paths()- path = paths.get(provider, f"/api/{provider}/callback")+ path = get_oauth_callback_path(provider)return get_base_url(connection) + pathdiff --git a/server/services/plugin/registry.py b/server/services/plugin/registry.pyindex 6c6d2cab..d4969c83 100644--- a/server/services/plugin/registry.py+++ b/server/services/plugin/registry.py@@ -78,9 +78,25 @@ class IdempotentRegistry(Generic[K, V]):self._on_register = on_registerdef register(self, key: K, value: V) -> None:- """Add ``key -> value``. Idempotent on identity; raises on conflict."""+ """Add ``key -> value``. Idempotent on equality; raises on conflict.++ Equality (``!=``) instead of identity (``is not``) so the+ registry stays idempotent across:++ * String / int / dataclass value types where Python does not+ guarantee interning (e.g. two ``"/api/google/callback"``+ literals from separate module loads).+ * ``importlib.reload(module)`` — reload constructs fresh+ function / class objects whose identity differs from the+ originals, but they're functionally equivalent and should+ re-register cleanly. The self-containment test suite relies+ on this.++ Genuinely conflicting registrations (different values for the+ same key) still raise ``ValueError`` at import time.+ """existing = self._items.get(key)- if existing is not None and existing is not value:+ if existing is not None and existing != value:raise ValueError(f"{self._name}: {key!r} is already registered by "f"{_qual(existing)}; refusing to overwrite with {_qual(value)}"diff --git a/server/services/ws_handler_registry.py b/server/services/ws_handler_registry.pyindex dcc4fae0..d2ab7bd7 100644--- a/server/services/ws_handler_registry.py+++ b/server/services/ws_handler_registry.py@@ -1,6 +1,6 @@"""Plugin self-registration registries.-Three sibling concerns share this file because they're the same pattern+Four sibling concerns share this file because they're the same pattern(idempotent dict + collision check) for the same audience (plugin``__init__.py`` modules wiring themselves into the framework):@@ -20,9 +20,13 @@ Three sibling concerns share this file because they're the same pattern:func:`dispatch_load_options` (here in this module) and the matchingWS handler in ``routers/websocket.py``.+4. **OAuth callback paths** — ``register_oauth_callback_path(provider,+ path)`` so :func:`services.oauth_utils.get_redirect_uri` can derive+ the full redirect URI at runtime without a cross-folder import.+No hardcoded plugin names anywhere in the central router or main.py.-Adding a new plugin's WS / HTTP / option-loader surface is one-registration call inside that plugin's package.+Adding a new plugin's WS / HTTP / option-loader / oauth-callback+surface is one registration call inside that plugin's package."""from __future__ import annotations@@ -44,6 +48,9 @@ _ROUTER_REGISTRY: IdempotentRegistry[str, APIRouter] = IdempotentRegistry("route_OPTION_LOADER_REGISTRY: IdempotentRegistry[str, LoadOptionsFn] = IdempotentRegistry("option_loader")+_OAUTH_CALLBACK_PATHS: IdempotentRegistry[str, str] = IdempotentRegistry(+ "oauth_callback_path"+)# ---- WebSocket handlers --------------------------------------------------@@ -142,3 +149,32 @@ def list_load_options_methods() -> List[str]:prefetches this once on boot so it knows which ``loadOptionsMethod``values are wired."""return list_registered_option_methods()+++# ---- OAuth callback paths -----------------------------------------------++def register_oauth_callback_path(provider: str, path: str) -> None:+ """Publish a plugin's OAuth callback path (``/api/<provider>/callback``).++ Same idempotency contract as the other registries: the same path+ for the same provider is a no-op; a different path for an existing+ provider raises ``ValueError``. ``services.oauth_utils.get_redirect_uri``+ looks up via this registry so the OAuth callback URL is plugin-+ registered, not cross-imported from ``nodes/<plugin>/_oauth.py``.+ """+ _OAUTH_CALLBACK_PATHS.register(provider, path)+++def get_oauth_callback_path(provider: str) -> str:+ """Look up the registered callback path or fall back to a default+ (`/api/<provider>/callback`). The fallback matches the convention+ every existing plugin uses."""+ registered = _OAUTH_CALLBACK_PATHS.get(provider)+ if registered is not None:+ return registered+ return f"/api/{provider}/callback"+++def list_registered_oauth_providers() -> List[str]:+ """For diagnostics / startup logging."""+ return sorted(_OAUTH_CALLBACK_PATHS.keys())diff --git a/server/tests/credentials/test_oauth_utils.py b/server/tests/credentials/test_oauth_utils.pyindex e5c0ba99..e3f2d986 100644--- a/server/tests/credentials/test_oauth_utils.py+++ b/server/tests/credentials/test_oauth_utils.py@@ -52,38 +52,42 @@ class TestGetBaseUrl:class TestGetRedirectUri:- @patch("services.oauth_utils.get_callback_paths")- def test_google_dev_localhost(self, mock_paths):- mock_paths.return_value = {- "google": "/api/google/callback",- "twitter": "/api/twitter/callback",- }+ """Wave 11.I, X2: callback paths come from the plugin-registered+ `register_oauth_callback_path` registry in+ `services.ws_handler_registry`, not from a JSON config helper in+ `nodes/google/_oauth.py`. Tests patch the registry lookup."""++ @patch("services.oauth_utils.get_oauth_callback_path")+ def test_google_dev_localhost(self, mock_lookup):+ mock_lookup.return_value = "/api/google/callback"uri = get_redirect_uri(_conn("ws://localhost:3010/ws/status"), "google")assert uri == "http://localhost:3010/api/google/callback"+ mock_lookup.assert_called_once_with("google")- @patch("services.oauth_utils.get_callback_paths")- def test_twitter_prod_https(self, mock_paths):- mock_paths.return_value = {- "google": "/api/google/callback",- "twitter": "/api/twitter/callback",- }+ @patch("services.oauth_utils.get_oauth_callback_path")+ def test_twitter_prod_https(self, mock_lookup):+ mock_lookup.return_value = "/api/twitter/callback"uri = get_redirect_uri(_conn("wss://flow.zeenie.xyz/ws/status"), "twitter")assert uri == "https://flow.zeenie.xyz/api/twitter/callback"+ mock_lookup.assert_called_once_with("twitter")- @patch("services.oauth_utils.get_callback_paths")- def test_unknown_provider_falls_back_to_default_path(self, mock_paths):- mock_paths.return_value = {} # no entries at all+ def test_unknown_provider_falls_back_to_default_path(self):+ """`get_oauth_callback_path` (the real one, not mocked) falls+ back to ``/api/<provider>/callback`` for any unregistered+ provider. Test the integrated path end-to-end."""uri = get_redirect_uri(_conn("http://localhost:3010/anything"), "newprovider")assert uri == "http://localhost:3010/api/newprovider/callback"- def test_paths_loaded_from_config_not_hardcoded(self):- """Smoke test: the real config must expose google + twitter paths."""- from nodes.google._oauth import get_callback_paths+ def test_paths_registered_by_plugins(self):+ """Smoke test: the real plugin packages must register the+ canonical google + twitter callback paths from their+ ``__init__.py`` (verified by ensuring import side-effect)."""+ import nodes.google # noqa: F401 -- side effect registers callback path+ import nodes.twitter # noqa: F401- paths = get_callback_paths()- assert "google" in paths- assert "twitter" in paths- assert paths["google"].startswith("/api/")- assert paths["twitter"].startswith("/api/")+ from services.ws_handler_registry import get_oauth_callback_path++ assert get_oauth_callback_path("google") == "/api/google/callback"+ assert get_oauth_callback_path("twitter") == "/api/twitter/callback"