Skip to content

The agent that grows with you

fix(connectors): Connectors page requests name the desktop surface

Siddharth Balyan4cc7bb56e4d3
diff --git a/tests/tui_gateway/test_connector_account_surface.py b/tests/tui_gateway/test_connector_account_surface.py
new file mode 100644
index 00000000000..b2a15e4c41c
--- /dev/null
+++ b/tests/tui_gateway/test_connector_account_surface.py
@@ -0,0 +1,52 @@
+"""Account-owned connector RPCs come only from the desktop Connectors page, so their gateway requests
+name the ``desktop`` surface even on a backend started without ``HERMES_DESKTOP`` (a URL, SSH-less or
+cloud backend the app attached to)."""
+
+import pytest
+
+from tools.connectors import live
+from tools.managed_gateway_identity import user_agent
+from tui_gateway import server
+
+
+def remote_backend(monkeypatch):
+ monkeypatch.delenv("HERMES_DESKTOP", raising=False)
+ monkeypatch.setattr("tools.connectors.connectors_available", lambda: True)
+ monkeypatch.setattr("tools.connectors.ensure_guest_identity", lambda: None)
+ monkeypatch.setattr("tools.connectors.managed.ensure_guest_identity", lambda: None)
+ live.reset_for_tests()
+ yield
+ live.reset_for_tests()
+
+
+def _dispatch(method, params):
+ return server.handle_request({"jsonrpc": "2.0", "id": 1, "method": method, "params": params})
+
+
+def test_account_reads_and_connects_name_the_desktop(remote_backend, monkeypatch):
+ from tools.connectors.gateway.errors import GatewayUnavailable
+
+ agents = []
+
+ def read_tools(slug, *, client, refresh):
+ agents.append(user_agent())
+ raise GatewayUnavailable("gone", code="connector_not_found")
+
+ class Client:
+ def __init__(self, **_):
+ pass
+
+ def connections(self, names, **_):
+ agents.append(user_agent())
+ return {"results": [{"connector": n, "status": "active"} for n in names]}
+
+ monkeypatch.setattr("tools.connectors.portal.client.PortalConnectorClient.require_authentication", lambda self: None)
+ monkeypatch.setattr("tools.connectors.portal.tools_cache.read_tools", read_tools)
+ monkeypatch.setattr("tools.connectors.gateway.client.ConnectorClient", Client)
+
+ assert _dispatch("connectors.tools", {"slug": "gmail", "refresh": False})["error"]["code"] == 4041
+ connected = _dispatch("connectors.connect", {"owner": {"type": "account"}, "connectors": ["gmail"]})
+ assert "result" in connected, connected
+ assert user_agent().split(" (")[1].startswith("cli;") # nothing leaks out of the request
+ assert [agent.split(" (")[1].split(";")[0] for agent in agents] == ["desktop", "desktop"]
diff --git a/tui_gateway/methods_connectors.py b/tui_gateway/methods_connectors.py
index 6d5c0d051ed..15b181ca606 100644
--- a/tui_gateway/methods_connectors.py
+++ b/tui_gateway/methods_connectors.py
@@ -315,14 +315,24 @@ def _account_home(request):
return str(home) if home else None
+def _desktop_page_vars():
+ """Bind the session vars of a sessionless request from the desktop Connectors page. Only that
+ page sends account-owned and account RPCs, so the surface comes from the request: the backend's
+ ``HERMES_DESKTOP`` is absent on a URL or cloud backend and would name the caller ``tui``."""
+ from gateway.session_context import set_session_vars
+
+ tokens = set_session_vars(source="desktop", profile=_current_profile_name(), cron_session="")
+ try:
+ yield
+ finally:
+ _clear_session_context(tokens)
+
+
@contextlib.contextmanager
def _account_scope(request):
- with _session_profile_runtime_scope({"profile_home": _account_home(request)}):
- tokens = _set_session_context("")
- try:
- yield
- finally:
- _clear_session_context(tokens)
+ with _session_profile_runtime_scope({"profile_home": _account_home(request)}), _desktop_page_vars():
+ yield
def _operation_for_request(rid, request, session):
diff --git a/tui_gateway/methods_connectors_account.py b/tui_gateway/methods_connectors_account.py
index 0c2e0b9675e..3a7d86a75c3 100644
--- a/tui_gateway/methods_connectors_account.py
+++ b/tui_gateway/methods_connectors_account.py
@@ -21,22 +21,23 @@ def _account_method(params_model=None, *, invalid="", invalid_reason=ConnectorEr
from pydantic import ValidationError
from tools.connectors.gateway.errors import GatewayAuthError
- if gate_error := _account_gate_closed(rid) or _account_identity_error(rid):
- return gate_error
- try:
- request = params if params_model is None else params_model.model_validate(params)
- except ValidationError:
- return _connector_rpc_error(rid, 4000, invalid_reason, invalid)
- try:
- return fn(rid, request)
- except GatewayAuthError as exc:
- return _connector_auth_error(rid, exc)
- except Exception as exc:
- if getattr(exc, "code", None) == "org_required":
- return _connector_rpc_error(
- rid, 4090, ConnectorErrorReason.org_required, "Select an organization to manage connector rules."
- )
- return _connector_rpc_error(rid, 5034, unavailable, unavailable_message)
+ with _desktop_page_vars():
+ if gate_error := _account_gate_closed(rid) or _account_identity_error(rid):
+ return gate_error
+ try:
+ request = params if params_model is None else params_model.model_validate(params)
+ except ValidationError:
+ return _connector_rpc_error(rid, 4000, invalid_reason, invalid)
+ try:
+ return fn(rid, request)
+ except GatewayAuthError as exc:
+ return _connector_auth_error(rid, exc)
+ except Exception as exc:
+ if getattr(exc, "code", None) == "org_required":
+ return _connector_rpc_error(
+ rid, 4090, ConnectorErrorReason.org_required, "Select an organization to manage connector rules."
+ )
+ return _connector_rpc_error(rid, 5034, unavailable, unavailable_message)
return handler