Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

43.4 KB
Raw Blame
"""Gmail tools on a Composio-backed grant.

Such a grant holds no Google token: the tool process is launched with the
Composio ids instead (``tools/config._composio_grant_env``), the service is
built on the proxy transport, and every call goes through Composio's Proxy
Execute endpoint. Composio is faked here; no request leaves the process.
"""

from __future__ import annotations

import base64
import json
import logging
import time
from contextlib import asynccontextmanager
from email import policy
from email.parser import BytesParser
from types import SimpleNamespace
from typing import Any
from unittest.mock import Mock

import httplib2
import pytest
from googleapiclient.discovery import build
from mcp.types import CallToolResult

from tests.web.tools.conftest import (
    ACCOUNT_ID,
    API_KEY,
    COMPOSIO_USER,
    _Response,
)
from xagent.core.model.chat.token_context import TokenContextManager
from xagent.core.tools.adapters.vibe import mcp_adapter as mcp_adapter_module
from xagent.core.tools.adapters.vibe.mcp_adapter import MCPToolAdapter
from xagent.core.tools.core.mcp.composio_requests import COMPOSIO_REQUESTS_META_KEY
from xagent.web.tools import config as tools_config
from xagent.web.tools.mcp import composio_proxy, gmail, google_api_proxy

_MESSAGE = {
    "id": "m1",
    "threadId": "t1",
    "snippet": "hello there",
    "payload": {
        "headers": [
            {"name": "Subject", "value": "Hello"},
            {"name": "From", "value": "[email protected]"},
        ]
    },
}
_NOT_FOUND = {
    "error": {
        "code": 404,
        "message": "Requested entity was not found.",
        "errors": [{"message": "Not Found", "domain": "global"}],
    }
}
# A metadata read that names no header returns every header, in the order
# the mail carries them; one of them is spelled differently from the name the
# tool reads, to keep its case-insensitive lookup covered.
_METADATA_MESSAGE = {
    "id": "m1",
    "threadId": "t1",
    "snippet": "the numbers",
    "payload": {
        "headers": [
            {"name": "Delivered-To", "value": "[email protected]"},
            {"name": "Received", "value": "by mx.example.com with SMTP id abc"},
            {"name": "DKIM-Signature", "value": "v=1; a=rsa-sha256; d=example.com"},
            {"name": "subject", "value": "Quarterly numbers"},
            {"name": "To", "value": "Ada <[email protected]>"},
            {"name": "From", "value": "Grace <[email protected]>"},
            {"name": "Date", "value": "Mon, 10 Nov 2025 09:00:00 +0000"},
        ]
    },
}
_BATCH_BOUNDARY = "gmail-tools-batch"


def _proxy_service() -> Any:
    return build(
        "gmail",
        "v1",
        http=google_api_proxy.GoogleApiProxyHttp(
            allowed_hosts=("gmail.googleapis.com",)
        ),
        static_discovery=True,
    )


def _secrets_in(text: str) -> list[str]:
    return [value for value in (API_KEY, ACCOUNT_ID, COMPOSIO_USER) if value in text]


def _batch_part(request_id: str, status: int, body: str) -> str:
    return (
        f"--{_BATCH_BOUNDARY}\r\n"
        "Content-Type: application/http\r\n"
        "Content-Transfer-Encoding: binary\r\n"
        f"Content-ID: <base + {request_id}>\r\n"
        "\r\n"
        f"HTTP/1.1 {status} OK\r\n"
        "Content-Type: application/json; charset=UTF-8\r\n"
        f"Content-Length: {len(body)}\r\n"
        "\r\n"
        f"{body}\r\n"
    )


class FakeNativeHttp:
    """A native grant's transport: one JSON answer, or a batch answer.

    ``batch_request_body`` keeps the ``multipart/mixed`` body the batch was
    sent with, so a test can read the requests it carried.
    """

    def __init__(
        self, list_body: dict[str, Any], parts: list[tuple[str, int, str]]
    ) -> None:
        self._list_body = json.dumps(list_body)
        self._batch_body = (
            "".join(_batch_part(*part) for part in parts) + f"--{_BATCH_BOUNDARY}--\r\n"
        )
        self.batch_request_body = ""

    def request(self, uri, method=None, body=None, headers=None):
        if "batch" in uri:
            self.batch_request_body = body
            response = httplib2.Response(
                {"content-type": f'multipart/mixed; boundary="{_BATCH_BOUNDARY}"'}
            )
            return response, self._batch_body.encode("utf-8")
        response = httplib2.Response({"content-type": "application/json"})
        return response, self._list_body.encode("utf-8")


def test_a_composio_grant_builds_the_service_on_the_proxy_transport(
    monkeypatch, composio_env
):
    built: dict[str, Any] = {}

    def fake_build(*args: Any, **kwargs: Any) -> Any:
        built["args"] = args
        built["kwargs"] = kwargs
        return Mock()

    monkeypatch.setattr(gmail, "build", fake_build)

    gmail.get_gmail_service()

    assert built["args"] == ("gmail", "v1")
    kwargs = built["kwargs"]
    assert "credentials" not in kwargs
    assert kwargs["static_discovery"] is True
    transport = kwargs["http"]
    assert isinstance(transport, google_api_proxy.GoogleApiProxyHttp)
    # Gmail's own host only: Composio's injected token cannot be sent to
    # another Google API.
    with pytest.raises(ValueError):
        transport.request("https://www.googleapis.com/drive/v3/files")


