The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(connectors): a guessed connector slug fails naming the listed one
alt-glitch1402b60b906e
diff --git a/tests/tools/test_connectors_managed.py b/tests/tools/test_connectors_managed.pyindex 9265e4840bf..811cd634837 100644--- a/tests/tools/test_connectors_managed.py+++ b/tests/tools/test_connectors_managed.py@@ -239,6 +239,40 @@ def test_mint_failure_detail_survives_and_only_an_unlisted_catalog_name_is_misroassert out["targets"][0]["detail"] == "gateway hiccup"+def test_a_guessed_slug_fails_naming_the_listed_one_and_status_names_it_too():+ # The gateway lists granola as granola_mcp and refuses "granola" as a policy block, with the+ # reason in its instruction.+ class Fake(GatewayFake):+ def list_connectors(self, *, timeout=None):+ self.lists += 1+ return [{"connector": s, "enabled": True, "connected": False} for s in ("gmail", "granola_mcp")]++ def connections(self, connectors, **kw):+ out = super().connections(connectors, **kw)+ for row in out["results"]:+ row["instruction"] = "This connector is not allowed by your organization's policy."+ return out++ gw = Fake(mint_status="failed")+ card = _desktop_callback()+ with patch("tools.connectors.operation.OPERATION_DEADLINE_SECONDS", 30.0), \+ patch("tools.connectors.managed.catalog_names", return_value=set()):+ out = _run({"action": "connect", "connectors": ["granola"]}, gw, callback=card, tick=0.01)+ assert card.seen == [] and out["settled_by"] == "all_resolved"+ assert out["targets"][0]["detail"] == "'granola' is not a connector slug; use granola_mcp"++ gw = Fake(mint_status="failed")+ with patch("tools.connectors.operation.OPERATION_DEADLINE_SECONDS", 0.05), \+ patch("tools.connectors.managed.catalog_names", return_value=set()):+ out = _run({"action": "connect", "connectors": ["gmail"]}, gw, callback=_desktop_callback(), tick=0.01)+ assert out["targets"][0]["detail"] == "This connector is not allowed by your organization's policy."++ with patch("tools.connectors.managed.portal_accounts", return_value=[]):+ out = _run({"action": "status", "connectors": ["granola"]}, Fake())+ assert out["connectors"] == []+ assert out["unknown"] == {"granola": "'granola' is not a connector slug; use granola_mcp"}++# ---------------------------------------------------------------------------# reconnect = repair# ---------------------------------------------------------------------------diff --git a/tools/connectors/managed.py b/tools/connectors/managed.pyindex 2b33837d1a4..10b6f97bbf0 100644--- a/tools/connectors/managed.py+++ b/tools/connectors/managed.py@@ -100,6 +100,9 @@ def mint(client: Any, operation: ConnectionOperation, names: list[str], *, reinicontinuestatus = str(entry.get("status") or "")detail = str(entry.get("status_reason") or "")+ if status not in ("active", "initiated"):+ # A refusal's reason comes in its instruction ("not allowed by your organization's policy").+ detail = detail or str(entry.get("instruction") or "")connection_id = entry.get("connection_id")if status == "active":operation.transition(name, TargetState.initiated, actor)@@ -214,12 +217,43 @@ def _mark_misrouted(operation: ConnectionOperation) -> None:operation.settle(SettleReason.all_resolved)+def _unknown_slug(name: str, known: set[str]) -> str:+ close = sorted(slug for slug in known if name in slug or slug in name)+ if close:+ return f"'{name}' is not a connector slug; use {' or '.join(close)}"+ return f"'{name}' is not a connector slug; manage_connections status lists them"+++def _mark_unknown(client: Any, operation: ConnectionOperation) -> None:+ """The gateway refuses a slug it does not list as a policy block. The model guesses a slug from the+ app's name (granola for granola_mcp), so the row names the listed slug instead."""+ unminted = [t for t in operation.targets if t.state == TargetState.failed and not t.connection_id]+ if operation.settled or not unminted:+ return+ catalog = catalog_names() # a local MCP name already carries its misrouted detail+ unminted = [t for t in unminted if t.name not in catalog]+ if not unminted:+ return+ try:+ known = {str(i.get("connector", "")).lower() for i in client.list_connectors()}+ except ToolGatewayError as exc:+ logger.debug("connector list for an unminted slug failed: %s", exc)+ return+ unknown = [t for t in unminted if t.name not in known]+ for target in unknown:+ operation.refresh(target.name, connect_url=None, actor=Actor.backend_watcher,+ detail=_unknown_slug(target.name, known))+ if unknown and len(unknown) == len(operation.targets):+ operation.settle(SettleReason.all_resolved)++def _prepare(client: Any, action: str, force: bool) -> Callable[[ConnectionOperation], None]:def prepare(operation: ConnectionOperation) -> None:names = [t.name for t in operation.targets]if action == "connect":mint(client, operation, names, reinitiate=False, actor=Actor.backend_watcher)_mark_misrouted(operation)+ _mark_unknown(client, operation)return# A named account is checked on its own row, read before force so a label resolves to its# account either way: the connector-wide flag reports any account.@@ -229,6 +263,7 @@ def _prepare(client: Any, action: str, force: bool) -> Callable[[ConnectionOperaaccount = next((s["connection_id"] for s in (named or {}).values() if s.get("connection_id")), None)mint(client, operation, names, reinitiate=True, actor=Actor.backend_watcher, connection_id=account)_mark_misrouted(operation)+ _mark_unknown(client, operation)returnstatus = named or _status_by_slug(client)repair = []@@ -247,6 +282,7 @@ def _prepare(client: Any, action: str, force: bool) -> Callable[[ConnectionOperaaccount = next((status[n].get("connection_id") for n in repair if status.get(n, {}).get("connection_id")), None)mint(client, operation, repair, reinitiate=True, actor=Actor.backend_watcher, connection_id=account)_mark_misrouted(operation)+ _mark_unknown(client, operation)return prepare@@ -300,6 +336,8 @@ def _no_card_result(client: Any, action: str, targets: list[HostedTarget], forcedef _status_result(client: Any, connectors: list[str]) -> str:items = client.list_connectors()+ known = {str(i.get("connector", "")).lower() for i in items}+ unknown = {name: _unknown_slug(name, known) for name in connectors if name not in known}if connectors:wanted = set(connectors)items = [i for i in items if str(i.get("connector", "")).lower() in wanted]@@ -321,7 +359,8 @@ def _status_result(client: Any, connectors: list[str]) -> str:"connected=false means calls to that connector will return CONNECTION_REQUIRED. ""Use action 'connect' to start an authorization. 'accounts' lists each account by its name ""(alias) and the vendor's label; address an unnamed account by its label. A retired account was "- "replaced by a reconnect and is kept only for the record.")}, ensure_ascii=False)+ "replaced by a reconnect and is kept only for the record."), **({"unknown": unknown} if unknown else {})},+ ensure_ascii=False)def _display_result(client: Any, suggested: list[str], *, session_id: Optional[str], tool_call_id: Optional[str],