Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/copilot): give the sandbox the credential the connect card shows as connected (#14662)

Bently26ce23dda9d0
diff --git a/autogpt_platform/backend/backend/copilot/integration_creds.py b/autogpt_platform/backend/backend/copilot/integration_creds.py
index 1b3ca44821..9b747f9aba 100644
--- a/autogpt_platform/backend/backend/copilot/integration_creds.py
+++ b/autogpt_platform/backend/backend/copilot/integration_creds.py
@@ -23,6 +23,7 @@ creds-changed bus does. See ``_ensure_cache_invalidation_listener``.
import asyncio
import logging
import threading
+from collections.abc import Mapping
from typing import cast
from cachetools import TTLCache
@@ -34,6 +35,7 @@ from backend.integrations.creds_manager import (
IntegrationCredentialsManager,
register_creds_changed_hook,
)
+from backend.integrations.providers import ProviderName
from backend.util.retry import continuous_retry
logger = logging.getLogger(__name__)
@@ -90,13 +92,21 @@ class _LockedTTLCache(TTLCache):
with self._lock:
return super().popitem()
+ def pop_prefix(self, prefix: tuple) -> None:
+ """Drop every entry whose key starts with *prefix*."""
+ with self._lock:
+ for key in [k for k in self.keys() if k[: len(prefix)] == prefix]:
+ super().pop(key, None)
+
-# (user_id, provider) → token string. TTLCache handles expiry + eviction.
-_token_cache: TTLCache[tuple[str, str], str] = _LockedTTLCache(
+# (user_id, provider) → token string, or (user_id, provider, required_scopes)
+# when the caller asked for specific scopes. TTLCache handles expiry + eviction.
+_CacheKey = tuple[str, str] | tuple[str, str, frozenset[str]]
+_token_cache: _LockedTTLCache = _LockedTTLCache(
maxsize=_CACHE_MAX_SIZE, ttl=_TOKEN_CACHE_TTL
)
# Separate cache for "no credentials" results with a shorter TTL.
-_null_cache: TTLCache[tuple[str, str], bool] = _LockedTTLCache(
+_null_cache: _LockedTTLCache = _LockedTTLCache(
maxsize=_CACHE_MAX_SIZE, ttl=_NULL_CACHE_TTL
)
@@ -111,6 +121,21 @@ _gh_identity_null_cache: TTLCache[str, bool] = _LockedTTLCache(
)
+def _canonical_provider(provider: str) -> str:
+ """``"ProviderName.GITHUB"`` -> ``"github"``.
+
+ Credentials persisted under Python 3.13's ``str(StrEnum)`` carry the enum's
+ repr as their provider, and change events pass it on as stored, while
+ lookups (and so cache keys) use the canonical value.
+ """
+ if provider.startswith("ProviderName."):
+ try:
+ return ProviderName[provider.removeprefix("ProviderName.")].value
+ except KeyError:
+ pass
+ return provider
+
+
def invalidate_user_provider_cache(user_id: str, provider: str) -> None:
"""Remove the cached entry for *user_id*/*provider* from both caches.
@@ -122,9 +147,10 @@ def invalidate_user_provider_cache(user_id: str, provider: str) -> None:
``get_github_user_git_identity()`` re-fetches the user's profile on
the next call instead of serving stale identity data.
"""
- key = (user_id, provider)
- _token_cache.pop(key, None)
- _null_cache.pop(key, None)
+ provider = _canonical_provider(provider)
+ # Every scope-specific entry for this pair is stale too.
+ _token_cache.pop_prefix((user_id, provider))
+ _null_cache.pop_prefix((user_id, provider))
if provider == "github":
_gh_identity_cache.pop(user_id, None)
@@ -145,15 +171,25 @@ except RuntimeError:
_manager = IntegrationCredentialsManager()
-async def get_provider_token(user_id: str, provider: str) -> str | None:
+def _cache_key(user_id: str, provider: str, required: frozenset[str]) -> _CacheKey:
+ return (user_id, provider, required) if required else (user_id, provider)
+
+
+async def get_provider_token(
+ user_id: str, provider: str, required_scopes: frozenset[str] = frozenset()
+) -> str | None:
"""Return the user's access token for *provider*, or ``None`` if not connected.
OAuth2 tokens are preferred (refreshed if needed); API keys are the fallback.
+ Among several OAuth2 credentials, one granting every scope in
+ *required_scopes* wins: that is the credential the connect card shows as
+ connected, so injecting any other would send the model back to a card that
+ already says "Connected".
Both found tokens and "not connected" results are cached for 60 s, and a
credential write in any process evicts the entry before that lapses.
"""
_ensure_cache_invalidation_listener()
- cache_key = (user_id, provider)
+ cache_key = _cache_key(user_id, provider, required_scopes)
if cache_key in _null_cache:
return None
@@ -173,20 +209,26 @@ async def get_provider_token(user_id: str, provider: str) -> str | None:
return None
# Pass 1: prefer OAuth2 (carry scope info, refreshable via token endpoint).
- # Sort so broader-scoped tokens come first: a token with "repo" scope covers
- # full git access, while a public-data-only token lacks push/pull permission.
+ # Credentials covering the requested scopes come first, then ones with
+ # "repo" (full git access, where a public-data-only token lacks push/pull).
+ # The sort is stable, so ties keep their stored order, as the card does.
# lock=False — background injection; not worth a distributed lock acquisition.
+ def rank(creds: OAuth2Credentials) -> tuple[int, int]:
+ granted = set(creds.scopes or [])
+ return (
+ 0 if required_scopes <= granted else 1,
+ 0 if "repo" in granted else 1,
+ )
+
oauth2_creds = sorted(
- [c for c in creds_list if c.type == "oauth2"],
- key=lambda c: 0 if "repo" in (cast(OAuth2Credentials, c).scopes or []) else 1,
+ [cast(OAuth2Credentials, c) for c in creds_list if c.type == "oauth2"],
+ key=rank,
)
refresh_failed = False
for creds in oauth2_creds:
if creds.type == "oauth2":
try:
- fresh = await manager.refresh_if_needed(
- user_id, cast(OAuth2Credentials, creds), lock=False
- )
+ fresh = await manager.refresh_if_needed(user_id, creds, lock=False)
token = fresh.access_token.get_secret_value()
except Exception:
logger.warning(
@@ -252,16 +294,20 @@ _listener_start_lock = threading.Lock()
_listener_thread: threading.Thread | None = None
-async def get_integration_env_vars(user_id: str) -> dict[str, str]:
+async def get_integration_env_vars(
+ user_id: str, required_scopes: Mapping[str, frozenset[str]] | None = None
+) -> dict[str, str]:
"""Return env vars for all providers the user has connected.
Iterates :data:`PROVIDER_ENV_VARS`, fetches each token, and builds a flat
``{env_var: token}`` dict ready to pass to a subprocess or E2B sandbox.
Only providers with a stored credential contribute entries.
+ *required_scopes* maps a provider to the scopes its token should carry.
"""
env: dict[str, str] = {}
for provider, var_names in PROVIDER_ENV_VARS.items():
- token = await get_provider_token(user_id, provider)
+ scopes = (required_scopes or {}).get(provider, frozenset())
+ token = await get_provider_token(user_id, provider, scopes)
if token:
for var in var_names:
env[var] = token
diff --git a/autogpt_platform/backend/backend/copilot/integration_creds_test.py b/autogpt_platform/backend/backend/copilot/integration_creds_test.py
index 220be870e3..00bb47c567 100644
--- a/autogpt_platform/backend/backend/copilot/integration_creds_test.py
+++ b/autogpt_platform/backend/backend/copilot/integration_creds_test.py
@@ -235,6 +235,103 @@ class TestGetProviderToken:
assert _TOKEN_CACHE_TTL <= 60.0
+def _github_oauth(creds_id: str, token: str, scopes: list[str]) -> OAuth2Credentials:
+ return OAuth2Credentials(
+ id=creds_id,
+ provider=_PROVIDER,
+ title="GitHub",
+ username="Otto-AGPT",
+ access_token=SecretStr(token),
+ refresh_token=None,
+ access_token_expires_at=None,
+ refresh_token_expires_at=None,
+ scopes=scopes,
+ )
+
+
+class TestRequiredScopes:
+ """Reproduces the reconnect loop: an older credential for the same account
+ lacks a scope, a newer one has it. The connect card shows the newer one as
+ connected, so the sandbox must be handed the newer one too."""
+
+ older = _github_oauth("older", "tok-older", ["repo", "workflow"])
+ newer = _github_oauth("newer", "tok-newer", ["repo", "read:org"])
+
+ def _manager(self, creds: list) -> MagicMock:
+ manager = MagicMock()
+ manager.store.get_creds_by_provider = AsyncMock(return_value=creds)
+ manager.refresh_if_needed = AsyncMock(side_effect=lambda _u, c, **_k: c)
+ return manager
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_credential_covering_the_requested_scopes_wins(self):
+ manager = self._manager([self.older, self.newer])
+ with patch("backend.copilot.integration_creds._manager", manager):
+ token = await get_provider_token(
+ _USER, _PROVIDER, frozenset({"repo", "read:org"})
+ )
+ assert token == "tok-newer"
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_without_requested_scopes_stored_order_still_decides(self):
+ manager = self._manager([self.older, self.newer])
+ with patch("backend.copilot.integration_creds._manager", manager):
+ token = await get_provider_token(_USER, _PROVIDER)
+ assert token == "tok-older"
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_no_credential_covers_them_falls_back_to_repo_first(self):
+ public = _github_oauth("public", "tok-public", ["read:user"])
+ manager = self._manager([public, self.older])
+ with patch("backend.copilot.integration_creds._manager", manager):
+ token = await get_provider_token(
+ _USER, _PROVIDER, frozenset({"repo", "admin:org"})
+ )
+ assert token == "tok-older"
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_a_cached_unscoped_token_is_not_served_for_a_scoped_ask(self):
+ _token_cache[(_USER, _PROVIDER)] = "tok-older"
+ manager = self._manager([self.older, self.newer])
+ with patch("backend.copilot.integration_creds._manager", manager):
+ token = await get_provider_token(
+ _USER, _PROVIDER, frozenset({"repo", "read:org"})
+ )
+ assert token == "tok-newer"
+
+ def test_invalidation_accepts_the_enum_repr_a_stored_credential_may_carry(self):
+ # Rows written under Python 3.13's str(StrEnum) have this provider, and
+ # change events pass it on as stored; cache keys use the canonical one.
+ scoped = (_USER, _PROVIDER, frozenset({"repo"}))
+ _token_cache[(_USER, _PROVIDER)] = "tok"
+ _token_cache[scoped] = "tok"
+ _gh_identity_cache[_USER] = {"GIT_AUTHOR_NAME": "x"}
+ invalidate_user_provider_cache(_USER, "ProviderName.GITHUB")
+ assert (_USER, _PROVIDER) not in _token_cache
+ assert scoped not in _token_cache
+ assert _USER not in _gh_identity_cache
+
+ def test_invalidation_drops_scoped_entries_too(self):
+ scoped = (_USER, _PROVIDER, frozenset({"repo", "read:org"}))
+ _token_cache[scoped] = "tok"
+ _null_cache[scoped] = True
+ _token_cache[("other-user", _PROVIDER, frozenset({"repo"}))] = "keep"
+ invalidate_user_provider_cache(_USER, _PROVIDER)
+ assert scoped not in _token_cache
+ assert scoped not in _null_cache
+ assert ("other-user", _PROVIDER, frozenset({"repo"})) in _token_cache
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_env_vars_use_the_scopes_asked_for_per_provider(self):
+ manager = self._manager([self.older, self.newer])
+ with patch("backend.copilot.integration_creds._manager", manager):
+ env = await get_integration_env_vars(
+ _USER, {"github": frozenset({"repo", "read:org"})}
+ )
+ assert env["GH_TOKEN"] == "tok-newer"
+ assert env["GITHUB_TOKEN"] == "tok-newer"
+
+
class TestThreadSafetyLocks:
"""Bug reproduction: shared AsyncRedisKeyedMutex across threads caused
'Future attached to a different loop' when copilot workers accessed
diff --git a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py b/autogpt_platform/backend/backend/copilot/tools/bash_exec.py
index 0228f65d54..bb8b99400f 100644
--- a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py
+++ b/autogpt_platform/backend/backend/copilot/tools/bash_exec.py
@@ -34,6 +34,7 @@ from backend.copilot.integration_creds import (
from backend.copilot.model import ChatSession
from .base import BaseTool
+from .connect_integration import requested_scopes
from .models import BashExecResponse, ErrorResponse, ToolResponseBase
from .sandbox import get_workspace_dir, has_full_sandbox, run_sandboxed
@@ -147,7 +148,12 @@ class BashExecTool(BaseTool):
sandbox = get_current_sandbox()
if sandbox is not None:
return await self._execute_on_e2b(
- sandbox, command, timeout, session_id, user_id
+ sandbox,
+ command,
+ timeout,
+ session_id,
+ user_id,
+ required_scopes=requested_scopes(session),
)
# Bubblewrap fallback: local isolated execution.
@@ -186,6 +192,7 @@ class BashExecTool(BaseTool):
timeout: int,
session_id: str | None,
user_id: str | None = None,
+ required_scopes: dict[str, frozenset[str]] | None = None,
) -> ToolResponseBase:
"""Execute *command* on the E2B sandbox via commands.run().
@@ -199,7 +206,7 @@ class BashExecTool(BaseTool):
# Collect injected secret values so we can scrub them from output.
secret_values: list[str] = []
if user_id is not None:
- integration_env = await get_integration_env_vars(user_id)
+ integration_env = await get_integration_env_vars(user_id, required_scopes)
secret_values = [v for v in integration_env.values() if v]
envs.update(integration_env)
diff --git a/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py b/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py
index 3b64116a0e..51412890a2 100644
--- a/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py
@@ -54,7 +54,7 @@ class TestBashExecE2BTokenInjection:
user_id=_USER,
)
- mock_get_env.assert_awaited_once_with(_USER)
+ mock_get_env.assert_awaited_once_with(_USER, None)
call_kwargs = sandbox.commands.run.call_args[1]
assert call_kwargs["envs"]["GH_TOKEN"] == "gh-secret"
assert call_kwargs["envs"]["GITHUB_TOKEN"] == "gh-secret"
diff --git a/autogpt_platform/backend/backend/copilot/tools/connect_integration.py b/autogpt_platform/backend/backend/copilot/tools/connect_integration.py
index 142c63840f..98a3a2968d 100644
--- a/autogpt_platform/backend/backend/copilot/tools/connect_integration.py
+++ b/autogpt_platform/backend/backend/copilot/tools/connect_integration.py
@@ -6,6 +6,7 @@ setup card in the chat — the same UI that appears when a GitHub block runs
without configured credentials.
"""
+import json
from typing import Any, cast
from backend.copilot.model import ChatSession
@@ -25,13 +26,52 @@ from backend.integrations.providers import ProviderName
from .base import BaseTool
from .expert_scope import annotate_expert_grants
+CONNECT_INTEGRATION_TOOL = "connect_integration"
+
+
+def _merged_scopes(provider: str, extra: list[str]) -> frozenset[str]:
+ entry = SUPPORTED_PROVIDERS.get(provider)
+ defaults = entry["default_scopes"] if entry else []
+ return frozenset(s for s in (*defaults, *extra) if s)
+
+
+def requested_scopes(session: ChatSession | None) -> dict[str, frozenset[str]]:
+ """The scopes this session's latest connect card asked for, per provider.
+
+ The card counts an account as connected only when it grants every one of
+ these, so whatever gets injected into the sandbox has to be chosen by the
+ same rule. Read from the transcript, which already survives every turn.
+ """
+ found: dict[str, frozenset[str]] = {}
+ for message in reversed(session.messages if session else []):
+ for call in reversed(message.tool_calls or []):
+ function = call.get("function") or {}
+ name = str(function.get("name") or call.get("name") or "")
+ if name.rsplit("__", 1)[-1] != CONNECT_INTEGRATION_TOOL:
+ continue
+ # Both transcript shapes: nested under "function", or flat.
+ raw = function.get("arguments") or call.get("arguments") or "{}"
+ try:
+ args = raw if isinstance(raw, dict) else json.loads(raw)
+ except (TypeError, ValueError):
+ continue
+ if not isinstance(args, dict):
+ continue
+ provider = str(args.get("provider") or "").strip().lower()
+ if not provider or provider in found:
+ continue
+ scopes = args.get("scopes")
+ extra = [str(x).strip() for x in scopes] if isinstance(scopes, list) else []
+ found[provider] = _merged_scopes(provider, extra)
+ return found
+
class ConnectIntegrationTool(BaseTool):
"""Surface the credentials setup UI when an integration is not connected."""
@property
def name(self) -> str:
- return "connect_integration"
+ return CONNECT_INTEGRATION_TOOL
@property
def description(self) -> str:
diff --git a/autogpt_platform/backend/backend/copilot/tools/connect_integration_test.py b/autogpt_platform/backend/backend/copilot/tools/connect_integration_test.py
index cf17f4cc79..5347957a9c 100644
--- a/autogpt_platform/backend/backend/copilot/tools/connect_integration_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/connect_integration_test.py
@@ -2,8 +2,10 @@
import pytest
+from backend.copilot.model import ChatMessage
+
from ._test_data import make_session
-from .connect_integration import ConnectIntegrationTool
+from .connect_integration import ConnectIntegrationTool, requested_scopes
from .models import ErrorResponse, SetupRequirementsResponse
_TEST_USER_ID = "test-user-connect-integration"
@@ -133,3 +135,78 @@ class TestConnectIntegrationTool:
output = json.loads(raw) if isinstance(raw, str) else raw
assert output.get("type") == "need_login"
assert result.success is False
+
+
+def _connect_call(arguments: str, name: str = "connect_integration") -> ChatMessage:
+ return ChatMessage(
+ role="assistant",
+ content="",
+ tool_calls=[
+ {
+ "id": "call-1",
+ "type": "function",
+ "function": {"name": name, "arguments": arguments},
+ }
+ ],
+ )
+
+
+class TestRequestedScopes:
+ def test_no_connect_call_asks_for_nothing(self):
+ session = make_session("user-1", guide_read=False, library_check=False)
+ assert requested_scopes(session) == {}
+ assert requested_scopes(None) == {}
+
+ def test_scopes_include_the_provider_defaults(self):
+ # The card is built from defaults + requested, so the sandbox must be too.
+ session = make_session("user-1", guide_read=False, library_check=False)
+ session.messages.append(
+ _connect_call('{"provider": "github", "scopes": ["read:org"]}')
+ )
+ assert requested_scopes(session) == {"github": frozenset({"repo", "read:org"})}
+
+ def test_latest_card_wins(self):
+ session = make_session("user-1", guide_read=False, library_check=False)
+ session.messages.append(
+ _connect_call('{"provider": "github", "scopes": ["workflow"]}')
+ )
+ session.messages.append(
+ _connect_call('{"provider": "GitHub", "scopes": ["read:org"]}')
+ )
+ assert requested_scopes(session) == {"github": frozenset({"repo", "read:org"})}
+
+ def test_sdk_prefixed_tool_name_counts(self):
+ session = make_session("user-1", guide_read=False, library_check=False)
+ session.messages.append(
+ _connect_call(
+ '{"provider": "github", "scopes": ["read:org"]}',
+ name="mcp__copilot__connect_integration",
+ )
+ )
+ assert requested_scopes(session)["github"] == {"repo", "read:org"}
+
+ def test_unreadable_arguments_are_skipped(self):
+ session = make_session("user-1", guide_read=False, library_check=False)
+ session.messages.append(
+ _connect_call('{"provider": "github", "scopes": ["read:org"]}')
+ )
+ session.messages.append(_connect_call("{not json"))
+ assert requested_scopes(session) == {"github": frozenset({"repo", "read:org"})}
+
+ def test_flat_tool_call_shape_counts(self):
+ # Some transcripts keep name and arguments at the top level.
+ session = make_session("user-1", guide_read=False, library_check=False)
+ session.messages.append(
+ ChatMessage(
+ role="assistant",
+ content="",
+ tool_calls=[
+ {
+ "id": "call-1",
+ "name": "connect_integration",
+ "arguments": {"provider": "github", "scopes": ["read:org"]},
+ }
+ ],
+ )
+ )
+ assert requested_scopes(session) == {"github": frozenset({"repo", "read:org"})}