def test_building_the_service_fetches_no_discovery_document(proxy):
    """The discovery document is the library's bundled copy; fetching it
    through the proxy would spend a call and could be refused cross-domain."""
    service = gmail.get_gmail_service()

    assert service is not None
    assert proxy.calls == []


def test_a_native_grant_still_builds_credentials(monkeypatch):
    for name in (
        composio_proxy.COMPOSIO_API_KEY_ENV,
        composio_proxy.COMPOSIO_CONNECTED_ACCOUNT_ID_ENV,
        composio_proxy.COMPOSIO_USER_ID_ENV,
    ):
        monkeypatch.delenv(name, raising=False)
    monkeypatch.setenv("GOOGLE_ACCESS_TOKEN", "access-token")
    built: dict[str, Any] = {}

    def fake_build(*args: Any, **kwargs: Any) -> Any:
        built["kwargs"] = kwargs
        return Mock()

    monkeypatch.setattr(gmail, "build", fake_build)

    gmail.get_gmail_service()

    kwargs = built["kwargs"]
    assert kwargs["credentials"].token == "access-token"
    assert "http" not in kwargs
    assert "static_discovery" not in kwargs


def test_a_native_batch_is_still_one_batch(monkeypatch):
    monkeypatch.delenv(composio_proxy.COMPOSIO_CONNECTED_ACCOUNT_ID_ENV, raising=False)
    batch = Mock()

    gmail._execute_batch(batch)

    batch.execute.assert_called_once_with()


def test_a_composio_batch_is_run_by_the_proxy_fallback(monkeypatch, composio_env):
    batch = Mock()
    fallback = Mock()
    monkeypatch.setattr(google_api_proxy, "execute_batch", fallback)

    gmail._execute_batch(batch)

    fallback.assert_called_once_with(batch)
    batch.execute.assert_not_called()


def test_search_reads_each_message_through_the_proxy(proxy, monkeypatch):
    proxy.answer("/messages/m1", 200, _MESSAGE)
    proxy.answer("/messages", 200, {"messages": [{"id": "m1"}]})
    service = _proxy_service()
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: service)

    result = json.loads(gmail.gmail_search_messages("from:someone", max_results=1))

    assert result == {
        "status": "success",
        "messages": [
            {
                "id": "m1",
                "threadId": "t1",
                "snippet": "hello there",
                "subject": "Hello",
                "from": "[email protected]",
            }
        ],
        "errors": None,
    }
    list_call, message_call = proxy.calls
    assert list_call["method"] == "GET"
    assert list_call["endpoint"] == (
        "https://gmail.googleapis.com/gmail/v1/users/me/messages"
    )
    assert {
        "name": "q",
        "value": "from:someone",
        "type": "query",
    } in list_call["parameters"]
    assert message_call["endpoint"] == (
        "https://gmail.googleapis.com/gmail/v1/users/me/messages/m1"
    )


def test_search_reads_subjects_from_every_header_on_a_composio_grant(
    proxy, monkeypatch
):
    """A metadata read that names no header returns every header. Asking for
    ``metadataHeaders`` twice puts the same query name twice in one request,
    and the proxy appears to keep one value of it (replies carried From but
    not Subject), so every subject read "No Subject"."""
    proxy.answer("/messages/m1", 200, _METADATA_MESSAGE)
    proxy.answer("/messages", 200, {"messages": [{"id": "m1"}]})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(gmail.gmail_search_messages("q", max_results=1))

    assert result["messages"] == [
        {
            "id": "m1",
            "threadId": "t1",
            "snippet": "the numbers",
            "subject": "Quarterly numbers",
            "from": "Grace <[email protected]>",
        }
    ]
    list_call, message_call = proxy.calls
    assert list_call["endpoint"] == (
        "https://gmail.googleapis.com/gmail/v1/users/me/messages"
    )
    query = [item for item in message_call["parameters"] if item["type"] == "query"]
    assert {"name": "format", "value": "metadata", "type": "query"} in query
    assert [item for item in query if item["name"] == "metadataHeaders"] == []


def test_a_composio_search_with_two_label_ids_is_refused_before_sending(
    proxy, monkeypatch
):
    """The tool refuses a second label id itself and points at ``query``: on a
    Composio grant a search takes one label id, and Gmail returns only the
    messages that carry every label given, so one search per label would
    merge into a wider result. The shared client's refusal stays as a
    backstop."""
    proxy.answer("/messages", 200, {"messages": []})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(
        gmail.gmail_search_messages("q", label_ids=["INBOX", "STARRED"], max_results=1)
    )

    assert result["status"] == "error"
    assert "one label id" in result["message"]
    assert "query" in result["message"]
    assert proxy.calls == []


