Skip to content

The agent that grows with you

feat(connectors): execute sends one call per request and reads status codes

Siddharth Balyand535df3a1df3
diff --git a/tests/tools/test_connector_accounts.py b/tests/tools/test_connector_accounts.py
index ea95bbe2d4d..1c5c4b90fa6 100644
--- a/tests/tools/test_connector_accounts.py
+++ b/tests/tools/test_connector_accounts.py
@@ -39,6 +39,10 @@ class _ExecuteTransport:
"""Answers every execute with one result per call, each naming the account it ran under."""
def request(self, method, url, *, headers=None, json=None, timeout=None):
+ if "call" in json:
+ call = json["call"]
+ return _Response({"result": {"connector": call["connector"], "tool": call["tool"], "data": {"ok": True},
+ "account": call["arguments"].get("connector_alias")}})
results = [{"index": i, "connector": c["connector"], "tool": c["tool"], "data": {"ok": True},
"account": c["arguments"].get("connector_alias")} for i, c in enumerate(json["tools"])]
return _Response({"results": results, "successCount": len(results), "errorCount": 0,
diff --git a/tests/tools/test_connector_bridge_wiring.py b/tests/tools/test_connector_bridge_wiring.py
index 18b77b5be62..6ab2dd35697 100644
--- a/tests/tools/test_connector_bridge_wiring.py
+++ b/tests/tools/test_connector_bridge_wiring.py
@@ -595,26 +595,16 @@ class _FakeResponse:
class _RecordingTransport:
- """Records every outgoing request; answers each with `data` echoes."""
+ """Records every outgoing request; answers each single call with a `data` echo."""
def __init__(self):
self.requests = []
def request(self, method, url, *, headers=None, json=None, timeout=None):
self.requests.append({"method": method, "url": url, "json": json})
- tools = (json or {}).get("tools") or []
- results = [
- {"index": i, "connector": t.get("connector"), "tool": t.get("tool"), "data": "ok"}
- for i, t in enumerate(tools)
- ]
+ call = (json or {}).get("call") or {}
return _FakeResponse(
- {
- "results": results,
- "successCount": len(results),
- "errorCount": 0,
- "totalCount": len(results),
- }
- )
+ {"result": {"connector": call.get("connector"), "tool": call.get("tool"), "data": "ok"}})
def _recording_client_factory(transport):
@@ -628,8 +618,7 @@ def _recording_client_factory(transport):
def _sent_tools(transport):
- assert len(transport.requests) == 1
- return transport.requests[0]["json"]["tools"]
+ return [request["json"]["call"] for request in transport.requests]
def test_hook_rewrite_and_restored_vendor_slug_reach_the_gateway_request_body(monkeypatch):
diff --git a/tests/tools/test_connectors_gateway_client.py b/tests/tools/test_connectors_gateway_client.py
index 5cd43431cf2..18220a0eed3 100644
--- a/tests/tools/test_connectors_gateway_client.py
+++ b/tests/tools/test_connectors_gateway_client.py
@@ -89,31 +89,25 @@ def planned(calls=PLAN_CALLS):
# ---------------------------------------------------------------------------
-def test_execute_sends_one_request_with_camelcase_body_and_key():
+def single_result(connector, tool, data):
+ return {"result": {"connector": connector, "tool": tool, "data": data}}
+
+
+def test_execute_sends_one_single_call_request_per_call_with_its_own_key():
transport = FakeTransport(
- FakeResponse(
- 200,
- execute_envelope(
- [
- {"index": 0, "connector": "gmail", "tool": "GMAIL_SEND_EMAIL", "data": {"id": "m1"}},
- {"index": 1, "connector": "slack", "tool": "SLACK_POST_MESSAGE", "data": "ok"},
- ]
- ),
- )
+ FakeResponse(200, single_result("gmail", "GMAIL_SEND_EMAIL", {"id": "m1"})),
+ FakeResponse(200, single_result("slack", "SLACK_POST_MESSAGE", "ok")),
)
results = make_client(transport).execute(planned())
- assert len(transport.requests) == 1
- request = transport.requests[0]
- assert request["url"].endswith("/v1/connectors/execute")
- assert request["json"] == {
- "tools": [
- {"connector": "gmail", "tool": "GMAIL_SEND_EMAIL", "arguments": {"to": "x"}},
- {"connector": "slack", "tool": "SLACK_POST_MESSAGE", "arguments": {}},
- ]
- }
- assert request["headers"]["x-idempotency-key"] # present, non-empty
- assert request["headers"]["Authorization"] == "Bearer nous-token"
+ assert [r["url"] for r in transport.requests] == ["https://tool-gateway.test/v1/connectors/execute"] * 2
+ assert [r["json"] for r in transport.requests] == [
+ {"call": {"connector": "gmail", "tool": "GMAIL_SEND_EMAIL", "arguments": {"to": "x"}}},
+ {"call": {"connector": "slack", "tool": "SLACK_POST_MESSAGE", "arguments": {}}},
+ ]
+ keys = [r["headers"]["x-idempotency-key"] for r in transport.requests]
+ assert all(keys) and keys[0] != keys[1]
+ assert transport.requests[0]["headers"]["Authorization"] == "Bearer nous-token"
assert results == [
{"data": {"id": "m1"}, "error": None},
{"data": "ok", "error": None},
@@ -123,12 +117,7 @@ def test_execute_sends_one_request_with_camelcase_body_and_key():
def test_retry_on_5xx_reuses_the_same_idempotency_key():
transport = FakeTransport(
FakeResponse(502, {"error": {"code": "BAD_GATEWAY", "message": "upstream"}}),
- FakeResponse(
- 200,
- execute_envelope(
- [{"index": 0, "connector": "gmail", "tool": "GMAIL_SEND_EMAIL", "data": "sent"}]
- ),
- ),
+ FakeResponse(200, single_result("gmail", "GMAIL_SEND_EMAIL", "sent")),
)
results = make_client(transport).execute(planned(PLAN_CALLS[:1]))
@@ -204,32 +193,27 @@ def test_401_raises_auth_error_and_missing_token_fails_fast():
assert no_token.requests == []
-def test_connection_required_stays_inside_the_200_envelope():
+def test_connection_required_409_is_a_call_entry_with_link_and_account():
transport = FakeTransport(
FakeResponse(
- 200,
- execute_envelope(
- [
- {
- "index": 0,
- "connector": "gmail",
- "tool": "GMAIL_SEND_EMAIL",
- "error": {
- "code": "CONNECTION_REQUIRED",
- "message": "connect gmail",
- "connector": "gmail",
- "connectUrl": "https://example.test/connect/1",
- "connectionId": "ca_1",
- },
- }
- ]
- ),
+ 409,
+ {
+ "code": "CONNECTION_REQUIRED",
+ "family": "product",
+ "message": "connect gmail",
+ "connector": "gmail",
+ "connectUrl": "https://example.test/connect/1",
+ "connectionId": "ca_1",
+ "requestId": "req_1",
+ },
)
)
(result,) = make_client(transport).execute(planned(PLAN_CALLS[:1]))
+ assert len(transport.requests) == 1
assert result["error"]["code"] == "CONNECTION_REQUIRED"
assert result["error"]["connect_url"] == "https://example.test/connect/1"
assert result["error"]["connection_id"] == "ca_1"
+ assert result["error"]["request_id"] == "req_1"
# ---------------------------------------------------------------------------
diff --git a/tools/connectors/gateway/bridge.py b/tools/connectors/gateway/bridge.py
index 2793e5d753c..384dec8882c 100644
--- a/tools/connectors/gateway/bridge.py
+++ b/tools/connectors/gateway/bridge.py
@@ -166,6 +166,7 @@ def run_remote(
# A CONNECTION_REQUIRED link minted by this call is the user's next click, so the call names
# the surface that browser should come back to.
remote_results = client.execute(wire_planned, **return_to_args())
+ _log_call_failures(remote_results, dispatch_id)
entries = splice_remote_results(planned, remote_results)
except ToolGatewayError as exc:
logger.warning(
@@ -205,6 +206,7 @@ def run_remote(
# Retry confirmed misses once without disturbing successful sibling slots.
try:
fallback_results = client.execute(fallback_planned, **return_to_args())
+ _log_call_failures(fallback_results, dispatch_id)
fallback_entries = splice_remote_results(fallback_planned, fallback_results)
except ToolGatewayError as exc:
logger.warning(
@@ -228,3 +230,14 @@ def run_remote(
for slot, fallback_entry in zip(fallback_slots, fallback_entries):
entries[slot] = fallback_entry
return entries
+
+
+def _log_call_failures(results: Any, dispatch_id: Optional[str]) -> None:
+ """One WARNING per failed call, carrying the gateway's request id so the line joins its trace."""
+ for result in results if isinstance(results, list) else ():
+ error = result.get("error") if isinstance(result, dict) else None
+ if isinstance(error, dict):
+ logger.warning(
+ "connector execute %s %s request_id=%s dispatch=%s",
+ error.get("status", 200), error.get("code"), error.get("request_id", "-"), dispatch_id,
+ )
diff --git a/tools/connectors/gateway/client.py b/tools/connectors/gateway/client.py
index 17eff69730b..feef66f0f72 100644
--- a/tools/connectors/gateway/client.py
+++ b/tools/connectors/gateway/client.py
@@ -7,9 +7,10 @@ from __future__ import annotations
import logging
import os
+import time
import uuid
from dataclasses import dataclass
-from typing import Any, Callable, Optional, Protocol, Sequence
+from typing import Any, Callable, Mapping, Optional, Protocol, Sequence
import requests
from pydantic import ValidationError
@@ -37,6 +38,12 @@ SEARCH_TIMEOUT_SECONDS = 30.0
SCHEMAS_TIMEOUT_SECONDS = 10.0
_MAX_RETRIES = 1
+# Execute retries these once under the same idempotency key; the gateway replays a settled call.
+_RETRY_ONCE_STATUSES = frozenset({502, 503, 504})
+# A 429 asking for a longer wait is returned to the model rather than holding the turn.
+_MAX_RETRY_AFTER_SECONDS = 10.0
+# Gateway origins that rejected the single-call execute form; they get the array form for the process.
+_LEGACY_EXECUTE_ORIGINS: set[str] = set()
@dataclass(frozen=True)
@@ -91,6 +98,7 @@ class ConnectorClient:
header_provider: Optional[Callable[[str], dict]] = None,
call: Optional[CallRef] = None,
initiator: Optional[str] = None,
+ sleep: Callable[[float], None] = time.sleep,
) -> None:
self._transport = transport or _default_transport()
self._endpoint_resolver = endpoint_resolver or _default_endpoint_resolver
@@ -98,6 +106,7 @@ class ConnectorClient:
# The model call this client serves, and who asked for a connect; both go on the wire.
self._call = call
self._initiator = initiator
+ self._sleep = sleep
def search(self, queries: Sequence[dict[str, Any]]) -> dict[str, Any]:
body = wire.ConnectorSearchRequest(
@@ -198,7 +207,67 @@ class ConnectorClient:
def execute(
self, planned: Sequence[PlannedCall], *, return_to: Optional[str] = None, op: Optional[str] = None,
) -> list[dict[str, Any]]:
- """Return gateway results in request order; merge owns length mismatches."""
+ """One result per planned call, in request order: ``{"data", "error"}``.
+
+ Each call is its own single-call request; a failure status with an error body is that call's
+ error entry. A gateway that predates the single-call form gets the legacy array once and is
+ remembered for the process."""
+ origin = self._endpoint_resolver()
+ if origin and origin in _LEGACY_EXECUTE_ORIGINS:
+ return self._execute_legacy(planned, return_to=return_to, op=op)
+ results: list[dict[str, Any]] = []
+ for index, plan in enumerate(planned):
+ result = self._execute_one(plan, return_to=return_to, op=op)
+ if result is None:
+ if origin:
+ _LEGACY_EXECUTE_ORIGINS.add(origin)
+ logger.info("Connector gateway predates single-call execute; using the array form")
+ return results + self._execute_legacy(planned[index:], return_to=return_to, op=op)
+ results.append(result)
+ return results
+
+ def _execute_one(
+ self, plan: PlannedCall, *, return_to: Optional[str], op: Optional[str],
+ ) -> Optional[dict[str, Any]]:
+ """``None`` when the gateway rejected the single-call form itself (an older gateway)."""
+ body = wire.ConnectorExecuteSingleRequest(
+ call=wire.ConnectorExecuteCall(connector=plan.connector, tool=plan.tool, arguments=plan.arguments),
+ return_to=return_to, op=op,
+ ).model_dump(by_alias=True, exclude_none=True)
+ url = self._url(wire.CONNECTOR_EXECUTE_PATH)
+ idempotency_key = str(uuid.uuid4())
+ for attempt in range(1 + _MAX_RETRIES):
+ response = self._send("POST", url, body, timeout=EXECUTE_TIMEOUT_SECONDS, idempotency_key=idempotency_key,
+ retry_transport=attempt < _MAX_RETRIES)
+ if response is None:
+ continue
+ status = int(getattr(response, "status_code", 0))
+ payload = _safe_json(response)
+ if 200 <= status < 300:
+ parsed = self._parse(wire.ConnectorExecuteSingleResponse, payload, "connector execute result")
+ entry = {"data": parsed.result.data, "error": None}
+ if parsed.result.account:
+ entry["account"] = parsed.result.account
+ return entry
+ headers = getattr(response, "headers", None)
+ if status == 400 and _rejects_single_call_form(payload):
+ return None
+ failure = _call_failure(payload)
+ wait = _retry_wait(status, headers) if attempt < _MAX_RETRIES else None
+ if wait is not None:
+ logger.debug("Connector execute attempt %d got %d; retrying with same key", attempt + 1, status)
+ if wait:
+ self._sleep(wait)
+ continue
+ if failure is None:
+ raise parse_gateway_error(status, payload, headers)
+ return {"data": None, "error": _error_dict(failure, status=status, headers=headers)}
+ raise ToolGatewayError("connector execute retries exhausted", code="TRANSPORT_ERROR", retryable=True)
+
+ def _execute_legacy(
+ self, planned: Sequence[PlannedCall], *, return_to: Optional[str] = None, op: Optional[str] = None,
+ ) -> list[dict[str, Any]]:
+ """The array form an older gateway accepts: a 200 envelope with per-call errors inside."""
body = wire.ConnectorExecuteRequest(
tools=[
wire.ConnectorExecuteCall(
@@ -233,6 +302,30 @@ class ConnectorClient:
timeout=timeout, idempotency_key=idempotency_key, retries=retries,
)
+ def _url(self, path: str) -> str:
+ origin = self._endpoint_resolver()
+ if not origin:
+ raise GatewayUnavailable(
+ "no tool gateway origin resolves", code="NO_ORIGIN"
+ )
+ return f"{origin.rstrip('/')}/{path}"
+
+ def _send(
+ self, method: str, url: str, body: Optional[dict[str, Any]], *,
+ timeout: float, idempotency_key: Optional[str], retry_transport: bool,
+ ) -> Any:
+ """One HTTP attempt. A transport failure returns ``None`` when the caller may retry, else raises."""
+ headers = self._attempt_headers(url, idempotency_key)
+ try:
+ return self._transport.request(method, url, headers=headers, json=body, timeout=timeout)
+ except Exception as exc:
+ log_connector_outcome(_route_key(url.split("/", 3)[-1]), None, code="TRANSPORT_ERROR", call=self._call)
+ if retry_transport:
+ return None
+ raise ToolGatewayError(
+ f"transport failure: {exc}", code="TRANSPORT_ERROR", retryable=True
+ ) from exc
+
def _request(
self,
method: str,
@@ -245,28 +338,13 @@ class ConnectorClient:
) -> _Reply:
"""The 2xx reply, unchecked; the caller logs its outcome once the body is read. A failed
status is logged and raised here."""
- origin = self._endpoint_resolver()
- if not origin:
- raise GatewayUnavailable(
- "no tool gateway origin resolves", code="NO_ORIGIN"
- )
- url = f"{origin.rstrip('/')}/{path}"
-
+ url = self._url(path)
route = _route_key(path)
- last_error: Optional[ToolGatewayError] = None
for attempt in range(1 + retries):
- headers = self._attempt_headers(url, idempotency_key)
- try:
- response = self._transport.request(
- method, url, headers=headers, json=body, timeout=timeout
- )
- except Exception as exc:
- last_error = ToolGatewayError(
- f"transport failure: {exc}", code="TRANSPORT_ERROR", retryable=True
- )
- log_connector_outcome(route, None, code="TRANSPORT_ERROR", call=self._call)
+ response = self._send(method, url, body, timeout=timeout, idempotency_key=idempotency_key,
+ retry_transport=attempt < retries)
+ if response is None:
continue
-
status = int(getattr(response, "status_code", 0))
request_id = response_request_id(response)
if 200 <= status < 300:
@@ -276,7 +354,6 @@ class ConnectorClient:
error.request_id = error.request_id or request_id
log_connector_outcome(route, status, code=error.code, request_id=error.request_id, call=self._call)
if error.retryable and attempt < retries:
- last_error = error
logger.debug(
"Connector %s attempt %d got %d; retrying with same key",
path,
@@ -285,9 +362,7 @@ class ConnectorClient:
)
continue
raise error
-
- assert last_error is not None
- raise last_error
+ raise ToolGatewayError("connector request retries exhausted", code="TRANSPORT_ERROR", retryable=True)
def _attempt_headers(self, url: str, idempotency_key: Optional[str]) -> dict[str, str]:
"""Every header of one HTTP attempt: auth, caller identity, and the idempotency key."""
@@ -327,21 +402,82 @@ def _route_key(path: str) -> str:
def _result_dict(result: wire.ConnectorExecuteResult) -> dict[str, Any]:
error = None
if result.error is not None:
- error = {"code": result.error.code, "message": result.error.message}
- if result.error.connector:
- error["connector"] = result.error.connector
- if result.error.connect_url:
- error["connect_url"] = result.error.connect_url
- if result.error.connection_id:
- error["connection_id"] = result.error.connection_id
- if result.error.hint:
- error["hint"] = result.error.hint
+ error = _error_dict(result.error)
entry = {"data": result.data, "error": error}
if result.account:
entry["account"] = result.account
return entry
+def _error_dict(
+ failure: wire.ConnectorToolError, *, status: Optional[int] = None, headers: Optional[Mapping[str, Any]] = None,
+) -> dict[str, Any]:
+ """The plain error entry merge renders. ``status`` and ``request_id`` are for logs, never the model."""
+ error: dict[str, Any] = {"code": failure.code, "message": failure.message}
+ family = wire.execute_error_family(failure.code, failure.family)
+ if family:
+ error["family"] = family
+ for key, value in (("connector", failure.connector), ("connect_url", failure.connect_url),
+ ("connection_id", failure.connection_id), ("hint", failure.hint)):
+ if value:
+ error[key] = value
+ if status is not None:
+ error["status"] = status
+ request_id = getattr(failure, "request_id", None) or (headers or {}).get("x-request-id")
+ if request_id:
+ error["request_id"] = str(request_id)
+ return error
+
+
+def _call_failure(payload: Any) -> Optional[wire.ConnectorExecuteFailure]:
+ """A single-call failure body: a flat ``{code, message}`` the gateway marks with a family or a code
+ this client knows. Anything else (auth, dark route, idempotency conflict) stays a route failure."""
+ if not isinstance(payload, Mapping) or not isinstance(payload.get("code"), str):
+ return None
+ if payload.get("family") is None and payload["code"] not in wire.EXECUTE_ERROR_CODES:
+ return None
+ try:
+ return wire.ConnectorExecuteFailure.model_validate(
+ {"message": "The connector gateway reported an error.", **payload})
+ except ValidationError:
+ return None
+
+
+def _rejects_single_call_form(payload: Any) -> bool:
+ """An older gateway validates every body as the array form and names ``tools`` in its 400."""
+ if not isinstance(payload, Mapping):
+ return False
+ issues = payload.get("issues")
+ if isinstance(issues, list):
+ for issue in issues:
+ path = issue.get("path") if isinstance(issue, Mapping) else None
+ if isinstance(path, list) and path[:1] == ["tools"]:
+ return True
+ envelope = payload.get("error")
+ code = envelope.get("code") if isinstance(envelope, Mapping) else payload.get("code")
+ if code != "VALIDATION_ERROR":
+ return False
+ text = envelope if isinstance(envelope, Mapping) else payload
+ return "tools" in f"{text.get('message', '')} {text.get('details', '')}"
+
+
+def _retry_wait(status: int, headers: Optional[Mapping[str, Any]]) -> Optional[float]:
+ """Seconds to wait before the one retry, or ``None`` when this status is not retried.
+
+ A 429 is retried only when the gateway names a wait, and only a short one; a longer wait goes back
+ to the model as the call's error instead of holding the turn."""
+ if status in _RETRY_ONCE_STATUSES:
+ return 0.0
+ if status != 429:
+ return None
+ raw = (headers or {}).get("Retry-After")
+ try:
+ wait = float(raw)
+ except (TypeError, ValueError):
+ return None
+ return wait if 0 <= wait <= _MAX_RETRY_AFTER_SECONDS else None
+
+
def _safe_json(response: Any) -> Any:
try:
return response.json()