Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
py 286 lines 9.96 KB
Raw
1 """Tool executions and proxied API calls must not retry.
2
3 A POST that times out while the backend is still processing it is unsafe to
4 retry: the request may already have taken effect, so a silent re-send can
5 duplicate the side effect (e.g. send an email twice). The backend does not
6 deduplicate executions, so every execution path (``tools.execute``,
7 ``tools.proxy``, and the session ``execute`` / ``proxy_execute`` paths) routes
8 through ``client.without_retries`` (a retry-disabled clone), while reads keep
9 the default retry behaviour.
10
11 See https://github.com/ComposioHQ/composio/issues/3654.
12 """
13
14 import inspect
15 import typing as t
16 from unittest.mock import Mock, patch
17
18 import httpx
19 import pytest
20 from composio_client import DEFAULT_MAX_RETRIES, APIError, ProjectAuth
21 from composio_client import Composio as BaseComposio
22
23 from composio.client import HttpClient
24 from composio.core.models.base import allow_tracking
25 from composio.core.models.session_context import SessionContextImpl
26 from composio.core.models.tool_router_session import ToolRouterSession
27 from composio.core.models.tools import Tools
28
29
30 @pytest.fixture(autouse=True)
31 def disable_telemetry():
32 """Disable telemetry for all tests to prevent thread issues."""
33 token = allow_tracking.set(False)
34 yield
35 allow_tracking.reset(token)
36
37
38 @pytest.fixture
39 def no_sleep(monkeypatch: pytest.MonkeyPatch) -> None:
40 """Neutralise retry backoff so retry tests run instantly."""
41 import composio_client._base_client as base
42
43 monkeypatch.setattr(base.time, "sleep", lambda *args, **kwargs: None)
44
45
46 def _client_with_transport(
47 handler: t.Callable[[httpx.Request], httpx.Response],
48 ) -> HttpClient:
49 """Build an ``HttpClient`` whose HTTP requests are served by ``handler``.
50
51 ``base_url`` is passed explicitly so the test does not depend on
52 ``COMPOSIO_BASE_URL`` possibly being set in the environment.
53 """
54 return HttpClient(
55 provider="test",
56 auth=ProjectAuth("sk-test"),
57 base_url="https://backend.invalid",
58 http_client=httpx.Client(transport=httpx.MockTransport(handler)),
59 )
60
61
62 class TestCopyOverride:
63 """Guards the ``HttpClient.with_options()`` override."""
64
65 def test_with_options_does_not_raise_and_overrides_max_retries(self) -> None:
66 client = HttpClient(
67 provider="myprovider",
68 auth=ProjectAuth("sk-test"),
69 base_url="https://backend.invalid",
70 )
71
72 # Without the with_options() override this raises:
73 # TypeError: missing required keyword-only argument 'provider'.
74 clone = client.with_options(max_retries=0)
75
76 assert isinstance(clone, HttpClient)
77 assert clone.max_retries == 0
78 assert clone.provider == "myprovider"
79 assert clone.request_ctx.get()["provider"] == "myprovider"
80 # The original client keeps its retries.
81 assert client.max_retries == DEFAULT_MAX_RETRIES
82
83 def test_without_retries_is_a_cached_zero_retry_sibling(self) -> None:
84 client = HttpClient(
85 provider="test",
86 auth=ProjectAuth("sk-test"),
87 base_url="https://backend.invalid",
88 )
89
90 assert client.without_retries.max_retries == 0
91 # Cached: repeated access returns the same instance (no per-call clone).
92 assert client.without_retries is client.without_retries
93 # Reads on the original client keep retrying.
94 assert client.max_retries == DEFAULT_MAX_RETRIES
95
96 def test_clone_preserves_strict_response_validation(self) -> None:
97 # The sibling must differ from the parent only in retries, including
98 # `_strict_response_validation`, which the generated `copy` carries.
99 client = HttpClient(
100 provider="test",
101 auth=ProjectAuth("sk-test"),
102 base_url="https://backend.invalid",
103 _strict_response_validation=True,
104 )
105
106 assert client.without_retries._strict_response_validation is True
107
108
109 class TestBaseWithOptionsContract:
110 """Pin the generated-client internals the ``with_options()`` override depends on.
111
112 These guard the contract so a ``composio_client`` regen that breaks it fails
113 as an obvious assertion here rather than a cryptic ``TypeError`` raised deep
114 inside ``with_options`` at runtime.
115 """
116
117 def test_base_with_options_accepts_extra_kwargs(self) -> None:
118 # The override threads `provider` (and the SDK logger) through
119 # `_extra_kwargs`; the base `with_options` must still accept it.
120 # Read through `__dict__`: up to rc.19 `with_options` is a class-level
121 # alias of `copy`, which mypy will not let a generic class expose.
122 base_with_options = BaseComposio.__dict__["with_options"]
123 assert "_extra_kwargs" in inspect.signature(base_with_options).parameters
124
125 def test_with_options_is_overridden(self) -> None:
126 assert "with_options" in HttpClient.__dict__
127
128
129 class TestWritePathDoesNotRetry:
130 """``execute`` / ``proxy`` must reach the transport exactly once."""
131
132 def test_execute_does_not_retry_on_transient_error(self, no_sleep: None) -> None:
133 attempts: t.List[httpx.Request] = []
134
135 def handler(request: httpx.Request) -> httpx.Response:
136 attempts.append(request)
137 return httpx.Response(500, json={"error": {"message": "boom"}})
138
139 client = _client_with_transport(handler)
140 tools = Tools(client=client, provider=Mock())
141
142 # Avoid the (read) tool-schema lookup hitting the transport.
143 mock_tool = Mock()
144 mock_tool.toolkit.slug = "gmail"
145 with patch.object(
146 tools, "get_raw_composio_tool_by_slug", return_value=mock_tool
147 ):
148 with pytest.raises(APIError):
149 tools._execute_tool(
150 slug="GMAIL_SEND_EMAIL",
151 arguments={},
152 tool=mock_tool,
153 version="1.0.0",
154 )
155
156 assert len(attempts) == 1
157
158 def test_proxy_does_not_retry_on_transient_error(self, no_sleep: None) -> None:
159 attempts: t.List[httpx.Request] = []
160
161 def handler(request: httpx.Request) -> httpx.Response:
162 attempts.append(request)
163 return httpx.Response(500, json={"error": {"message": "boom"}})
164
165 client = _client_with_transport(handler)
166 tools = Tools(client=client, provider=Mock())
167
168 # `proxy` does no tool-schema lookup, so the only transport hit is the
169 # write itself — no read to patch out (unlike the execute test above).
170 with pytest.raises(APIError):
171 tools.proxy(endpoint="/any", method="POST")
172
173 assert len(attempts) == 1
174
175
176 SESSION_ID = "sess_123"
177
178
179 def _session(client: HttpClient) -> ToolRouterSession:
180 return ToolRouterSession(
181 client=client,
182 provider=None,
183 dangerously_allow_auto_upload_download_files=False,
184 session_id=SESSION_ID,
185 mcp=Mock(),
186 experimental=Mock(),
187 )
188
189
190 def _provider_wrapped_session_tool(client: HttpClient) -> t.Any:
191 tools = Tools(client=client, provider=Mock())
192 # Avoid the (read) tool-schema lookup hitting the transport.
193 mock_tool = Mock()
194 mock_tool.toolkit.slug = "gmail"
195 mock_tool.input_parameters = {}
196 tools._tool_schemas["GMAIL_SEND_EMAIL"] = mock_tool
197 execute = tools._wrap_execute_tool_for_tool_router(session_id=SESSION_ID)
198 return execute("GMAIL_SEND_EMAIL", {"to": "test@test.com"})
199
200
201 PROXY_KWARGS: t.Dict[str, t.Any] = {
202 "toolkit": "github",
203 "endpoint": "https://api.github.com/user/repos",
204 "method": "POST",
205 "body": {"name": "repo"},
206 }
207
208 SESSION_EXECUTION_PATHS: t.List[t.Tuple[str, t.Callable[[HttpClient], t.Any]]] = [
209 ("provider_wrapped_session_tool", _provider_wrapped_session_tool),
210 (
211 "session_execute",
212 lambda client: _session(client).execute(
213 "GMAIL_SEND_EMAIL", arguments={"to": "test@test.com"}
214 ),
215 ),
216 (
217 "session_proxy_execute",
218 lambda client: _session(client).proxy_execute(**PROXY_KWARGS),
219 ),
220 (
221 "custom_tool_context_execute",
222 lambda client: SessionContextImpl(client, "test-user", SESSION_ID).execute(
223 "GMAIL_SEND_EMAIL", {"to": "test@test.com"}
224 ),
225 ),
226 (
227 "custom_tool_context_proxy_execute",
228 lambda client: SessionContextImpl(
229 client, "test-user", SESSION_ID
230 ).proxy_execute(**PROXY_KWARGS),
231 ),
232 ]
233
234
235 class TestSessionExecutionDoesNotRetry:
236 """Every session execution path must reach the transport exactly once."""
237
238 @pytest.mark.parametrize(
239 "run",
240 [run for _, run in SESSION_EXECUTION_PATHS],
241 ids=[name for name, _ in SESSION_EXECUTION_PATHS],
242 )
243 def test_does_not_retry_on_transient_error(
244 self, run: t.Callable[[HttpClient], t.Any], no_sleep: None
245 ) -> None:
246 attempts: t.List[httpx.Request] = []
247
248 def handler(request: httpx.Request) -> httpx.Response:
249 attempts.append(request)
250 return httpx.Response(500, json={"error": {"message": "boom"}})
251
252 with pytest.raises(APIError):
253 run(_client_with_transport(handler))
254
255 assert len(attempts) == 1
256
257
258 class TestReadPathStillRetries:
259 """Reads keep the default retry behaviour — only writes are scoped to no-retry."""
260
261 def test_read_retries_then_succeeds(self, no_sleep: None) -> None:
262 attempts: t.List[httpx.Request] = []
263 responses = [
264 httpx.Response(503, json={"error": {"message": "transient"}}),
265 httpx.Response(
266 200,
267 json={
268 "current_page": 1,
269 "items": [],
270 "total_items": 0,
271 "total_pages": 1,
272 },
273 ),
274 ]
275
276 def handler(request: httpx.Request) -> httpx.Response:
277 attempts.append(request)
278 return responses[len(attempts) - 1]
279
280 client = _client_with_transport(handler)
281
282 result = client.tools.list()
283
284 # First attempt 503, retried, second attempt 200 -> success.
285 assert len(attempts) == 2
286 assert result.total_items == 0