def test_a_composio_search_with_one_label_id_carries_it_once(proxy, monkeypatch):
    """The ``gmail.search_seed_message`` smoke check searches by a single label
    id on this path: the list request carries that one ``labelIds``
    parameter."""
    proxy.answer("/messages", 200, {"messages": []})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(
        gmail.gmail_search_messages(label_ids=["INBOX"], max_results=10)
    )

    assert result["status"] == "success"
    (list_call,) = proxy.calls
    assert list_call["endpoint"] == (
        "https://gmail.googleapis.com/gmail/v1/users/me/messages"
    )
    label_ids = [item for item in list_call["parameters"] if item["name"] == "labelIds"]
    assert label_ids == [{"name": "labelIds", "value": "INBOX", "type": "query"}]


def test_search_with_no_messages_sends_only_the_list_request(proxy, monkeypatch):
    proxy.answer("/messages", 200, {"messages": []})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(gmail.gmail_search_messages("nothing"))

    assert result == {"status": "success", "messages": []}
    assert len(proxy.calls) == 1


def test_read_threads_sends_one_request_per_thread(proxy, monkeypatch):
    proxy.answer("/threads/t1", 200, {"id": "t1", "messages": [{"id": "m1"}]})
    proxy.answer("/threads/t2", 200, {"id": "t2", "messages": []})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(gmail.gmail_read_threads(["t1", "t2"]))

    assert result == {
        "status": "success",
        "threads": [
            {"id": "t1", "messages": [{"id": "m1"}]},
            {"id": "t2", "messages": []},
        ],
        "errors": None,
    }
    # The two requests are sent concurrently, so the order in which they reach
    # the proxy is not fixed; only the order of the results is.
    assert sorted(call["endpoint"] for call in proxy.calls) == [
        "https://gmail.googleapis.com/gmail/v1/users/me/threads/t1",
        "https://gmail.googleapis.com/gmail/v1/users/me/threads/t2",
    ]


def test_creating_a_label_sends_its_body_through_the_proxy(proxy, monkeypatch):
    proxy.answer("/labels", 200, {"id": "Label_1", "name": "Receipts"})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(gmail.gmail_manage_labels("create", name="Receipts"))

    assert result["label"] == {"id": "Label_1", "name": "Receipts"}
    (call,) = proxy.calls
    assert call["method"] == "POST"
    assert call["endpoint"] == "https://gmail.googleapis.com/gmail/v1/users/me/labels"
    assert call["body"] == {
        "name": "Receipts",
        "labelListVisibility": "labelShow",
        "messageListVisibility": "show",
    }
    assert {"name": "content-type", "value": "application/json", "type": "header"} in (
        call["parameters"]
    )


def test_sending_with_an_attachment_sends_the_encoded_message(
    proxy, monkeypatch, tmp_path
):
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    attachment = tmp_path / "notes.txt"
    attachment.write_text("attached notes", encoding="utf-8")
    proxy.answer("/messages/send", 200, {"id": "sent-1"})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "Notes",
                    "body": "See attached.",
                    "attachments": [str(attachment)],
                }
            ],
            action="send",
        )
    )

    assert result["results"] == [
        {
            "status": "sent",
            "id": "sent-1",
            "attachments": [{"filename": "notes.txt", "size": 14}],
        }
    ]
    (call,) = proxy.calls
    assert call["endpoint"] == (
        "https://gmail.googleapis.com/gmail/v1/users/me/messages/send"
    )
    encoded = call["body"]["raw"]
    message = BytesParser(policy=policy.default).parsebytes(
        base64.urlsafe_b64decode(encoded)
    )
    assert [part.get_filename() for part in message.iter_attachments()] == ["notes.txt"]
    assert message["Subject"] == "Notes"


def test_a_message_too_large_for_the_proxy_is_refused_clearly(
    proxy, monkeypatch, tmp_path
):
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    attachment = tmp_path / "big.bin"
    attachment.write_bytes(b"\x00" * 4096)
    monkeypatch.setattr(composio_proxy, "MAX_JSON_BODY_BYTES", 2000)
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "Big",
                    "body": "B",
                    "attachments": [str(attachment)],
                }
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert "too large to send through Composio" in result["message"]
    assert "2000 byte limit" in result["message"]
    assert "smaller attachment" in result["message"]
    assert proxy.calls == []


def test_the_real_body_limit_binds_at_about_2_5_mb_of_attachments(
    proxy, monkeypatch, tmp_path
):
    """An attachment is base64 inside the MIME part and the whole message is
    base64 again as ``raw``, so the JSON body is about 1.8 times the file, not
    4/3. Against the real 4.5 MB limit a 2.4 MB attachment passes and a 2.6 MB
    one is refused before sending; the boundary is about 2.5 MB (a 3.3 MB
    attachment is 5.9 MB of JSON)."""
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())
    proxy.answer("/messages/send", 200, {"id": "sent-1"})
    limit = composio_proxy.MAX_JSON_BODY_BYTES

    accepted = tmp_path / "under.bin"
    accepted.write_bytes(b"\x00" * 2_400_000)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "Under",
                    "body": "B",
                    "attachments": [str(accepted)],
                }
            ],
            action="send",
        )
    )

    assert result["status"] == "success"
    (call,) = proxy.calls
    body_bytes = len(json.dumps(call))
    assert body_bytes <= limit
    assert 1.79 < body_bytes / 2_400_000 < 1.81

    refused = tmp_path / "over.bin"
    refused.write_bytes(b"\x00" * 2_600_000)

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {
                    "to": "[email protected]",
                    "subject": "Over",
                    "body": "B",
                    "attachments": [str(refused)],
                }
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert "too large to send through Composio" in result["message"]
    assert f"{limit} byte limit" in result["message"]
    assert len(proxy.calls) == 1, "the refused message must not be sent"


def test_an_upstream_error_reaches_the_tool_as_googles_message(proxy, monkeypatch):
    proxy.answer("/labels/missing", 404, _NOT_FOUND)
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(gmail.gmail_manage_labels("get", label_id="missing"))

    assert result["status"] == "error"
    assert "HttpError 404" in result["message"]
    assert "Requested entity was not found." in result["message"]


def test_a_composio_refusal_is_reported_without_secrets(proxy, monkeypatch):
    proxy.default = _Response(
        404,
        {
            "error": {
                "slug": "ConnectedAccount_NotFound",
                "message": f"account {ACCOUNT_ID} of {COMPOSIO_USER} key {API_KEY}",
            }
        },
    )
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(gmail.gmail_manage_labels("list"))

    assert result["status"] == "error"
    assert "ConnectedAccount_NotFound" in result["message"]
    assert _secrets_in(result["message"]) == []


def test_the_batch_fallback_output_matches_a_native_batch(monkeypatch, proxy):
    """The same messages, one tool run through a real native batch and one
    through the proxy, come back as the same messages and the same error. The
    native run still asks for the two headers by name, so the request URI in
    the error text differs between the two paths."""
    list_body = {"messages": [{"id": "m1"}, {"id": "m2"}]}
    native_http = FakeNativeHttp(
        list_body,
        [
            ("1", 200, json.dumps(_MESSAGE)),
            ("2", 404, json.dumps(_NOT_FOUND)),
        ],
    )
    native_service = build("gmail", "v1", http=native_http, static_discovery=True)

    for name in (
        composio_proxy.COMPOSIO_API_KEY_ENV,
        composio_proxy.COMPOSIO_CONNECTED_ACCOUNT_ID_ENV,
        composio_proxy.COMPOSIO_USER_ID_ENV,
    ):
        monkeypatch.delenv(name, raising=False)
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: native_service)
    native_output = json.loads(gmail.gmail_search_messages("q", max_results=2))

    proxy.answer("/messages/m1", 200, _MESSAGE)
    proxy.answer("/messages/m2", 404, _NOT_FOUND)
    proxy.answer("/messages", 200, list_body)
    monkeypatch.setenv(composio_proxy.COMPOSIO_API_KEY_ENV, API_KEY)
    monkeypatch.setenv(composio_proxy.COMPOSIO_CONNECTED_ACCOUNT_ID_ENV, ACCOUNT_ID)
    monkeypatch.setenv(composio_proxy.COMPOSIO_USER_ID_ENV, COMPOSIO_USER)
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())
    proxied_output = json.loads(gmail.gmail_search_messages("q", max_results=2))

    assert proxied_output["status"] == native_output["status"] == "success"
    assert proxied_output["messages"] == native_output["messages"]
    assert len(proxied_output["errors"]) == len(native_output["errors"]) == 1
    assert "HttpError 404" in proxied_output["errors"][0]
    assert "HttpError 404" in native_output["errors"][0]
    assert native_output["messages"][0]["subject"] == "Hello"
    # A native grant still asks Gmail for the two headers by name.
    assert (
        "metadataHeaders=Subject&metadataHeaders=From" in native_http.batch_request_body
    )
    assert "metadataHeaders" not in json.dumps(proxy.calls)


def test_a_composio_gmail_grant_gets_the_composio_launch_variables(monkeypatch):
    monkeypatch.setenv(composio_proxy.COMPOSIO_API_KEY_ENV, API_KEY)

    env = tools_config._composio_grant_env(
        app_id="gmail",
        connected_account_id=ACCOUNT_ID,
        composio_user_id=COMPOSIO_USER,
    )

    assert env == {
        composio_proxy.COMPOSIO_API_KEY_ENV: API_KEY,
        composio_proxy.COMPOSIO_CONNECTED_ACCOUNT_ID_ENV: ACCOUNT_ID,
        composio_proxy.COMPOSIO_USER_ID_ENV: COMPOSIO_USER,
    }


def test_an_oversized_second_message_refuses_the_whole_batch_before_sending(
    proxy, monkeypatch, tmp_path
):
    """#3647 regression guard: the proxy refuses an oversized JSON body
    locally, so a batch whose second message is too large must fail entirely
    up front instead of sending the first one and then erroring."""
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    attachment = tmp_path / "big.bin"
    attachment.write_bytes(b"\x00" * 4096)
    monkeypatch.setattr(composio_proxy, "MAX_JSON_BODY_BYTES", 3000)
    proxy.answer("/messages/send", 200, {"id": "sent-1"})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {"to": "[email protected]", "subject": "Small", "body": "hi"},
                {
                    "to": "[email protected]",
                    "subject": "Big",
                    "body": "B",
                    "attachments": [str(attachment)],
                },
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert "too large to send through Composio" in result["message"]
    assert "3000 byte limit" in result["message"]
    assert proxy.calls == [], "no message may be sent before the whole batch is checked"


def test_sending_more_messages_than_the_cap_is_refused_before_sending(
    proxy, monkeypatch
):
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())
    messages = [
        {"to": "[email protected]", "subject": str(number), "body": "B"}
        for number in range(google_api_proxy.MAX_BATCH_REQUESTS + 1)
    ]

    result = json.loads(gmail.gmail_send_messages(messages, action="send"))

    assert result["status"] == "error"
    assert "request limit for one tool call" in result["message"]
    assert proxy.calls == []


def test_a_send_that_fails_midway_returns_the_messages_already_sent(proxy, monkeypatch):
    """Messages already sent cannot be taken back; the error must report
    them instead of dropping the partial result."""
    calls: list[dict[str, Any]] = []

    def request(**kwargs: Any) -> _Response:
        calls.append(kwargs["json"])
        if len(calls) == 1:
            return _Response(200, {"status": 200, "data": {"id": "sent-1"}})
        return _Response(500, {"error": {"message": "upstream is down"}})

    monkeypatch.setattr(composio_proxy.requests, "request", request)
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(
        gmail.gmail_send_messages(
            [
                {"to": "[email protected]", "subject": "One", "body": "1"},
                {"to": "[email protected]", "subject": "Two", "body": "2"},
            ],
            action="send",
        )
    )

    assert result["status"] == "error"
    assert result["results"] == [{"status": "sent", "id": "sent-1", "attachments": []}]
    assert "1 of 2 messages were sent" in result["message"]
    assert "upstream is down" in result["message"]


def test_a_search_above_the_request_cap_is_refused_before_the_list_call(
    proxy, monkeypatch
):
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(
        gmail.gmail_search_messages(
            "q", max_results=google_api_proxy.MAX_BATCH_REQUESTS + 1
        )
    )

    assert result["status"] == "error"
    assert "request limit for one tool call" in result["message"]
    # The list request is one more proxy call than the message cap counts.
    assert "101 message requests (plus the list request)" in result["message"]
    assert proxy.calls == []


def test_reading_more_threads_than_the_request_cap_is_refused_before_sending(
    proxy, monkeypatch
):
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())
    thread_ids = [
        f"t{number}" for number in range(google_api_proxy.MAX_BATCH_REQUESTS + 1)
    ]

    result = json.loads(gmail.gmail_read_threads(thread_ids))

    assert result["status"] == "error"
    assert "request limit for one tool call" in result["message"]
    assert proxy.calls == []


def test_deleting_a_label_sends_a_delete_through_the_proxy(proxy, monkeypatch):
    proxy.default = _Response(200, {"status": 204, "data": None})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(gmail.gmail_manage_labels("delete", label_id="L1"))

    assert result == {"status": "success", "message": "Label L1 deleted"}
    (call,) = proxy.calls
    assert call["method"] == "DELETE"
    assert (
        call["endpoint"] == "https://gmail.googleapis.com/gmail/v1/users/me/labels/L1"
    )


def test_updating_a_label_patches_through_the_proxy(proxy, monkeypatch):
    proxy.answer("/labels/L1", 200, {"id": "L1", "name": "New"})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(gmail.gmail_manage_labels("update", label_id="L1", name="New"))

    assert result["label"] == {"id": "L1", "name": "New"}
    (call,) = proxy.calls
    assert call["method"] == "PATCH"
    assert (
        call["endpoint"] == "https://gmail.googleapis.com/gmail/v1/users/me/labels/L1"
    )
    assert call["body"] == {"name": "New"}


def test_modifying_a_message_posts_its_label_changes(proxy, monkeypatch):
    proxy.answer("/messages/m1/modify", 200, {"id": "m1"})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(
        gmail.gmail_manage_labels(
            "modify_message",
            message_id="m1",
            add_label_ids=["STARRED"],
            remove_label_ids=["UNREAD"],
        )
    )

    assert result["message"] == {"id": "m1"}
    (call,) = proxy.calls
    assert call["method"] == "POST"
    assert call["endpoint"] == (
        "https://gmail.googleapis.com/gmail/v1/users/me/messages/m1/modify"
    )
    assert call["body"] == {"addLabelIds": ["STARRED"], "removeLabelIds": ["UNREAD"]}


def test_drafting_a_message_through_the_proxy(proxy, monkeypatch):
    proxy.answer("/drafts", 200, {"id": "draft-1"})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result = json.loads(
        gmail.gmail_send_messages(
            [{"to": "[email protected]", "subject": "Draft", "body": "later"}],
            action="draft",
        )
    )

    assert result["results"] == [
        {"status": "drafted", "id": "draft-1", "attachments": []}
    ]
    (call,) = proxy.calls
    assert call["endpoint"] == "https://gmail.googleapis.com/gmail/v1/users/me/drafts"
    assert "raw" in call["body"]["message"]


def test_an_error_that_echoes_the_message_hides_its_body_and_attachment(
    proxy, monkeypatch, tmp_path, caplog
):
    """The vendor can echo the refused request body; the base64 ``raw`` in it
    carries the mail body and attachment, and must not reach the tool result
    or the server log."""
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    attachment = tmp_path / "secret.txt"
    attachment.write_text("ATTACHMENT-SECRET", encoding="utf-8")
    leaked: dict[str, str] = {}

    def request(**kwargs: Any) -> _Response:
        raw = kwargs["json"]["body"]["raw"]
        leaked["raw"] = raw
        return _Response(
            400, {"error": {"message": f"invalid body {json.dumps({'raw': raw})}"}}
        )

    monkeypatch.setattr(composio_proxy.requests, "request", request)
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    with caplog.at_level(logging.ERROR, logger="gmail-mcp"):
        result = json.loads(
            gmail.gmail_send_messages(
                [
                    {
                        "to": "[email protected]",
                        "subject": "Secret",
                        "body": "BODY-SECRET",
                        "attachments": [str(attachment)],
                    }
                ],
                action="send",
            )
        )

    assert result["status"] == "error"
    assert leaked["raw"] not in result["message"]
    assert leaked["raw"] not in "\n".join(
        record.getMessage() for record in caplog.records
    )
    decoded = base64.urlsafe_b64decode(leaked["raw"])
    assert b"BODY-SECRET" in decoded
    # The attachment body is base64 inside the MIME message.
    assert base64.b64encode(b"ATTACHMENT-SECRET") in decoded


def test_an_error_that_echoes_the_decoded_message_hides_a_long_recipient_list(
    proxy, monkeypatch, tmp_path, caplog
):
    """A group send keeps the recipients on one long ``To:`` line. When the
    vendor echoes the decoded message instead of the request body, redacting
    that line used to shorten the text in front of the mail body and the
    attached file, which then reached the tool result and the server log."""
    monkeypatch.setenv("XAGENT_GMAIL_FILE_ALLOWED_DIRS", str(tmp_path))
    attachment_bytes = b"ATTACHMENT-SECRET-" + bytes(range(256)) * 20
    attachment = tmp_path / "secret.bin"
    attachment.write_bytes(attachment_bytes)
    recipients = ",".join(f"user{index:03d}@example.com" for index in range(70))
    body = "TOOL-BODY-MARKER this is the mail body"
    encoded_attachment = base64.b64encode(attachment_bytes).decode()
    attachment_lines = [
        encoded_attachment[index : index + 76]
        for index in range(0, len(encoded_attachment), 76)
    ]

    def request(**kwargs: Any) -> _Response:
        raw = kwargs["json"]["body"]["raw"]
        decoded = base64.urlsafe_b64decode(raw + "=" * (-len(raw) % 4)).decode()
        return _Response(400, {"error": {"message": f"invalid body {decoded}"}})

    monkeypatch.setattr(composio_proxy.requests, "request", request)
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    with caplog.at_level(logging.ERROR, logger="gmail-mcp"):
        result = json.loads(
            gmail.gmail_send_messages(
                [
                    {
                        "to": recipients,
                        "subject": "Group send",
                        "body": body,
                        "attachments": [str(attachment)],
                    }
                ],
                action="send",
            )
        )

    assert result["status"] == "error"
    seen = (
        result["message"]
        + "\n"
        + "\n".join(record.getMessage() for record in caplog.records)
    )
    assert body not in seen
    assert "[email protected]" not in seen
    assert "[email protected]" not in seen
    assert not [line for line in attachment_lines if line[:24] in seen]


def test_an_attachment_of_same_prefix_base64_lines_stays_bounded(
    monkeypatch, composio_env
):
    """The lookup's worst shape reached through the real message builder: a
    2.4 MB attachment (inside the documented passing boundary) whose base64
    lines all open with the same 32 characters, and a vendor that echoes only
    the first 100 decoded lines. Each matched line used to try all 42k
    fragments that shared its opening; the index must cost one lookup per
    distinct length instead."""
    attachment = b"".join(
        b"\x00" * 45 + number.to_bytes(12, "big") for number in range(42000)
    )
    create, _ = gmail._build_message(
        {"to": "[email protected]", "subject": "Many", "body": "hi"},
        [("blob.bin", attachment)],
    )
    raw = create["raw"]
    assert len(raw) < composio_proxy.MAX_JSON_BODY_BYTES
    decoded = base64.urlsafe_b64decode(raw + "=" * (-len(raw) % 4)).decode()
    lines = decoded.split("\n")
    base64_lines = [line for line in lines if len(line) == 76 and line[:32] == "A" * 32]
    assert len(base64_lines) > 40000
    echo = "invalid body " + "\n".join(lines[: len(lines) - len(base64_lines) + 100])
    monkeypatch.setattr(
        composio_proxy.requests,
        "request",
        lambda **kwargs: _Response(400, {"error": {"message": echo}}),
    )

    started = time.monotonic()
    with pytest.raises(composio_proxy.ComposioProxyError) as raised:
        composio_proxy.proxy_request(
            "POST",
            "https://gmail.googleapis.com/gmail/v1/users/me/messages/send",
            allowed_hosts=("gmail.googleapis.com",),
            body={"raw": raw},
        )
    elapsed = time.monotonic() - started

    text = str(raised.value)
    assert base64_lines[0][:24] not in text
    assert not [line for line in base64_lines if line in text]
    assert elapsed < 1.0, "a same-prefix bucket must be looked up by length"


def test_a_text_body_of_same_prefix_lines_stays_bounded(monkeypatch, composio_env):
    """The same shape as a plain 7bit text body through the real builder:
    30k lines of 76 characters that all open with the same 70, and a vendor
    that echoes the first 100 of them."""
    lines = ["X" * 70 + f"{number:06d}" for number in range(30000)]
    create, _ = gmail._build_message(
        {"to": "[email protected]", "subject": "Many", "body": "\r\n".join(lines)}, []
    )
    raw = create["raw"]
    assert len(raw) < composio_proxy.MAX_JSON_BODY_BYTES
    decoded_lines = (
        base64.urlsafe_b64decode(raw + "=" * (-len(raw) % 4)).decode().split("\n")
    )
    echo = "invalid body " + "\n".join(
        decoded_lines[: len(decoded_lines) - len(lines) + 100]
    )
    monkeypatch.setattr(
        composio_proxy.requests,
        "request",
        lambda **kwargs: _Response(400, {"error": {"message": echo}}),
    )

    started = time.monotonic()
    with pytest.raises(composio_proxy.ComposioProxyError) as raised:
        composio_proxy.proxy_request(
            "POST",
            "https://gmail.googleapis.com/gmail/v1/users/me/messages/send",
            allowed_hosts=("gmail.googleapis.com",),
            body={"raw": raw},
        )
    elapsed = time.monotonic() - started

    text = str(raised.value)
    assert lines[0][:20] not in text
    assert not [line for line in lines if line in text]
    assert elapsed < 1.0, "a same-prefix bucket must be looked up by length"


def test_a_builder_body_echoed_with_crlf_stays_bounded(monkeypatch, composio_env):
    """The shape a real send can reach: one recipient and a 3.24 MB 7-bit body
    of 41k 78-character lines, whose whole decoded echo the vendor rewrites
    with CRLF line endings. The body fragment fits at every position near its
    start, so the pass hashed the whole body at each of them (3.5 s here,
    while the previous lookup took 0.05 s on the same output). The tail probe
    has to make the mismatch cost one slice, and no line of the body or the
    recipient may survive."""
    create, _ = gmail._build_message(
        {
            "to": "[email protected]",
            "subject": "s",
            "body": "\n".join(["A" * 78] * 41000),
        },
        [],
    )
    raw = create["raw"]
    assert len(raw) < composio_proxy.MAX_JSON_BODY_BYTES
    decoded = base64.urlsafe_b64decode(raw + "=" * (-len(raw) % 4)).decode()
    echo = decoded.replace("\n", "\r\n")
    monkeypatch.setattr(
        composio_proxy.requests,
        "request",
        lambda **kwargs: _Response(400, {"error": {"message": "invalid body " + echo}}),
    )

    started = time.monotonic()
    with pytest.raises(composio_proxy.ComposioProxyError) as raised:
        composio_proxy.proxy_request(
            "POST",
            "https://gmail.googleapis.com/gmail/v1/users/me/messages/send",
            allowed_hosts=("gmail.googleapis.com",),
            body={"raw": raw},
        )
    elapsed = time.monotonic() - started

    text = str(raised.value)
    assert "A" * 78 not in text
    assert "[email protected]" not in text
    assert elapsed < 1.0, "a body fragment must not be hashed at every position"


@pytest.mark.parametrize("rewrite", ["double-blank", "indent"])
def test_a_group_send_echoed_with_rewritten_whitespace_stays_bounded(
    monkeypatch, composio_env, rewrite
):
    """The same message with 500 recipients (a 40 kB ``To`` header) and an echo
    that doubles every blank line or indents every line. Both rewrites add a
    character per line to the echo, which keeps the 3.24 MB body fragment a
    candidate for the first lines after its start; the pass then paid for the
    whole body at each of them. Neither the body nor the recipient list may
    survive, and the pass stays bounded."""
    recipients = ",".join(
        "A" * 64 + f"{number:04d}@example.com" for number in range(500)
    )
    create, _ = gmail._build_message(
        {"to": recipients, "subject": "s", "body": "\n".join(["A" * 78] * 41000)},
        [],
    )
    raw = create["raw"]
    assert len(raw) < composio_proxy.MAX_JSON_BODY_BYTES
    decoded = base64.urlsafe_b64decode(raw + "=" * (-len(raw) % 4)).decode()
    echo = {
        "double-blank": decoded.replace("\n", "\n\n"),
        "indent": "\n".join("  " + line for line in decoded.split("\n")),
    }[rewrite]
    monkeypatch.setattr(
        composio_proxy.requests,
        "request",
        lambda **kwargs: _Response(400, {"error": {"message": "invalid body " + echo}}),
    )

    started = time.monotonic()
    with pytest.raises(composio_proxy.ComposioProxyError) as raised:
        composio_proxy.proxy_request(
            "POST",
            "https://gmail.googleapis.com/gmail/v1/users/me/messages/send",
            allowed_hosts=("gmail.googleapis.com",),
            body={"raw": raw},
        )
    elapsed = time.monotonic() - started

    text = str(raised.value)
    assert "A" * 78 not in text
    assert "A" * 64 not in text
    assert "@example.com" not in text
    assert elapsed < 1.0, "a rewritten echo must not multiply the body's cost"


# --- Metering: one usage row per Composio request ----------------------------


def _gmail_metering_adapter() -> MCPToolAdapter:
    return MCPToolAdapter(
        mcp_tool=SimpleNamespace(
            name="gmail_search_messages",
            description="Search Gmail messages",
            inputSchema={
                "type": "object",
                "properties": {
                    "max_results": {"type": "integer"},
                    "label_ids": {"type": "array", "items": {"type": "string"}},
                },
            },
        ),
        connection={
            "transport": "stdio",
            "command": "python",
            "args": [],
            "composio": True,
        },
        name_prefix="mcp_Gmail_",
        source_server="gmail",
    )


def _session_running_gmail():
    """A session whose calls run the real Gmail MCP tool in this process."""

    class _GmailSession:
        async def initialize(self):
            return None

        async def call_tool(self, name, arguments, **kwargs):
            return await gmail.mcp.call_tool(name, arguments)

    @asynccontextmanager
    async def _fake_create_session(_connection):
        yield _GmailSession()

    return _fake_create_session


def _answer_search(proxy, message_ids: list[str]) -> None:
    """Answer one list request and one metadata read per message id."""
    for message_id in message_ids:
        proxy.answer(
            f"/messages/{message_id}",
            200,
            {**_MESSAGE, "id": message_id, "threadId": f"t-{message_id}"},
        )
    proxy.answer("/messages", 200, {"messages": [{"id": m} for m in message_ids]})


async def _search_through_the_adapter(monkeypatch, arguments: dict[str, Any]):
    monkeypatch.setattr(mcp_adapter_module, "create_session", _session_running_gmail())
    adapter = _gmail_metering_adapter()
    with TokenContextManager() as manager:
        result = await adapter.run_json_async(arguments)
        details = manager.get_usage().details
    return result, details


@pytest.mark.asyncio
async def test_the_gmail_result_reports_the_requests_composio_accepted(
    proxy, monkeypatch
):
    _answer_search(proxy, ["msg0", "msg1"])
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    from mcp.shared.memory import create_connected_server_and_client_session

    async with create_connected_server_and_client_session(
        gmail.mcp._mcp_server
    ) as session:
        result = await session.call_tool("gmail_search_messages", {"max_results": 2})

    assert isinstance(result, CallToolResult)
    assert result.meta == {COMPOSIO_REQUESTS_META_KEY: 3}
    assert len(proxy.calls) == 3


@pytest.mark.asyncio
async def test_a_ten_message_search_records_its_eleven_requests(proxy, monkeypatch):
    # One list plus one metadata read per message: eleven billed requests.
    message_ids = [f"msg{number}" for number in range(10)]
    _answer_search(proxy, message_ids)
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result, details = await _search_through_the_adapter(
        monkeypatch, {"max_results": 10}
    )

    assert result["is_error"] is False
    assert len(proxy.calls) == 11
    assert len(details) == 11
    assert {row["call_type"] for row in details} == {"composio"}
    assert {row["unit"] for row in details} == {"requests"}
    assert {row["quantity"] for row in details} == {1.0}
    assert {row["model_id"] for row in details} == {"platform/composio"}


@pytest.mark.asyncio
async def test_a_composio_refusal_records_nothing(proxy, monkeypatch):
    proxy.answer_with(500, {"error": {"message": "rate limited"}})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result, details = await _search_through_the_adapter(
        monkeypatch, {"max_results": 10}
    )

    assert json.loads(result["content"][0]["text"])["status"] == "error"
    assert len(proxy.calls) == 1
    assert details == []


@pytest.mark.asyncio
async def test_a_search_refused_before_sending_records_nothing(proxy, monkeypatch):
    # Two label ids are refused on a Composio grant before the list request.
    proxy.answer("/messages", 200, {"messages": []})
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result, details = await _search_through_the_adapter(
        monkeypatch, {"max_results": 1, "label_ids": ["INBOX", "STARRED"]}
    )

    assert json.loads(result["content"][0]["text"])["status"] == "error"
    assert proxy.calls == []
    assert details == []


@pytest.mark.asyncio
async def test_a_partly_failed_batch_records_only_the_accepted_requests(
    proxy, monkeypatch
):
    # msg0 is read, msg1 answers 404 (Composio accepted and relayed it), and
    # msg2 is refused by Composio itself. The list and the first two reads are
    # billed; the refused read is not.
    proxy.answer(
        "/messages/msg0", 200, {**_MESSAGE, "id": "msg0", "threadId": "t-msg0"}
    )
    proxy.answer("/messages/msg1", 404, _NOT_FOUND)
    proxy.by_endpoint.append(
        ("/messages/msg2", _Response(500, {"error": {"message": "rate limited"}}))
    )
    proxy.answer(
        "/messages",
        200,
        {"messages": [{"id": "msg0"}, {"id": "msg1"}, {"id": "msg2"}]},
    )
    monkeypatch.setattr(gmail, "get_gmail_service", lambda: _proxy_service())

    result, details = await _search_through_the_adapter(monkeypatch, {"max_results": 3})

    payload = json.loads(result["content"][0]["text"])
    assert len(payload["errors"]) == 2
    assert len(proxy.calls) == 4
    assert len(details) == 3