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.
feat(backend): hold each box to the run's ceiling on connected accounts (#14872)
Krzysztof Czerwinski0bba9813b537
diff --git a/autogpt_platform/backend/backend/blocks/autopilot.py b/autogpt_platform/backend/backend/blocks/autopilot.pyindex 66b9aaf7ec..10da847221 100644--- a/autogpt_platform/backend/backend/blocks/autopilot.py+++ b/autogpt_platform/backend/backend/blocks/autopilot.py@@ -36,6 +36,7 @@ from backend.copilot.permissions import (all_known_tool_names,validate_block_identifiers,)+from backend.copilot.providers import SUPPORTED_PROVIDERSfrom backend.data.model import CredentialsField, CredentialsMetaInput, SchemaFieldfrom backend.integrations.providers import ProviderNamefrom backend.util.exceptions import BlockExecutionError@@ -269,6 +270,30 @@ class AutoPilotBlock(Block):advanced=True,)+ providers: list[str] = SchemaField(+ description=(+ "Connected-account providers (e.g. 'github') whose accounts "+ "the run may use: from code in the sandbox, and through blocks "+ "that act with that provider's credentials. Works with "+ "providers_exclude. Leave empty to apply no provider filter."+ ),+ default=[],+ advanced=True,+ )++ providers_exclude: bool = SchemaField(+ description=(+ "Controls how the 'providers' list is interpreted. "+ "True (default): 'providers' is a deny-list — listed providers "+ "are blocked, all others are allowed. An empty 'providers' list "+ "means allow everything. False: 'providers' is an allow-list — "+ "of the supported connected-account providers, only listed "+ "ones are permitted. Other providers are not covered."+ ),+ default=True,+ advanced=True,+ )+dry_run: bool = SchemaField(description=("When enabled, run_capability and run_agent tool calls in this "@@ -834,11 +859,22 @@ async def _build_and_validate_permissions("You may also use the first 8 characters of a block UUID.")+ unknown_providers = [+ p for p in input_data.providers if p not in SUPPORTED_PROVIDERS+ ]+ if unknown_providers:+ return (+ f"Unknown provider(s) in 'providers': {unknown_providers}. "+ f"Known providers: {sorted(SUPPORTED_PROVIDERS)}."+ )+return CopilotPermissions(tools=list(input_data.tools),tools_exclude=input_data.tools_exclude,blocks=input_data.blocks,blocks_exclude=input_data.blocks_exclude,+ providers=list(input_data.providers),+ providers_exclude=input_data.providers_exclude,)diff --git a/autogpt_platform/backend/backend/blocks/autopilot_permissions_test.py b/autogpt_platform/backend/backend/blocks/autopilot_permissions_test.pyindex faf6e7d7a8..fbe330ae58 100644--- a/autogpt_platform/backend/backend/blocks/autopilot_permissions_test.py+++ b/autogpt_platform/backend/backend/blocks/autopilot_permissions_test.py@@ -56,6 +56,19 @@ class TestBuildAndValidatePermissions:assert result.tools == ["run_block", "web_fetch"]assert result.tools_exclude is True+ async def test_providers_are_carried(self):+ inp = _make_input(providers=["github"], providers_exclude=False)+ result = await _build_and_validate_permissions(inp)+ assert isinstance(result, CopilotPermissions)+ assert result.providers == ["github"]+ assert result.providers_exclude is False++ async def test_an_unknown_provider_is_refused(self):+ inp = _make_input(providers=["gitlab"])+ result = await _build_and_validate_permissions(inp)+ assert isinstance(result, str)+ assert "gitlab" in result+async def test_invalid_tool_rejected_by_pydantic(self):"""Invalid tool names are now caught at Pydantic validation time(Literal type), before ``_build_and_validate_permissions`` is called."""diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex 50a2f1f768..83fac53788 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -89,7 +89,7 @@ from backend.copilot.pending_messages import (drain_pending_messages,format_pending_as_user_message,)-from backend.copilot.permissions import denied_tool_names+from backend.copilot.permissions import allowed_providers, denied_tool_namesfrom backend.copilot.prompting import (SHARED_TOOL_NOTES,approval_mode_supplement,@@ -97,6 +97,7 @@ from backend.copilot.prompting import (get_delegation_supplement,get_expert_oversight_supplement,get_graphiti_supplement,+ get_provider_ceiling_supplement,get_team_building_supplement,)from backend.copilot.provider_failure import classify as classify_provider_failure@@ -149,6 +150,7 @@ from backend.copilot.tools import ()from backend.copilot.tools.e2b_sandbox import (count_expert_turn,+ forget_turn_ceiling,get_or_create_sandbox,pause_sandbox_direct,)@@ -1899,6 +1901,7 @@ async def stream_chat_completion_baseline(volume_mounts=workspace_volume_mounts(user_id, session.expert_id),expert_id=session.expert_id,user_id=user_id,+ providers=allowed_providers(permissions),# Counted just before the try/finally that releases it, below:# everything between here and there can still fail or be# stopped, and a count with no release keeps the expert's box@@ -1907,6 +1910,7 @@ async def stream_chat_completion_baseline()except Exception:logger.warning("[Baseline] E2B sandbox setup failed", exc_info=True)+ await forget_turn_ceiling(session_id, session.expert_id)# --- Transcript support (feature parity with SDK path) ---transcript_builder = TranscriptBuilder()@@ -2008,6 +2012,8 @@ async def stream_chat_completion_baseline(system_prompt = (base_system_prompt+ SHARED_TOOL_NOTES+ # Empty unless the run's permissions restrict connected accounts.+ + get_provider_ceiling_supplement(permissions)+ delegation_supplement+ oversight_supplement+ team_building_supplementdiff --git a/autogpt_platform/backend/backend/copilot/computer.py b/autogpt_platform/backend/backend/copilot/computer.pyindex cbf6164122..4b371c8ad9 100644--- a/autogpt_platform/backend/backend/copilot/computer.py+++ b/autogpt_platform/backend/backend/copilot/computer.py@@ -40,7 +40,9 @@ from backend.blocks.desktop._common import ()from backend.copilot.sdk.env import config as chat_configfrom backend.copilot.tools.e2b_sandbox import (+ KEEP_CEILING,METADATA_MOUNTS,+ Ceiling,SandboxLookupError,SandboxOwner,get_or_create_owner_sandbox,@@ -140,6 +142,7 @@ async def open_desktop(*,user_id: Optional[str],session_id: Optional[str] = None,+ providers: Ceiling = KEEP_CEILING,) -> tuple[DesktopStream, bool, bool]:"""Turn the screen on in the owner's box; return ``(stream, first_time, shared)``.@@ -149,6 +152,8 @@ async def open_desktop(an idle box opened from the UI is paused by the lifecycle timeout instead.*user_id* is who the stream link is issued to (see ``_owner_bound``) and,with *session_id*, provenance stamped on a newly created box.+ *providers* is the turn's ceiling on connected accounts when a turn opens+ it (``start_desktop``); the UI has none and keeps the box's.One opener at a time per owner: the panel's button and the model's``start_desktop`` (or two tabs) would otherwise race to start a second@@ -169,7 +174,12 @@ async def open_desktop(try:return await asyncio.wait_for(_open_desktop_locked(- owner, mounts, api_key, user_id=user_id, session_id=session_id+ owner,+ mounts,+ api_key,+ user_id=user_id,+ session_id=session_id,+ providers=providers,),timeout=_DESKTOP_OPEN_DEADLINE_SECONDS,)@@ -184,6 +194,7 @@ async def _open_desktop_locked(*,user_id: str,session_id: Optional[str],+ providers: Ceiling = KEEP_CEILING,) -> tuple[DesktopStream, bool, bool]:sandbox = await get_or_create_owner_sandbox(owner,@@ -195,6 +206,7 @@ async def _open_desktop_locked(user_id=user_id,session_id=session_id,count_turn=False,+ providers=providers,)was_on = await screen_is_on(owner, sandbox.sandbox_id)desktop = DesktopSession(sandbox)diff --git a/autogpt_platform/backend/backend/copilot/computer_test.py b/autogpt_platform/backend/backend/copilot/computer_test.pyindex 4296435320..2ee7645690 100644--- a/autogpt_platform/backend/backend/copilot/computer_test.py+++ b/autogpt_platform/backend/backend/copilot/computer_test.py@@ -18,7 +18,11 @@ from backend.copilot.computer import (open_desktop,screen_is_on,)-from backend.copilot.tools.e2b_sandbox import SandboxLookupError, SandboxOwner+from backend.copilot.tools.e2b_sandbox import (+ KEEP_CEILING,+ SandboxLookupError,+ SandboxOwner,+)_C = "backend.copilot.computer"_USER, _EXPERT, _SESSION = "user-1", "exp-1", "sess-1"@@ -208,6 +212,8 @@ class TestOpenDesktop:user_id=_USER,session_id=_SESSION,count_turn=False,+ # The UI has no turn, so it keeps the box's ceiling.+ providers=KEEP_CEILING,)desktop_cls.assert_called_once_with(sandbox)desktop.ensure_display.assert_awaited_once()diff --git a/autogpt_platform/backend/backend/copilot/executor/utils.py b/autogpt_platform/backend/backend/copilot/executor/utils.pyindex 4a92fd18fb..73353659fe 100644--- a/autogpt_platform/backend/backend/copilot/executor/utils.py+++ b/autogpt_platform/backend/backend/copilot/executor/utils.py@@ -825,14 +825,15 @@ async def _admitted_turn_envelope(def _narrow_permissions(permissions: CopilotPermissions | None, envelope: TurnEnvelope) -> CopilotPermissions | None:- """The envelope's tool set as the turn's whitelist, keeping any block- filter the caller passed. Hides the tools from the model; the refusal+ """The envelope's tool set as the turn's whitelist, keeping any block and+ provider filter the caller passed. Hides the tools from the model; the refusalitself lives in ``BaseTool.execute``."""narrowed = envelope.as_permissions()if narrowed is None:return permissionsif permissions is None:return narrowed+ providers, providers_exclude = permissions.flattened_providers()# The caller's ``_parent`` is dropped on purpose: it belongs to the# spawner's turn, and the envelope is already the narrower bound.#@@ -845,6 +846,10 @@ def _narrow_permissions(tools_exclude=narrowed.tools_exclude,blocks=permissions.blocks,blocks_exclude=permissions.blocks_exclude,+ # The effective ceiling, parents included: this instance crosses the+ # queue, where ``_parent`` does not.+ providers=providers,+ providers_exclude=providers_exclude,)diff --git a/autogpt_platform/backend/backend/copilot/executor/utils_test.py b/autogpt_platform/backend/backend/copilot/executor/utils_test.pyindex 78242373b1..299a6ee920 100644--- a/autogpt_platform/backend/backend/copilot/executor/utils_test.py+++ b/autogpt_platform/backend/backend/copilot/executor/utils_test.py@@ -492,6 +492,30 @@ class TestNarrowPermissions:assert merged.tools_exclude is Falseassert merged.effective_allowed_tools(ALL_TOOL_NAMES) == {"read_workspace_file"}+ def test_the_callers_provider_ceiling_is_kept(self) -> None:+ narrowed = root_envelope("t").model_copy(+ update={"tools": frozenset({"bash_exec"})}+ )+ caller = CopilotPermissions(providers=["github"], providers_exclude=True)++ merged = utils._narrow_permissions(caller, narrowed)++ assert merged is not None+ assert not merged.is_provider_allowed("github")++ def test_a_parents_provider_ceiling_is_kept_through_the_queue(self) -> None:+ narrowed = root_envelope("t").model_copy(+ update={"tools": frozenset({"bash_exec"})}+ )+ parent = CopilotPermissions(providers=["github"], providers_exclude=True)+ caller = CopilotPermissions().merged_with_parent(parent, ALL_TOOL_NAMES)++ merged = utils._narrow_permissions(caller, narrowed)++ assert merged is not None+ again = CopilotPermissions.model_validate_json(merged.model_dump_json())+ assert not again.is_provider_allowed("github")+@pytest.mark.asyncioasync def test_dispatch_turn_puts_impersonation_on_the_queued_entry() -> None:diff --git a/autogpt_platform/backend/backend/copilot/integration_creds.py b/autogpt_platform/backend/backend/copilot/integration_creds.pyindex 32ba66900d..e5203b2bac 100644--- a/autogpt_platform/backend/backend/copilot/integration_creds.py+++ b/autogpt_platform/backend/backend/copilot/integration_creds.py@@ -23,7 +23,7 @@ creds-changed bus does. See ``_ensure_cache_invalidation_listener``.import asyncioimport loggingimport threading-from collections.abc import Mapping+from collections.abc import Collection, Mappingfrom typing import castimport aiohttp@@ -420,14 +420,16 @@ async def placeholder_grants(user_id: str,required_scopes: Mapping[str, frozenset[str]] | None = None,selected: Mapping[str, str] | None = None,+ providers: Collection[str] | None = None,) -> dict[str, str]:"""Provider to credential id: for each provider, the credential``get_integration_env_vars`` would have injected with the same arguments(the chat's pick, else the best match for the requested scopes). Aprovider with none (not connected, the pick deleted, a failed refresh) is- left out."""+ left out, and so is one outside the run's ceiling *providers* (``None``:+ every provider)."""grants: dict[str, str] = {}- for provider in PROVIDER_ENV_VARS:+ for provider, _ in _permitted(providers):scopes = (required_scopes or {}).get(provider, frozenset())credential_id = await get_provider_credential_id(user_id, provider, scopes, (selected or {}).get(provider)@@ -504,10 +506,20 @@ async def granted_to_box(sandbox_id: str, provider: str) -> set[str]:return {m.decode() if isinstance(m, bytes) else m for m in members}+def _permitted(providers: Collection[str] | None) -> list[tuple[str, list[str]]]:+ """``PROVIDER_ENV_VARS`` items within the ceiling *providers* (all: None)."""+ return [+ (provider, var_names)+ for provider, var_names in PROVIDER_ENV_VARS.items()+ if providers is None or provider in providers+ ]++async def get_integration_env_vars(user_id: str,required_scopes: Mapping[str, frozenset[str]] | None = None,selected: Mapping[str, str] | None = None,+ providers: Collection[str] | None = None,) -> dict[str, str]:"""Return env vars for all providers the user has connected.@@ -516,9 +528,11 @@ async def get_integration_env_vars(Only providers with a stored credential contribute entries.*required_scopes* maps a provider to the scopes its token should carry, and*selected* to the credential the user picked for it in this chat.+ *providers*, when given, is the run's ceiling: a provider outside it gets+ no variable."""env: dict[str, str] = {}- for provider, var_names in PROVIDER_ENV_VARS.items():+ for provider, var_names in _permitted(providers):scopes = (required_scopes or {}).get(provider, frozenset())token = await get_provider_token(user_id, provider, scopes, (selected or {}).get(provider)diff --git a/autogpt_platform/backend/backend/copilot/integration_creds_test.py b/autogpt_platform/backend/backend/copilot/integration_creds_test.pyindex 4df8ede9be..e9c868f980 100644--- a/autogpt_platform/backend/backend/copilot/integration_creds_test.py+++ b/autogpt_platform/backend/backend/copilot/integration_creds_test.py@@ -719,6 +719,17 @@ class TestPlaceholders:assert env["GIT_CONFIG_COUNT"] == "0"assert "GIT_CONFIG_KEY_0" not in env+ @pytest.mark.asyncio(loop_scope="session")+ async def test_a_provider_outside_the_runs_ceiling_gets_no_grant(self):+ manager = self._manager([self.older])+ with patch("backend.copilot.integration_creds._manager", manager):+ assert await placeholder_grants(_USER, providers=()) == {}+ assert await get_integration_env_vars(_USER, providers=()) == {}+ kept = await get_integration_env_vars(_USER, providers=("github",))+ assert kept["GH_TOKEN"] == "tok-older"+ # And its variables are blanked, not inherited from the box's own.+ assert placeholder_env({})["GH_TOKEN"] == ""+@pytest.mark.asyncio(loop_scope="session")async def test_grants_are_per_box_and_per_provider(self):store: dict[str, set[str]] = {}diff --git a/autogpt_platform/backend/backend/copilot/permissions.py b/autogpt_platform/backend/backend/copilot/permissions.pyindex e985bed2c8..a3fe54b697 100644--- a/autogpt_platform/backend/backend/copilot/permissions.py+++ b/autogpt_platform/backend/backend/copilot/permissions.py@@ -44,7 +44,18 @@ tools are denied and everything else is allowed. An empty list means``tools_exclude=False`` — ``tools`` is a **whitelist**; only listed toolsare allowed.-``blocks_exclude`` follows the same pattern for ``blocks``.+``blocks_exclude`` follows the same pattern for ``blocks``, and+``providers_exclude`` for ``providers``.++Providers+---------+``providers`` filters the connected accounts a run's sandbox may use, by+provider slug (``github``). It is a ceiling on credentials, not on tools: the+backend records it on the sandbox when the box's egress is pinned+(``backend.util.e2b_network``), and the swap proxy's credential service+(``backend.copilot.swap_credentials``) refuses to hand out a value for a+provider outside it. Nothing in the sandbox enforces it, so nothing there can+widen it.Denying a capability denies the tools that extend it (see``_IMPLIED_DENIALS``); allowing a capability gate allows the tool that now@@ -66,10 +77,13 @@ is at most as permissive as the parent:from __future__ import annotationsimport re-from typing import TYPE_CHECKING, Literal, get_args+from typing import TYPE_CHECKING, Any, Literal, get_argsfrom pydantic import BaseModel, PrivateAttr+from backend.copilot.providers import SUPPORTED_PROVIDERS+from backend.integrations.providers import ProviderName+if TYPE_CHECKING:from collections.abc import Iterable@@ -403,12 +417,18 @@ class CopilotPermissions(BaseModel):when False it is a whitelist. Ignored when *tools* is empty.blocks: Block identifiers (name, full UUID, or 8-char partial UUID).blocks_exclude: Same semantics as *tools_exclude* but for blocks.+ providers: Provider slugs (``github``) whose connected accounts the+ run's sandbox may use.+ providers_exclude: Same semantics as *tools_exclude* but for+ providers."""tools: list[str] = []tools_exclude: bool = Trueblocks: list[str] = []blocks_exclude: bool = True+ providers: list[str] = []+ providers_exclude: bool = True# Private: parent permissions for recursion inheritance.# Set only by merged_with_parent(); never exposed in block input schema.@@ -460,6 +480,38 @@ class CopilotPermissions(BaseModel):)return not matched if self.blocks_exclude else matched+ # ------------------------------------------------------------------+ # Provider helpers+ # ------------------------------------------------------------------++ def is_provider_allowed(self, provider: str) -> bool:+ """True if the run may use the user's *provider* account; the whole+ inheritance chain must agree."""+ if self.providers:+ listed = provider in self.providers+ if listed == self.providers_exclude:+ return False+ if self._parent is not None:+ return self._parent.is_provider_allowed(provider)+ return True++ def flattened_providers(self) -> tuple[list[str], bool]:+ """``(providers, providers_exclude)`` for the effective ceiling of this+ instance and its whole parent chain, needing no parent to read.++ An allow-list of the providers left, or, when none are left, a deny+ list of every one (an empty list means no filter at all)."""+ if not self.providers and self._parent is None:+ return [], True+ allowed = [p for p in SUPPORTED_PROVIDERS if self.is_provider_allowed(p)]+ if len(allowed) == len(SUPPORTED_PROVIDERS):+ # No filter, not an allow-list of today's providers: one added+ # later must not be denied to an unrestricted run.+ return [], True+ if allowed:+ return allowed, False+ return list(SUPPORTED_PROVIDERS), True+# ------------------------------------------------------------------# Recursion / merging# ------------------------------------------------------------------@@ -472,8 +524,9 @@ class CopilotPermissions(BaseModel):"""Return a new instance that is at most as permissive as *parent*.- Tools: intersection of effective-allowed sets, stored as a whitelist.- - Blocks: parent is stored internally; both constraints are applied- during :meth:`is_block_allowed`.+ - Blocks and providers: parent is stored internally; both+ constraints are applied during :meth:`is_block_allowed` and+ :meth:`is_provider_allowed`."""merged_tools = self.effective_allowed_tools(all_tools@@ -483,8 +536,15 @@ class CopilotPermissions(BaseModel):tools_exclude=False,blocks=self.blocks,blocks_exclude=self.blocks_exclude,+ # The child's own filter first, then the parent's chain on top.+ providers=self.providers,+ providers_exclude=self.providers_exclude,)result._parent = parent+ # Unlike blocks, the provider ceiling is written out in full rather+ # than left to ``_parent``: a private attribute does not survive the+ # executor queue's JSON, and the child turn must not come back wider.+ result.providers, result.providers_exclude = result.flattened_providers()return result# ------------------------------------------------------------------@@ -493,7 +553,12 @@ class CopilotPermissions(BaseModel):def is_empty(self) -> bool:"""Return True when no filtering is configured (allow-all passthrough)."""- return not self.tools and not self.blocks and self._parent is None+ return (+ not self.tools+ and not self.blocks+ and not self.providers+ and self._parent is None+ )# ---------------------------------------------------------------------------@@ -531,6 +596,78 @@ def validate_tool_names(tools: list[str]) -> list[str]:_tool_names_checked = False+def allowed_providers(+ permissions: CopilotPermissions | None,+) -> tuple[str, ...] | None:+ """The providers a run may use, of those a sandbox can be handed; ``None``+ when nothing restricts them, which is what a box's egress record stores+ for "every provider"."""+ if permissions is None or permissions.is_empty():+ return None+ allowed = tuple(+ p for p in SUPPORTED_PROVIDERS if permissions.is_provider_allowed(p)+ )+ if len(allowed) == len(SUPPORTED_PROVIDERS):+ return None+ return allowed+++def _denied(permissions: CopilotPermissions, infos: Iterable[Any]) -> list[str]:+ """The providers named by credential field specs *infos* that+ *permissions* keeps from the run, of those the ceiling covers."""+ named = {ProviderName(p).value for info in infos for p in info.provider}+ return sorted(+ p+ for p in named+ if p in SUPPORTED_PROVIDERS and not permissions.is_provider_allowed(p)+ )+++def denied_block_providers(+ permissions: CopilotPermissions | None, block: object+) -> list[str]:+ """The providers among *block*'s credential fields that *permissions*+ keeps from the run, of those the providers ceiling covers+ (``SUPPORTED_PROVIDERS``). Empty when nothing restricts them."""+ if permissions is None or allowed_providers(permissions) is None:+ return []+ schema = getattr(block, "input_schema", None)+ fields = schema.get_credentials_fields_info() if schema is not None else {}+ return _denied(permissions, fields.values())+++def denied_graph_providers(+ permissions: CopilotPermissions | None, graph: Any+) -> list[str]:+ """``denied_block_providers`` for every block of an agent graph: an agent+ run is its blocks acting with the user's credentials.++ An AutoPilot block counts as acting with every provider: its turn runs in+ the graph executor, which this run's ceiling does not reach, so it could+ use any account the user connected."""+ if permissions is None or allowed_providers(permissions) is None:+ return []+ fields = graph.aggregate_credentials_inputs()+ denied = _denied(permissions, (info for info, _, _ in fields.values()))+ if _runs_autopilot(graph):+ denied += [+ p+ for p in SUPPORTED_PROVIDERS+ if p not in denied and not permissions.is_provider_allowed(p)+ ]+ return denied+++def _runs_autopilot(graph: Any) -> bool:+ from backend.blocks.autopilot import AUTOPILOT_BLOCK_ID++ return any(+ node.block_id == AUTOPILOT_BLOCK_ID+ for g in [graph, *graph.sub_graphs]+ for node in g.nodes+ )++def denied_tool_names(permissions: CopilotPermissions | None) -> frozenset[str]:"""Short names *permissions* withholds from the turn (empty when none)."""if permissions is None or permissions.is_empty():diff --git a/autogpt_platform/backend/backend/copilot/permissions_test.py b/autogpt_platform/backend/backend/copilot/permissions_test.pyindex 65970550b1..5683ee44e8 100644--- a/autogpt_platform/backend/backend/copilot/permissions_test.py+++ b/autogpt_platform/backend/backend/copilot/permissions_test.py@@ -13,6 +13,7 @@ from backend.copilot.permissions import (CopilotPermissions,_block_matches,all_known_tool_names,+ allowed_providers,apply_tool_permissions,validate_block_identifiers,validate_tool_names,@@ -814,3 +815,113 @@ class TestApplyToolPermissionsIsLossless:for name in allowed_unfilteredif name.rsplit("__", 1)[-1] in DESCENT_DENIED_TOOLS}+++# ---------------------------------------------------------------------------+# Providers: the ceiling on a run's connected accounts+# ---------------------------------------------------------------------------+++class TestProviders:+ def test_no_filter_allows_every_provider(self):+ perms = CopilotPermissions()+ assert perms.is_provider_allowed("github")+ assert allowed_providers(perms) is None+ assert allowed_providers(None) is None++ def test_deny_list(self):+ perms = CopilotPermissions(providers=["github"], providers_exclude=True)+ assert not perms.is_provider_allowed("github")+ assert perms.is_provider_allowed("linear")+ assert allowed_providers(perms) == ()+ assert not perms.is_empty()++ def test_allow_list(self):+ perms = CopilotPermissions(providers=["github"], providers_exclude=False)+ assert perms.is_provider_allowed("github")+ assert not perms.is_provider_allowed("linear")+ # Every provider a box can be handed is allowed: no ceiling to record.+ assert allowed_providers(perms) is None++ def test_an_allow_list_of_nothing_the_box_can_use_allows_nothing(self):+ perms = CopilotPermissions(providers=["linear"], providers_exclude=False)+ assert allowed_providers(perms) == ()++ def test_a_tool_filter_alone_does_not_restrict_providers(self):+ perms = CopilotPermissions(tools=["web_fetch"], tools_exclude=True)+ assert allowed_providers(perms) is None++ def test_a_child_cannot_widen_its_parent(self):+ parent = CopilotPermissions(providers=["github"], providers_exclude=True)+ child = CopilotPermissions(providers=["github"], providers_exclude=False)+ merged = child.merged_with_parent(parent, ALL_TOOL_NAMES)+ assert not merged.is_provider_allowed("github")+ assert allowed_providers(merged) == ()++ def test_the_ceiling_survives_serialisation(self):+ """Permissions cross the executor queue as JSON."""+ perms = CopilotPermissions(providers=["github"], providers_exclude=True)+ again = CopilotPermissions.model_validate_json(perms.model_dump_json())+ assert not again.is_provider_allowed("github")++ def test_a_merged_ceiling_survives_the_queue_without_its_parent(self):+ """``_parent`` is private and does not cross the executor queue."""+ parent = CopilotPermissions(providers=["github"], providers_exclude=True)+ child = CopilotPermissions()+ merged = child.merged_with_parent(parent, ALL_TOOL_NAMES)+ again = CopilotPermissions.model_validate_json(merged.model_dump_json())+ assert not again.is_provider_allowed("github")+ assert allowed_providers(again) == ()++ def test_flattening_keeps_an_open_ceiling_open(self):+ assert CopilotPermissions().flattened_providers() == ([], True)+ # Under a parent too: not an allow-list of today's providers, which+ # would deny one added later.+ merged = CopilotPermissions().merged_with_parent(+ CopilotPermissions(tools=["web_fetch"], tools_exclude=True),+ ALL_TOOL_NAMES,+ )+ assert merged.flattened_providers() == ([], True)+ merged = CopilotPermissions().merged_with_parent(+ CopilotPermissions(), ALL_TOOL_NAMES+ )+ assert allowed_providers(merged) is None++ @pytest.mark.parametrize("round_trip", [False, True])+ def test_a_child_restriction_under_an_open_parent_is_kept(self, round_trip):+ """The other direction of inheritance: the child narrows, the parent+ does not."""+ child = CopilotPermissions(providers=["github"], providers_exclude=True)+ merged = child.merged_with_parent(CopilotPermissions(), ALL_TOOL_NAMES)+ if round_trip:+ merged = CopilotPermissions.model_validate_json(merged.model_dump_json())+ assert not merged.is_provider_allowed("github")+ assert allowed_providers(merged) == ()++ def test_child_and_parent_restrictions_combine(self):+ child = CopilotPermissions(providers=["github"], providers_exclude=False)+ parent = CopilotPermissions(providers=["linear"], providers_exclude=True)+ merged = child.merged_with_parent(parent, ALL_TOOL_NAMES)+ assert merged.is_provider_allowed("github")+ assert not merged.is_provider_allowed("linear")+ assert allowed_providers(merged) is None # github is every sandbox provider+++class TestGraphProviders:+ def _graph(self, *providers):+ from unittest.mock import MagicMock++ graph = MagicMock()+ graph.aggregate_credentials_inputs.return_value = {+ "credentials": (MagicMock(provider=frozenset(providers)), set(), True)+ }+ return graph++ def test_a_graph_using_a_denied_provider_is_named(self):+ from backend.copilot.permissions import denied_graph_providers++ perms = CopilotPermissions(providers=["github"], providers_exclude=True)+ assert denied_graph_providers(perms, self._graph("github")) == ["github"]+ # Providers the ceiling does not cover are not its to refuse.+ assert denied_graph_providers(perms, self._graph("slack")) == []+ assert denied_graph_providers(None, self._graph("github")) == []diff --git a/autogpt_platform/backend/backend/copilot/prompting.py b/autogpt_platform/backend/backend/copilot/prompting.pyindex 626f8cfcee..14ad5ba176 100644--- a/autogpt_platform/backend/backend/copilot/prompting.py+++ b/autogpt_platform/backend/backend/copilot/prompting.py@@ -7,6 +7,12 @@ handling the distinction between:"""from functools import cache+from typing import TYPE_CHECKING++from backend.copilot.providers import SUPPORTED_PROVIDERS++if TYPE_CHECKING:+ from backend.copilot.permissions import CopilotPermissionsfrom backend.blocks.desktop._api import DISPLAY@@ -655,7 +661,61 @@ def _get_cloud_sandbox_supplement() -> str:],file_move_name_1_to_2="Sandbox → Persistent",file_move_name_2_to_1="Persistent → Sandbox",- extra_notes=_E2B_TOOL_NOTES,+ extra_notes=_E2B_TOOL_NOTES + _placeholder_notes(),+ )+++def _placeholder_notes() -> str:+ """What a sandbox behind the credential swap proxy holds instead of the+ user's tokens, and where they work. The same for every user and session+ (it follows the deployment's proxy setting and the provider table), so the+ system prompt stays cacheable."""+ from backend.util.e2b_network import proxy_address++ if proxy_address() is None:+ return ""+ lines = [+ f"- `{'`/`'.join(entry['env_vars'])}` ({entry['name']}): "+ + ", ".join(h.lstrip(".") for h in entry["swap_hosts"])+ for entry in SUPPORTED_PROVIDERS.values()+ if entry["swap_hosts"]+ ]+ return (+ "\n### Connected accounts are placeholders\n"+ "A connected account's variables hold a placeholder (`hsurr:...`), "+ "never the token. The real token is put in outside the sandbox, only "+ "in the `Authorization` header of HTTPS requests to that provider's "+ "hosts, so `gh`, `git` over HTTPS and API clients work as usual:\n"+ + "\n".join(lines)+ + "\nSeeing the placeholder when printing the variable is expected. It "+ "does nothing in a URL, a request body or on any other host.\n"+ )+++def get_provider_ceiling_supplement(+ permissions: "CopilotPermissions | None",+) -> str:+ """The connected accounts a restricted run may use, so the model does not+ find the limit by failing (and ask the user to connect an account that is+ connected). Empty for an unrestricted run, which keeps its prompt+ identical to every other run's."""+ from backend.copilot.permissions import allowed_providers++ allowed = allowed_providers(permissions)+ if allowed is None:+ return ""+ names = [SUPPORTED_PROVIDERS[p]["name"] for p in allowed]+ reach = (+ f"only these of the user's connected accounts: {', '.join(names)}"+ if names+ else "none of the user's connected accounts"+ )+ return (+ "\n\n### Connected accounts this run may use\n"+ f"Code in the sandbox may use {reach}. Requests to any other "+ "provider are not authenticated, and connecting that account again "+ "will not change it: tell the user it is outside what this run was "+ "allowed to do.\n")diff --git a/autogpt_platform/backend/backend/copilot/prompting_test.py b/autogpt_platform/backend/backend/copilot/prompting_test.pyindex 5323e0b9e9..e7be99f708 100644--- a/autogpt_platform/backend/backend/copilot/prompting_test.py+++ b/autogpt_platform/backend/backend/copilot/prompting_test.py@@ -341,3 +341,57 @@ class TestMathGuidance:def test_sdk_supplement_tells_the_model_formulas_render(self, use_e2b):result = prompting.get_sdk_supplement(use_e2b=use_e2b)assert "`$…$` inline, `$$…$$` for display" in result+++class TestConnectedAccountNotes:+ """What the model is told about connected accounts: that they are+ placeholders behind the swap proxy, and which a restricted run may use."""++ @pytest.fixture(autouse=True)+ def fresh_supplements(self):+ prompting.get_sdk_supplement.cache_clear()+ prompting._get_cloud_sandbox_supplement.cache_clear()+ yield+ prompting.get_sdk_supplement.cache_clear()+ prompting._get_cloud_sandbox_supplement.cache_clear()++ def test_behind_the_proxy_the_sandbox_notes_say_where_placeholders_work(self):+ from unittest.mock import patch++ with patch("backend.util.e2b_network.proxy_address", return_value="p:1080"):+ text = prompting.get_sdk_supplement(use_e2b=True)+ assert "hsurr:" in text+ assert "`GH_TOKEN`/`GITHUB_TOKEN` (GitHub): github.com, api.github.com" in text++ def test_without_the_proxy_nothing_is_said(self):+ from unittest.mock import patch++ with patch("backend.util.e2b_network.proxy_address", return_value=None):+ assert "hsurr:" not in prompting.get_sdk_supplement(use_e2b=True)++ def test_an_unrestricted_run_gets_no_ceiling_note(self):+ from backend.copilot.permissions import CopilotPermissions++ assert prompting.get_provider_ceiling_supplement(None) == ""+ assert prompting.get_provider_ceiling_supplement(CopilotPermissions()) == ""++ def test_a_run_allowed_some_accounts_is_told_which(self):+ """The branch that names them: unreachable while GitHub is the only+ provider in the table, so pinned here with a stubbed ceiling."""+ from unittest.mock import patch++ from backend.copilot.permissions import CopilotPermissions++ with patch(+ "backend.copilot.permissions.allowed_providers", return_value=("github",)+ ):+ note = prompting.get_provider_ceiling_supplement(CopilotPermissions())+ assert "only these of the user's connected accounts: GitHub" in note++ def test_a_restricted_run_is_told_what_it_may_use(self):+ from backend.copilot.permissions import CopilotPermissions++ none = prompting.get_provider_ceiling_supplement(+ CopilotPermissions(providers=["github"], providers_exclude=True)+ )+ assert "none of the user's connected accounts" in nonediff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 5cbff68f21..eae356dddd 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -144,12 +144,14 @@ from ..pending_messages import ()from ..permissions import (CopilotPermissions,+ allowed_providers,apply_tool_permissions,denied_tool_names,)from ..prompting import (approval_mode_supplement,get_chat_platform_supplement,+ get_provider_ceiling_supplement,get_delegation_supplement,get_expert_oversight_supplement,get_team_building_supplement,@@ -205,7 +207,11 @@ from ..tools import (origin_disabled_tools,tool_names_in_groups,)-from ..tools.e2b_sandbox import get_or_create_sandbox, pause_sandbox_direct+from ..tools.e2b_sandbox import (+ forget_turn_ceiling,+ get_or_create_sandbox,+ pause_sandbox_direct,+)from ..tools.sandbox import WORKSPACE_PREFIX, make_session_pathfrom ..tools.seen_capabilities import build_seen_capabilities_noticefrom ..tools.session_context import build_session_context@@ -1745,6 +1751,7 @@ async def _apply_building_mode_restart(session_id: str,message_id: str,log_prefix: str,+ provider_supplement: str = "",) -> StreamStatus:"""Reconfigure *state* to relaunch the attempt with the guide in thesystem prompt, returning the status event to yield.@@ -1783,6 +1790,7 @@ async def _apply_building_mode_restart(system_prompt = (base_system_prompt+ get_sdk_supplement(use_e2b=use_e2b, expert_session=bool(session.expert_id))+ + provider_supplement+ delegation_supplement+ oversight_supplement+ team_building_supplement@@ -5079,6 +5087,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesvolume_mounts=workspace_volume_mounts(user_id, owner_expert_id),expert_id=owner_expert_id,user_id=user_id,+ providers=allowed_providers(permissions),)# Publish the live box before the gather returns: if a sibling# setup leg fails, the finally below still pauses it and@@ -5091,6 +5100,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuese2b_err,exc_info=True,)+ await forget_turn_ceiling(session_id, owner_expert_id)return Nonereturn sandbox@@ -5165,11 +5175,14 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues# calls into a gate that asks about *this* turn.session.clear_inflight_tool_calls()session.guide_in_system_prompt = bool(builder_session_suffix)+ # Empty unless the run's permissions restrict connected accounts.+ provider_supplement = get_provider_ceiling_supplement(permissions)system_prompt = (base_system_prompt+ get_sdk_supplement(use_e2b=use_e2b, expert_session=bool(session.expert_id))+ + provider_supplement+ delegation_supplement+ oversight_supplement+ team_building_supplement@@ -6029,6 +6042,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesstate=state,sdk_options=sdk_options,base_system_prompt=base_system_prompt,+ provider_supplement=provider_supplement,delegation_supplement=delegation_supplement,oversight_supplement=oversight_supplement,team_building_supplement=team_building_supplement,diff --git a/autogpt_platform/backend/backend/copilot/swap_credentials.py b/autogpt_platform/backend/backend/copilot/swap_credentials.pyindex 36b6b8b459..d9820c0903 100644--- a/autogpt_platform/backend/backend/copilot/swap_credentials.py+++ b/autogpt_platform/backend/backend/copilot/swap_credentials.py@@ -112,6 +112,11 @@ async def resolve_swap_credential(best match for the requested scopes); another of the user's accounts, oran id typed by hand, resolves to nothing.+ A provider outside the box's ceiling (``providers`` in its record, the+ run's ``CopilotPermissions``) comes back with no host it may be sent to:+ the proxy still scrubs the user's values out of what that provider's+ hosts send back, and swaps nothing.+``None`` for an unknown name, an unbound host, or nothing granted thatstill yields a token (disconnected since): in each case the placeholdergoes out as it is. :class:`NoLiveBox` if *box* is not a live box of@@ -129,6 +134,8 @@ async def resolve_swap_credential(if entry is None or not _host_is_bound(host, _bound_hosts(entry)):return Nonerecord = await _box_record(box, user_id)+ ceiling = record.get("providers")+ permitted = ceiling is None or name in ceilingsandbox_id = record.get("sandbox_id")granted = await granted_to_box(sandbox_id, name) if sandbox_id else set()values: dict[str, str] = {}@@ -151,5 +158,7 @@ async def resolve_swap_credential(raise failurereturn Nonereturn SwapCredential(- name=name, values=values, allowed_hosts=list(entry["swap_hosts"])+ name=name,+ values=values,+ allowed_hosts=list(entry["swap_hosts"]) if permitted else [],)diff --git a/autogpt_platform/backend/backend/copilot/swap_credentials_test.py b/autogpt_platform/backend/backend/copilot/swap_credentials_test.pyindex 75f35ac45e..32d8fbccf3 100644--- a/autogpt_platform/backend/backend/copilot/swap_credentials_test.py+++ b/autogpt_platform/backend/backend/copilot/swap_credentials_test.py@@ -383,3 +383,22 @@ async def test_the_github_content_hosts_that_take_the_token_may_be_sent_it(host)credential = await resolve_swap_credential("user-1", "github", host, _BOX)assert credential is not Noneassert _host_is_bound(host, credential.allowed_hosts)+++ "providers, sendable",+ [(None, True), (["github"], True), ([], False), (["linear"], False)],+ ids=["no ceiling", "github allowed", "none allowed", "github outside"],+)+async def test_a_provider_outside_the_boxs_ceiling_is_scrubbed_never_sent(+ live_box, providers, sendable+):+ live_box["providers"] = providers+ with _token("ghp_real"):+ credential = await resolve_swap_credential(+ "user-1", "github", "api.github.com", _BOX+ )+ assert credential is not None+ assert credential.values["cred-a"] == "ghp_real"+ assert credential.allowed_hosts == (_GITHUB_HOSTS if sendable else [])diff --git a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py b/autogpt_platform/backend/backend/copilot/tools/bash_exec.pyindex 1980c55d69..8267d0731a 100644--- a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py+++ b/autogpt_platform/backend/backend/copilot/tools/bash_exec.py@@ -33,6 +33,7 @@ from pydantic import BaseModelfrom backend.copilot.constants import HUNG_TOOL_CAP_SECONDSfrom backend.copilot.context import (E2B_WORKDIR,+ get_current_permissions,get_current_sandbox,looks_like_sdk_tool_result_path,sdk_tool_result_redirect_hint,@@ -50,6 +51,7 @@ from backend.copilot.integration_creds import (placeholder_grants,)from backend.copilot.model import ChatSession+from backend.copilot.permissions import allowed_providersfrom backend.copilot.sdk.env import config as chat_configfrom backend.util.e2b_network import proxy_address, reattach_commandfrom backend.util.sandbox_login import (@@ -362,8 +364,16 @@ class BashExecTool(BaseTool):secret_values: list[str] = []if user_id is not None:selected = await selected_credentials(session_id)+ # The run's ceiling on connected accounts. Without the proxy,+ # leaving a provider's token out is what keeps it from the box.+ # Behind it, a provider outside the ceiling gets no grant (and so+ # empty variables); the ceiling recorded when the box's egress was+ # pinned, enforced by the swap service, is what holds.+ providers = allowed_providers(get_current_permissions())if proxy_address() is not None:- grants = await placeholder_grants(user_id, required_scopes, selected)+ grants = await placeholder_grants(+ user_id, required_scopes, selected, providers+ )try:# Before the command runs: its placeholders resolve only# for credentials granted to this box.@@ -379,18 +389,20 @@ class BashExecTool(BaseTool):envs.update(placeholder_env(grants))else:integration_env = await get_integration_env_vars(- user_id, required_scopes, selected+ user_id, required_scopes, selected, providers)secret_values = [v for v in integration_env.values() if v]envs.update(integration_env)# Set git author/committer identity from the user's GitHub profile- # so commits made in the sandbox are attributed correctly.- git_identity = await get_github_user_git_identity(- user_id, selected.get("github")- )- if git_identity:- envs.update(git_identity)+ # so commits made in the sandbox are attributed correctly; not for+ # a run whose ceiling leaves GitHub out, which may not use it.+ if providers is None or "github" in providers:+ git_identity = await get_github_user_git_identity(+ user_id, selected.get("github")+ )+ if git_identity:+ envs.update(git_identity)# The box's running-time limit was armed when this turn connected;# E2B pauses the box at it, breaking the command's stream mid-run.diff --git a/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py b/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.pyindex 6df47d95c4..7dc4f04e13 100644--- a/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py@@ -140,7 +140,7 @@ class TestBashExecE2BTokenInjection:)# The session's picks reach the token lookup, not just its scopes.- mock_get_env.assert_awaited_once_with(_USER, None, _PICKED)+ mock_get_env.assert_awaited_once_with(_USER, None, _PICKED, None)# And the commit identity comes from that same GitHub account, so a# commit made with one account's token is not signed as another's.mock_identity.assert_awaited_once_with(_USER, "cred-picked")@@ -571,6 +571,57 @@ class TestBashExecWithoutTheSwapProxy:assert "GIT_CONFIG_COUNT" not in envs+class TestBashExecUnderAProviderCeiling:+ """A run whose permissions leave GitHub out gets no GitHub variable,+ with or without the proxy."""++ @pytest.fixture(autouse=True)+ def no_github(self):+ from backend.copilot.permissions import CopilotPermissions++ with patch(+ "backend.copilot.tools.bash_exec.get_current_permissions",+ return_value=CopilotPermissions(+ providers=["github"], providers_exclude=True+ ),+ ):+ yield++ @pytest.mark.asyncio(loop_scope="session")+ @pytest.mark.parametrize("proxy", [None, "proxy:1080"])+ async def test_github_is_left_out(self, proxy):+ sandbox = _make_sandbox(stdout="ok")+ session = make_session(user_id=_USER)+ with (+ patch("backend.copilot.tools.bash_exec.proxy_address", return_value=proxy),+ patch(+ "backend.copilot.tools.bash_exec.get_integration_env_vars",+ new=AsyncMock(return_value={}),+ ) as real,+ patch(+ "backend.copilot.tools.bash_exec.placeholder_grants",+ new=AsyncMock(return_value={}),+ ) as placeholders,+ patch("backend.copilot.tools.bash_exec.grant_to_box", new=AsyncMock()),+ patch(+ "backend.copilot.tools.bash_exec.get_github_user_git_identity",+ new=AsyncMock(return_value={"GIT_AUTHOR_NAME": "Ada"}),+ ) as identity,+ ):+ await _make_tool()._execute_on_e2b(+ sandbox=sandbox,+ command="gh repo list",+ timeout=10,+ session_id=session.session_id,+ user_id=_USER,+ )+ called = placeholders if proxy else real+ assert called.await_args.args[3] == ()+ # Nor is the GitHub account read for the commit identity.+ identity.assert_not_awaited()+ assert "GIT_AUTHOR_NAME" not in sandbox.commands.run.call_args[1]["envs"]++# ---------------------------------------------------------------------------# Timeouts against a box that behaves like E2B's# ---------------------------------------------------------------------------diff --git a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.pyindex cc120bec88..5963a6ec53 100644--- a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py+++ b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py@@ -71,6 +71,7 @@ box through the E2B API if the cache is ever lost.import asyncioimport contextlib+import jsonimport loggingimport mathimport time@@ -103,6 +104,7 @@ from backend.util.e2b_network import (create_sandbox,forget_sandbox,proxy_address,+ recorded_providers,)from backend.util.e2b_template import (SUPERSEDED_TEMPLATES,@@ -118,6 +120,20 @@ _SANDBOX_KEY_PREFIX = "copilot:e2b:sandbox:"_EXPERT_KEY_PREFIX = "copilot:e2b:expert:"_CREATING_SENTINEL = "creating"++class _KeepCeiling:+ """No ceiling given: keep the one the box already has."""++ def __repr__(self) -> str:+ return "KEEP_CEILING"+++# A re-pin from outside a turn (turning the screen on from the UI) or by a+# tool that has no ceiling of its own at hand (``start_desktop``) must not+# widen what the turn that pinned the box last allowed.+KEEP_CEILING = _KeepCeiling()+Ceiling = tuple[str, ...] | None | _KeepCeiling+# E2B sandbox metadata that lets an owner find its box without Redis.METADATA_OWNER = "autogpt_owner"METADATA_KIND = "autogpt_kind"@@ -201,11 +217,23 @@ _ACQUIRE_TURN_SCRIPT = (# the key. One script, so a turn that starts between the DECR and the DEL# can never have its count wiped._RELEASE_TURN_SCRIPT = (+ 'redis.call("hdel", KEYS[2], ARGV[1]) ''local n = redis.call("decr", KEYS[1]) ''if n <= 0 then redis.call("del", KEYS[1]) return 0 end '"return n")+# The provider ceiling each session last pinned an expert's box with, and+# when; a pin keeps only what all of them allow. Recorded and read in one+# script at the pin itself, so two turns pinning at once see each other even+# before either is counted. A turn's release removes its own session's entry+# and no other, and an entry older than any turn can run is ignored.+_PIN_CEILING_SCRIPT = (+ 'redis.call("hset", KEYS[1], ARGV[1], ARGV[2]) '+ 'redis.call("expire", KEYS[1], ARGV[3]) '+ 'return redis.call("hvals", KEYS[1])'+)+# One more reconnect attempt before a transient error surfaces: a box that# fails once on a blip must not be replaced, whoever owns it._RECONNECT_RETRY_DELAY_SECONDS = 1.0@@ -269,9 +297,14 @@ class SandboxOwner(BaseModel):"""return f"{self.key()}:stream"- def egress_owner(self, user_id: str | None) -> EgressOwner:- """Who the egress proxy sees this box as (``backend.util.e2b_network``)."""- return EgressOwner(kind=self.kind, id=self.id, user_id=user_id)+ def egress_owner(+ self, user_id: str | None, providers: tuple[str, ...] | None = None+ ) -> EgressOwner:+ """Who the egress proxy sees this box as (``backend.util.e2b_network``),+ and which of the user's providers it may use (``None``: every one)."""+ return EgressOwner(+ kind=self.kind, id=self.id, user_id=user_id, providers=providers+ )def display_lock_key(self) -> str:"""Redis key held by whoever is turning the screen on right now."""@@ -325,6 +358,7 @@ async def connect_owned(*,timeout: int | None = None,user_id: str | None = None,+ providers: "Ceiling" = KEEP_CEILING,pin_egress: bool = True,) -> AsyncSandbox:"""Connect to *sandbox_id* only if E2B says it belongs to *owner*.@@ -339,7 +373,8 @@ async def connect_owned(id must be refused without ever waking someone else's box. *timeout*is that limit for the owner's box (a resumed box would otherwise get theSDK's default). A connect that will run work re-pins the box's egress- (``backend.util.e2b_network``) for *user_id*; one that only pauses or+ (``backend.util.e2b_network``) for *user_id*, limited to *providers*+ (by default the ceiling the box already has); one that only pauses orkills passes ``pin_egress=False``."""info = await _owned_info(sandbox_id, owner, api_key)@@ -350,6 +385,7 @@ async def connect_owned(api_key,timeout=timeout,user_id=user_id,+ providers=providers,pin_egress=pin_egress,)@@ -374,9 +410,20 @@ async def _connect_pinned(*,timeout: int | None,user_id: str | None,+ providers: "Ceiling" = KEEP_CEILING,pin_egress: bool,+ session_id: str | None = None,) -> AsyncSandbox:"""Connect to *sandbox_id*, which *info* already showed to be *owner*'s."""++ async def ceiling_at_pin(egress_owner: EgressOwner) -> EgressOwner:+ # Under the box's rotation lock: concurrent pins see each other's.+ if isinstance(providers, _KeepCeiling):+ ceiling = await recorded_providers(sandbox_id)+ else:+ ceiling = await _shared_ceiling(owner, providers, sandbox_id, session_id)+ return egress_owner.model_copy(update={"providers": ceiling})+stamped = info.metadata or {}# Whose credentials the proxy may swap in is the box's own record too,# not the caller's word: processes of the user it was created for may@@ -394,8 +441,9 @@ async def _connect_pinned(sandbox = await connect_sandbox(AsyncSandbox,sandbox_id,- owner.egress_owner(swap_user_id),+ owner.egress_owner(swap_user_id, ()),apply_network=pin_egress,+ owner_at_pin=ceiling_at_pin,api_key=api_key,timeout=timeout,)@@ -404,6 +452,65 @@ async def _connect_pinned(return sandbox+async def _shared_ceiling(+ owner: SandboxOwner,+ providers: tuple[str, ...] | None,+ sandbox_id: str | None,+ session_id: str | None,+) -> tuple[str, ...] | None:+ """The ceiling a turn of *session_id* pins *owner*'s box with.++ Turns share an expert's box and its egress, so a pin keeps only what every+ live session's ceiling allows: a turn may narrow another's, never lift it.+ A plain session's box is its own. If the record cannot be used, the box's+ current ceiling narrows it instead.+ """+ if not owner.is_expert:+ return providers+ now = time.time()+ try:+ redis = await get_redis_async()+ pinned = await redis.eval(+ _PIN_CEILING_SCRIPT,+ 1,+ _pin_ceilings_key(owner),+ session_id or "",+ json.dumps(+ {"providers": list(providers) if providers is not None else None}+ | {"at": now}+ ),+ _ACTIVE_TURN_TTL,+ )+ ceiling = providers+ for raw in pinned:+ entry = json.loads(raw.decode() if isinstance(raw, bytes) else raw)+ if entry["at"] < now - _ACTIVE_TURN_TTL:+ continue # A turn that ended without releasing.+ other = entry["providers"]+ ceiling = _narrowest(ceiling, None if other is None else tuple(other))+ return ceiling+ except Exception:+ logger.warning(+ "[E2B] Could not record the ceiling for %s; narrowing by the box's",+ owner,+ exc_info=True,+ )+ if sandbox_id is None:+ return providers+ return _narrowest(providers, await recorded_providers(sandbox_id))+++def _narrowest(+ a: tuple[str, ...] | None, b: tuple[str, ...] | None+) -> tuple[str, ...] | None:+ """The providers both ceilings allow; ``None`` is every provider."""+ if a is None:+ return b+ if b is None:+ return a+ return tuple(p for p in a if p in b)++async def _keep_grants(sandbox_id: str) -> None:"""Renew the box's credential grants on a reconnect that will run work.@@ -519,7 +626,9 @@ async def _try_reconnect(*,timeout: int | None = None,user_id: str | None = None,+ providers: "Ceiling" = KEEP_CEILING,template: str | None = None,+ session_id: str | None = None,) -> "AsyncSandbox | None":"""Reconnect to the owner's box, or ``None`` if it is gone.@@ -549,7 +658,9 @@ async def _try_reconnect(api_key,timeout=timeout,user_id=user_id,+ providers=providers,pin_egress=True,+ session_id=session_id,)except SandboxNotOwnedError as exc:logger.warning("[E2B] Refusing reconnect: %s", exc)@@ -659,6 +770,24 @@ def _active_turns_key(owner: SandboxOwner) -> str:return f"{owner.key()}:active"+async def forget_turn_ceiling(session_id: str, expert_id: str | None) -> None:+ """Drop the ceiling a turn of *session_id* recorded on its expert's box,+ for a turn whose box never came up: its release will not run, and the+ entry would hold every other session's pin down until it expires."""+ owner = SandboxOwner.for_session(session_id, expert_id)+ if not owner.is_expert:+ return+ with contextlib.suppress(Exception):+ redis = await get_redis_async()+ await redis.hdel(_pin_ceilings_key(owner), session_id)+++def _pin_ceilings_key(owner: SandboxOwner) -> str:+ # Tagged with the whole turn-counter key, so on Redis Cluster both hash to+ # one slot: the release script touches the two together.+ return f"{{{_active_turns_key(owner)}}}:ceilings"++async def _acquire_turn(owner: SandboxOwner) -> None:"""Count this turn on an expert's box so another turn's end can't pause it.@@ -683,14 +812,18 @@ async def count_expert_turn(session_id: str, expert_id: str | None) -> None:await _acquire_turn(SandboxOwner.for_session(session_id, expert_id))-async def _release_turn(owner: SandboxOwner) -> bool:+async def _release_turn(owner: SandboxOwner, session_id: str | None = None) -> bool:"""Return ``True`` when the box may be paused: no other turn is still on it."""if not owner.is_expert:return Truetry:redis = await get_redis_async()key = _active_turns_key(owner)- remaining = int(await redis.eval(_RELEASE_TURN_SCRIPT, 1, key))+ remaining = int(+ await redis.eval(+ _RELEASE_TURN_SCRIPT, 2, key, _pin_ceilings_key(owner), session_id or ""+ )+ )if remaining <= 0:return Truelogger.info(@@ -732,7 +865,9 @@ async def _placeholder_grants(assert egress_owner.user_id is not None # ``swaps`` requires onetry:selected = await selected_credentials(session_id) if session_id else None- return await placeholder_grants(egress_owner.user_id, selected=selected)+ return await placeholder_grants(+ egress_owner.user_id, providers=egress_owner.providers, selected=selected+ )except Exception as exc:# Commands still get theirs; a box without them is no less safe.logger.warning("[E2B] No placeholder env for %s: %s", egress_owner, exc)@@ -750,6 +885,7 @@ async def get_or_create_owner_sandbox(user_id: str | None = None,session_id: str | None = None,count_turn: bool = True,+ providers: "Ceiling" = KEEP_CEILING,) -> AsyncSandbox:"""Return the owner's E2B sandbox, creating it if needed.@@ -771,6 +907,11 @@ async def get_or_create_owner_sandbox(from outside a turn (turning its screen on from the UI), or when therelease is not yet guaranteed to run and ``count_expert_turn`` follows.*user_id* / *session_id* are provenance only, stamped on a newly created box.+ *providers* is the turn's ceiling on the user's connected accounts+ (``permissions.allowed_providers``), recorded when the box's egress is+ pinned; ``None`` leaves every provider usable. Left out, a reconnect+ keeps the ceiling the box already has, and a new box (opened from the UI,+ outside any turn) gets every provider; a turn always passes its own.Raises :class:`SandboxLookupError` when E2B cannot say whether an expertalready has a box: a fresh box would fork the expert's durable state.@@ -805,7 +946,9 @@ async def get_or_create_owner_sandbox(api_key,timeout=timeout,user_id=user_id,+ providers=providers,template=template,+ session_id=session_id,)except Exception as exc:if value in retried_ids:@@ -869,8 +1012,16 @@ async def get_or_create_owner_sandbox(# At most _SANDBOX_CREATE_MAX_RETRIES − 1 = 2 sandboxes can# leak per incident.mounts = await _resolve_volume_mounts(volume_mounts, api_key)+ egress_owner = owner.egress_owner(+ user_id,+ (+ None+ if isinstance(providers, _KeepCeiling)+ else await _shared_ceiling(owner, providers, None, session_id)+ ),+ )box_grants = await _placeholder_grants(- owner.egress_owner(user_id), None if owner.is_expert else session_id+ egress_owner, None if owner.is_expert else session_id)box_env = placeholder_env(box_grants) if box_grants else {}last_exc: Exception | None = None@@ -879,7 +1030,7 @@ async def get_or_create_owner_sandbox(sandbox = await asyncio.wait_for(create_sandbox(AsyncSandbox,- owner.egress_owner(user_id),+ egress_owner,template=template,api_key=api_key,timeout=timeout,@@ -998,6 +1149,7 @@ async def get_or_create_sandbox(expert_id: str | None = None,user_id: str | None = None,count_turn: bool = True,+ providers: "Ceiling" = KEEP_CEILING,) -> AsyncSandbox:"""The sandbox for this turn (the session's, or its expert's), counting the turn.@@ -1014,6 +1166,7 @@ async def get_or_create_sandbox(user_id=user_id,session_id=session_id,count_turn=count_turn,+ providers=providers,)@@ -1089,7 +1242,7 @@ async def pause_sandbox(Safe to call even when no sandbox exists for the session."""owner = SandboxOwner.for_session(session_id, expert_id)- if not await _release_turn(owner):+ if not await _release_turn(owner, session_id):return Falserevoked = False@@ -1126,7 +1279,7 @@ async def pause_sandbox_direct(box is deliberately left running."""owner = SandboxOwner.for_session(session_id, expert_id)- if not await _release_turn(owner):+ if not await _release_turn(owner, session_id):return Falserevoked = await _revoke_stream(owner, sandbox)try:@@ -1328,7 +1481,10 @@ async def _forget_owner_state(owner: SandboxOwner) -> None:with contextlib.suppress(Exception):redis = await get_redis_async()await redis.delete(- owner.display_key(), owner.stream_key(), _active_turns_key(owner)+ owner.display_key(),+ owner.stream_key(),+ _active_turns_key(owner),+ _pin_ceilings_key(owner),)diff --git a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_redis_test.py b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_redis_test.pynew file mode 100644index 0000000000..39f521997e--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_redis_test.py@@ -0,0 +1,49 @@+"""The box scripts against the real Redis (a cluster in CI): the unit tests+fake their semantics, so only here do the Lua, the key counts and the+cluster slots actually run."""++import uuid++import pytest++from backend.copilot.integration_creds import grant_to_box, granted_to_box+from backend.data.redis_client import get_redis_async++from .e2b_sandbox import (+ SandboxOwner,+ _acquire_turn,+ _active_turns_key,+ _pin_ceilings_key,+ _release_turn,+ _shared_ceiling,+)++pytestmark = pytest.mark.asyncio(loop_scope="session")+++async def test_pins_narrow_each_other_and_a_release_drops_only_its_own():+ owner = SandboxOwner(kind="expert", id=f"ceiling-{uuid.uuid4().hex}")+ redis = await get_redis_async()+ try:+ assert await _shared_ceiling(owner, ("slack",), None, "s1") == ("slack",)+ # A failed script would fall back to this pin's own (None).+ assert await _shared_ceiling(owner, None, None, "s2") == ("slack",)+ await _acquire_turn(owner)+ # The release script touches the counter and the ceilings: one slot.+ assert await _release_turn(owner, "s1") is True+ assert await _shared_ceiling(owner, None, None, "s3") is None+ finally:+ await redis.delete(_active_turns_key(owner))+ await redis.delete(_pin_ceilings_key(owner))+++async def test_a_grant_is_recorded_with_its_expiry():+ sandbox_id = f"sb-{uuid.uuid4().hex}"+ redis = await get_redis_async()+ key = f"e2b:egress:grant:{sandbox_id}:github"+ try:+ await grant_to_box(sandbox_id, {"github": "cred-a"})+ assert await granted_to_box(sandbox_id, "github") == {"cred-a"}+ assert await redis.ttl(key) > 0+ finally:+ await redis.delete(key)diff --git a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.pyindex b8af93c486..b73143a839 100644--- a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py@@ -13,13 +13,15 @@ session-scoped event loop in conftest.py.import asyncioimport contextlib+import timefrom datetime import datetime, timedelta, timezonefrom types import SimpleNamespace-from unittest.mock import AsyncMock, MagicMock, patch+from unittest.mock import DEFAULT, AsyncMock, MagicMock, patchimport pytestfrom e2b import SandboxStatefrom e2b.exceptions import SandboxNotFoundException+from redis.crc import key_slotfrom backend.blocks.desktop._api import _STOP_STREAM, SHARED_PATH, WORKSPACE_PATHfrom backend.blocks.desktop._common import (@@ -37,6 +39,7 @@ from .e2b_sandbox import (SandboxLookupError,SandboxNotOwnedError,SandboxOwner,+ _connect_pinned,_try_reconnect,connect_owned,count_expert_turn,@@ -130,9 +133,15 @@ def _mock_redis(r.get = AsyncMock(side_effect=lambda key: raw if key == shell_key else None)r.set = AsyncMock(return_value=set_nx_result)r.delete = AsyncMock()+ r.eval = AsyncMock(side_effect=_pin_ceiling_alone)return r+def _pin_ceiling_alone(script, *args):+ """The pin-ceiling script on a box no other turn pinned: only this pin's."""+ return [args[3]] if "hset" in script else DEFAULT++def _patch_redis(redis: AsyncMock):return patch("backend.copilot.tools.e2b_sandbox.get_redis_async",@@ -150,7 +159,15 @@ def _patch_redis(redis: AsyncMock):def _no_proxy_credentials():"""Pause and kill revoke the box's proxy credential (``e2b_network``),which has its own Redis handle; ``TestProxyCredentialIsRevoked`` covers it."""- with patch("backend.copilot.tools.e2b_sandbox.forget_sandbox", AsyncMock()):+ with (+ patch("backend.copilot.tools.e2b_sandbox.forget_sandbox", AsyncMock()),+ # A reconnect with no ceiling given keeps the box's recorded one;+ # ``TestProviderCeilingIsPinned`` covers that read.+ patch(+ "backend.copilot.tools.e2b_sandbox.recorded_providers",+ AsyncMock(return_value=None),+ ),+ ):yield@@ -628,6 +645,7 @@ class TestKillSandbox:f"copilot:e2b:sandbox:{_SESSION_ID}:display",f"copilot:e2b:sandbox:{_SESSION_ID}:stream",f"copilot:e2b:sandbox:{_SESSION_ID}:active",+ f"{{copilot:e2b:sandbox:{_SESSION_ID}:active}}:ceilings",f"copilot:e2b:desktop:{_SESSION_ID}",}@@ -1184,7 +1202,11 @@ def _keyed_redis(values: dict[str, str | None], decr_result: int = 0) -> AsyncMo# Two scripts: ``_acquire_turn`` (INCR + EXPIRE) and ``_release_turn``# (DECR, and DEL when nothing is left).r.eval = AsyncMock(- side_effect=lambda script, *_: 1 if "incr" in script else max(decr_result, 0)+ side_effect=lambda script, *args: (+ [args[3]]+ if "hset" in script+ else 1 if "incr" in script else max(decr_result, 0)+ ))return r@@ -1646,8 +1668,14 @@ class TestExpertPause:sb.pause.assert_awaited_once()# One script does the DECR and, at zero, the DEL: no window for a turn# that starts in between to lose its count.- script, _, key = redis.eval.await_args.args+ script, _, key, ceilings, session = redis.eval.await_args.argsassert key == _EXPERT_ACTIVE_KEY and "decr" in script and "del" in script+ # The ceilings pinned while turns shared the box go with the last one,+ # in the counter's cluster slot: one script may touch only one slot.+ assert ceilings == f"{{{_EXPERT_ACTIVE_KEY}}}:ceilings"+ assert key_slot(ceilings.encode()) == key_slot(key.encode())+ # Only this session's ceiling goes; another's may be pinned, uncounted.+ assert session == _SESSION_ID and "hdel" in scriptdef test_concurrent_turn_keeps_the_box_running(self):"""Pausing under another session of the same expert would sever its@@ -1715,6 +1743,7 @@ class TestExpertKill:_EXPERT_DISPLAY_KEY,_EXPERT_STREAM_KEY,_EXPERT_ACTIVE_KEY,+ f"{{{_EXPERT_ACTIVE_KEY}}}:ceilings",_EXPERT_LEGACY_DESKTOP_KEY,}# The cached id was enough; no lookup for the shell box.@@ -1770,6 +1799,7 @@ class TestKillSandboxForgetsScreen:f"copilot:e2b:sandbox:{_SESSION_ID}:display",f"copilot:e2b:sandbox:{_SESSION_ID}:stream",f"copilot:e2b:sandbox:{_SESSION_ID}:active",+ f"{{copilot:e2b:sandbox:{_SESSION_ID}:active}}:ceilings",f"copilot:e2b:desktop:{_SESSION_ID}",}@@ -2072,9 +2102,9 @@ class TestCreationEnvBehindTheSwapProxy:kwargs = self._create(proxy="proxy:1080", user_id="user-a")assert kwargs["envs"]["GH_TOKEN"] == "hsurr:github:cred-default"assert kwargs["envs"]["GITHUB_TOKEN"] == "hsurr:github:cred-default"- # A chat's own box starts with the chat's pick.+ # A chat's own box starts with the chat's pick, under the turn's ceiling.self.lookup.assert_awaited_once_with(- "user-a", selected={"github": "cred-default"}+ "user-a", providers=None, selected={"github": "cred-default"})self.grant.assert_awaited_once_with("sb-new", {"github": "cred-default"})@@ -2107,7 +2137,7 @@ class TestCreationEnvBehindTheSwapProxy:) as lookup,):asyncio.run(_placeholder_grants(owner, session_id))- lookup.assert_awaited_once_with("user-a", selected=selected)+ lookup.assert_awaited_once_with("user-a", providers=None, selected=selected)def test_without_the_proxy_the_create_call_is_unchanged(self):kwargs = self._create(proxy=None, user_id="user-a")@@ -2163,3 +2193,275 @@ class TestGrantsAreRenewedOnReconnect:)def test_otherwise_nothing_is_renewed(self, kwargs):self._reconnect(**kwargs).assert_not_awaited()+++def _pinning_connect() -> AsyncMock:+ """``connect_sandbox`` deciding the pin the way the real one does, under+ its lock; the owner it pinned with is ``.pinned``."""+ mock = AsyncMock()++ async def connect(cls, sandbox_id, owner, *, owner_at_pin=None, **kwargs):+ if owner_at_pin is not None and kwargs.get("apply_network", True):+ owner = await owner_at_pin(owner)+ mock.pinned = owner+ return MagicMock()++ mock.side_effect = connect+ return mock+++class TestProviderCeilingIsPinned:+ """The turn's ceiling on connected accounts reaches the box's egress+ record at create and at every reconnect (``e2b_network``)."""++ def test_on_create(self):+ redis = _mock_redis(set_nx_result=True, stored_sandbox_id=None)+ with (+ _patch_sdk(),+ _patch_redis(redis),+ patch(+ "backend.copilot.tools.e2b_sandbox.create_sandbox",+ AsyncMock(return_value=_mock_sandbox("sb-new")),+ ) as create,+ ):+ asyncio.run(+ get_or_create_sandbox(+ _SESSION_ID,+ _API_KEY,+ timeout=_TIMEOUT,+ user_id="user-a",+ providers=("github",),+ )+ )+ egress_owner = create.await_args.args[1]+ assert egress_owner.user_id == "user-a"+ assert egress_owner.providers == ("github",)++ def test_on_reconnect(self):+ owner = SandboxOwner(kind="expert", id=_EXPERT_ID)+ stamp = owner.creation_metadata(user_id="user-a")+ with (+ _patch_sdk() as mock_cls,+ patch(+ "backend.copilot.tools.e2b_sandbox.connect_sandbox",+ _pinning_connect(),+ ) as connect,+ _patch_redis(_mock_redis()),+ ):+ mock_cls.get_info = AsyncMock(return_value=MagicMock(metadata=stamp))+ asyncio.run(+ connect_owned("sb-1", owner, _API_KEY, user_id="user-a", providers=())+ )+ assert connect.pinned.providers == ()+++class _CeilingRedis:+ """The pin-ceiling and turn scripts, run against an in-memory store."""++ def __init__(self) -> None:+ self.hashes: dict[str, dict[str, str]] = {}+ self.counts: dict[str, int] = {}++ async def eval(self, script, numkeys, *args):+ keys, argv = args[:numkeys], args[numkeys:]+ if "hset" in script:+ self.hashes.setdefault(keys[0], {})[argv[0]] = argv[1]+ return list(self.hashes[keys[0]].values())+ if "incr" in script:+ self.counts[keys[0]] = self.counts.get(keys[0], 0) + 1+ return self.counts[keys[0]]+ self.hashes.get(keys[1], {}).pop(argv[0], None)+ n = self.counts.get(keys[0], 0) - 1+ if n <= 0:+ self.counts.pop(keys[0], None)+ return 0+ self.counts[keys[0]] = n+ return n++ def count(self) -> None:+ asyncio.run(self.eval("incr", 1, _EXPERT_ACTIVE_KEY, 1))++ def release(self, session_id: str) -> None:+ asyncio.run(+ self.eval(+ "hdel decr",+ 2,+ _EXPERT_ACTIVE_KEY,+ f"{{{_EXPERT_ACTIVE_KEY}}}:ceilings",+ session_id,+ )+ )+++class TestTurnsSharingABoxShareTheNarrowestCeiling:+ """Turns share an expert's box and its egress: a turn's pin may narrow the+ ceiling another live session pinned, never lift it, even when both pin at+ once or one has not been counted yet."""++ _OWNER = SandboxOwner(kind="expert", id=_EXPERT_ID)++ def _pin(self, redis, providers, session_id) -> tuple[str, ...] | None:+ stamp = self._OWNER.creation_metadata(user_id="user-a")+ redis_patch = patch(+ "backend.copilot.tools.e2b_sandbox.get_redis_async",+ AsyncMock(return_value=redis),+ )+ with (+ patch(+ "backend.copilot.tools.e2b_sandbox.connect_sandbox",+ _pinning_connect(),+ ) as connect,+ redis_patch,+ ):+ asyncio.run(+ _connect_pinned(+ "sb-1",+ MagicMock(metadata=stamp),+ self._OWNER,+ _API_KEY,+ timeout=None,+ user_id="user-a",+ providers=providers,+ pin_egress=True,+ session_id=session_id,+ )+ )+ return connect.pinned.providers++ @pytest.mark.parametrize(+ "first, second, pinned",+ [+ (("slack",), None, ("slack",)), # an unrestricted turn joins+ (None, ("github",), ("github",)), # a restricted turn joins+ (("github", "slack"), ("slack",), ("slack",)),+ (("github",), ("slack",), ()),+ (None, None, None),+ ],+ )+ def test_a_second_pin_keeps_what_both_allow_before_either_is_counted(+ self, first, second, pinned+ ):+ redis = _CeilingRedis()+ assert self._pin(redis, first, "s1") == first+ assert self._pin(redis, second, "s2") == pinned++ def test_another_sessions_release_keeps_an_uncounted_pin(self):+ """s1 ends while s2 has pinned but is not counted yet: s2's ceiling+ must still hold s3 down."""+ redis = _CeilingRedis()+ redis.count()+ self._pin(redis, None, "s1")+ self._pin(redis, ("slack",), "s2")+ redis.release("s1")+ assert self._pin(redis, None, "s3") == ("slack",)++ def test_once_a_session_ends_its_ceiling_no_longer_holds(self):+ redis = _CeilingRedis()+ redis.count()+ self._pin(redis, ("slack",), "s1")+ redis.release("s1")+ assert self._pin(redis, None, "s2") is None++ def test_a_session_that_never_released_stops_holding_after_a_turn_can_end(+ self,+ ):+ redis = _CeilingRedis()+ self._pin(redis, ("slack",), "s1")+ later = time.time() + 7 * 3600+ with patch("backend.copilot.tools.e2b_sandbox.time.time", return_value=later):+ assert self._pin(redis, None, "s2") is None++ def test_without_the_record_the_boxs_ceiling_narrows_the_pin(self):+ redis = MagicMock()+ redis.eval = AsyncMock(side_effect=ConnectionError("redis down"))+ with patch(+ "backend.copilot.tools.e2b_sandbox.recorded_providers",+ AsyncMock(return_value=("slack",)),+ ):+ assert self._pin(redis, None, "s1") == ("slack",)++ def test_a_plain_session_box_is_its_own(self):+ owner = SandboxOwner(kind="session", id=_SESSION_ID)+ stamp = owner.creation_metadata(user_id="user-a")+ redis = _CeilingRedis()+ with (+ _patch_sdk() as mock_cls,+ patch(+ "backend.copilot.tools.e2b_sandbox.connect_sandbox",+ _pinning_connect(),+ ) as connect,+ patch(+ "backend.copilot.tools.e2b_sandbox.get_redis_async",+ AsyncMock(return_value=redis),+ ),+ ):+ mock_cls.get_info = AsyncMock(return_value=MagicMock(metadata=stamp))+ asyncio.run(+ connect_owned("sb-1", owner, _API_KEY, user_id="user-a", providers=None)+ )+ assert connect.pinned.providers is None+ assert redis.hashes == {}+++class TestATurnWhoseBoxNeverCameUp:+ """Its release will not run, so its ceiling entry is dropped at once+ rather than holding every other session's pin down until it expires."""++ def test_its_sessions_entry_goes_and_no_other(self):+ from .e2b_sandbox import forget_turn_ceiling++ redis = MagicMock()+ redis.hdel = AsyncMock()+ with _patch_redis(redis):+ asyncio.run(forget_turn_ceiling(_SESSION_ID, _EXPERT_ID))+ redis.hdel.assert_awaited_once_with(+ f"{{{_EXPERT_ACTIVE_KEY}}}:ceilings", _SESSION_ID+ )++ def test_a_chats_own_box_has_none_to_drop(self):+ from .e2b_sandbox import forget_turn_ceiling++ redis = MagicMock()+ redis.hdel = AsyncMock()+ with _patch_redis(redis):+ asyncio.run(forget_turn_ceiling(_SESSION_ID, None))+ redis.hdel.assert_not_awaited()+++class TestNoCeilingGivenKeepsTheBoxs:+ """``start_desktop`` and the UI screen-on re-pin a box without a ceiling of+ their own: that must keep the turn's, not widen it to every provider."""++ def _reconnect(self, **kwargs) -> tuple[MagicMock, AsyncMock]:+ owner = SandboxOwner(kind="session", id=_SESSION_ID)+ stamp = owner.creation_metadata(user_id="user-a")+ with (+ _patch_sdk() as mock_cls,+ patch(+ "backend.copilot.tools.e2b_sandbox.connect_sandbox",+ _pinning_connect(),+ ) as connect,+ patch(+ "backend.copilot.tools.e2b_sandbox.recorded_providers",+ AsyncMock(return_value=()),+ ) as recorded,+ ):+ mock_cls.get_info = AsyncMock(return_value=MagicMock(metadata=stamp))+ asyncio.run(+ connect_owned("sb-1", owner, _API_KEY, user_id="user-a", **kwargs)+ )+ return connect, recorded++ def test_a_re_pin_without_one_keeps_the_recorded_ceiling(self):+ connect, recorded = self._reconnect()+ recorded.assert_awaited_once_with("sb-1")+ assert connect.pinned.providers == ()++ def test_a_turn_that_gives_one_sets_it(self):+ connect, recorded = self._reconnect(providers=("github",))+ recorded.assert_not_awaited()+ assert connect.pinned.providers == ("github",)++ def test_a_connect_that_does_not_pin_reads_nothing(self):+ _, recorded = self._reconnect(pin_egress=False)+ recorded.assert_not_awaited()diff --git a/autogpt_platform/backend/backend/copilot/tools/helpers.py b/autogpt_platform/backend/backend/copilot/tools/helpers.pyindex f841ed9461..f1a0ce8840 100644--- a/autogpt_platform/backend/backend/copilot/tools/helpers.py+++ b/autogpt_platform/backend/backend/copilot/tools/helpers.py@@ -22,6 +22,8 @@ from backend.copilot.constants import (from backend.copilot.context import get_current_permissions, is_unattended_turnfrom backend.copilot.credential_selection import turn_credential_pinsfrom backend.copilot.model import ChatSession+from backend.copilot.permissions import denied_block_providers+from backend.copilot.providers import SUPPORTED_PROVIDERSfrom backend.copilot.sdk.env import config as chat_configfrom backend.copilot.sdk.file_ref import FileRefExpansionError, expand_file_refs_in_argsfrom backend.copilot.tool_display import emit_tool_display_name@@ -1061,6 +1063,8 @@ async def prepare_block_for_execution(# Before credentials: resolving them can provision a managed profile.if denied := _permission_denied(block_id, block.name, session_id):return denied+ if denied := _provider_denied(block, block_id, session_id):+ return deniedemit_tool_display_name(block.name)@@ -1269,6 +1273,26 @@ def _permission_denied().from_outside()+def _provider_denied(+ block: AnyBlockSchema, block_id: str, session_id: str+) -> ErrorResponse | None:+ """The refusal when *block* would act with a provider's stored credential+ the run may not use. The run's ceiling on connected accounts covers+ blocks too, as the sandbox's placeholders are by the swap service."""+ denied = denied_block_providers(get_current_permissions(), block)+ if not denied:+ return None+ names = ", ".join(SUPPORTED_PROVIDERS[p]["name"] for p in denied)+ return ErrorResponse(+ message=(+ f"Block '{block.name}' ({block_id}) acts with the user's {names} "+ "account, which this run is not permitted to use. Tell the user it "+ "is outside what this run was allowed to do."+ ),+ session_id=session_id,+ ).from_outside()++def required_input_keys(block: AnyBlockSchema) -> set[str]:"""Inputs a block needs from its caller; without them ``run_block`` answerswith the schema and runs nothing. Credentials resolve on their own."""diff --git a/autogpt_platform/backend/backend/copilot/tools/run_agent.py b/autogpt_platform/backend/backend/copilot/tools/run_agent.pyindex 746576e7a4..682e565ed2 100644--- a/autogpt_platform/backend/backend/copilot/tools/run_agent.py+++ b/autogpt_platform/backend/backend/copilot/tools/run_agent.py@@ -13,7 +13,7 @@ from backend.api.features.library.model import ()from backend.copilot.config import ChatConfigfrom backend.copilot.constants import MAX_TOOL_WAIT_SECONDS-from backend.copilot.context import is_unattended_turn+from backend.copilot.context import get_current_permissions, is_unattended_turnfrom backend.copilot.gate.subject import (NO_OP,Subject,@@ -22,6 +22,8 @@ from backend.copilot.gate.subject import ()from backend.copilot.gate.workflow import awaiting_whom, card_chat_for_runfrom backend.copilot.model import ChatSession+from backend.copilot.permissions import denied_graph_providers+from backend.copilot.providers import SUPPORTED_PROVIDERSfrom backend.copilot.tool_display import emit_tool_display_namefrom backend.copilot.tracking import track_chat_outcomefrom backend.copilot.tree import charge_credits@@ -462,6 +464,11 @@ class RunAgentTool(BaseTool):graph_version=graph.version,).from_outside()+ # Before the credential and input prompts: no setup card for a+ # run that the ceiling refuses anyway.+ if refusal := _provider_refusal(graph, session_id):+ return refusal+# Step 2: Check credentials and inputsgraph_credentials, prereq_error = await self._check_prerequisites(graph=graph,@@ -1002,6 +1009,8 @@ class RunAgentTool(BaseTool):under the graph's own safe-mode setting rather than pausing again."""session_id = session.session_id+ if refusal := _provider_refusal(graph, session_id):+ return refusal# Check rate limits (dry runs don't count against the session limit)if (@@ -1300,6 +1309,8 @@ class RunAgentTool(BaseTool):) -> ToolResponseBase:"""Set up scheduled execution for an agent."""session_id = session.session_id+ if refusal := _provider_refusal(graph, session_id):+ return refusal# Validate schedule paramsschedule_name = schedule_name.strip()@@ -1431,6 +1442,24 @@ class RunAgentTool(BaseTool):).from_outside()+def _provider_refusal(graph: GraphModel, session_id: str) -> ErrorResponse | None:+ """Refuse an agent whose blocks act with a provider's credentials the run+ may not use: the run's ceiling on connected accounts covers what it+ launches, as it covers its own blocks (``run_block``) and its sandbox."""+ denied = denied_graph_providers(get_current_permissions(), graph)+ if not denied:+ return None+ names = ", ".join(SUPPORTED_PROVIDERS[p]["name"] for p in denied)+ return ErrorResponse(+ message=(+ f"Workflow '{graph.name}' acts with the user's {names} account, which "+ "this run is not permitted to use. Tell the user it is outside what "+ "this run was allowed to do."+ ),+ session_id=session_id,+ ).from_outside(graph.name)++# One lookup for the run and for the gate: were they two, a drift between# them would gate one graph and run another.async def _agent_graph(diff --git a/autogpt_platform/backend/backend/copilot/tools/run_agent_provider_ceiling_test.py b/autogpt_platform/backend/backend/copilot/tools/run_agent_provider_ceiling_test.pynew file mode 100644index 0000000000..08a2461ce6--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/tools/run_agent_provider_ceiling_test.py@@ -0,0 +1,115 @@+"""An agent run is held to the run's ceiling on connected accounts."""++from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.copilot.context import _current_permissions+from backend.copilot.permissions import CopilotPermissions+from backend.integrations.providers import ProviderName++from ._test_data import make_session+from .models import ErrorResponse+from .run_agent import RunAgentTool+++def _graph(*providers) -> MagicMock:+ graph = MagicMock()+ graph.name = "PR helper"+ graph.aggregate_credentials_inputs.return_value = {+ "credentials": (MagicMock(provider=frozenset(providers)), set(), True)+ }+ return graph++[email protected](loop_scope="session")[email protected]("scheduled", [False, True], ids=["run", "schedule"])+async def test_an_agent_acting_with_a_denied_provider_is_refused(scheduled):+ session = make_session(user_id="user-1")+ graph = _graph(ProviderName.GITHUB)+ token = _current_permissions.set(+ CopilotPermissions(providers=["github"], providers_exclude=True)+ )+ try:+ tool = RunAgentTool()+ if scheduled:+ response = await tool._schedule_agent(+ "user-1", session, graph, {}, {}, "nightly", "0 3 * * *", None+ )+ else:+ response = await tool._run_agent("user-1", session, graph, {}, {}, False)+ finally:+ _current_permissions.reset(token)+ assert isinstance(response, ErrorResponse)+ assert "GitHub account" in response.message+ assert "not permitted" in response.message++[email protected](loop_scope="session")+async def test_the_refusal_comes_before_any_setup_card():+ """A run the ceiling refuses asks the user for no credential or input."""+ session = make_session(user_id="user-1")+ graph = _graph(ProviderName.GITHUB)+ graph.has_external_trigger = False+ prerequisites = AsyncMock()+ token = _current_permissions.set(+ CopilotPermissions(providers=["github"], providers_exclude=True)+ )+ try:+ with (+ patch(+ "backend.copilot.tools.run_agent._agent_graph",+ new=AsyncMock(return_value=(graph, None)),+ ),+ patch(+ "backend.copilot.tools.run_agent._bind_builder_graph", new=AsyncMock()+ ),+ patch(+ "backend.copilot.tools.run_agent.require_installed_workflow",+ new=AsyncMock(return_value=None),+ ),+ patch.object(RunAgentTool, "_check_prerequisites", prerequisites),+ ):+ response = await RunAgentTool()._execute(+ user_id="user-1", session=session, username_agent_slug="ada/pr-helper"+ )+ finally:+ _current_permissions.reset(token)+ assert isinstance(response, ErrorResponse)+ assert "GitHub account" in response.message+ prerequisites.assert_not_awaited()++[email protected](loop_scope="session")+async def test_an_agent_with_an_autopilot_block_is_refused_under_a_ceiling():+ """Its nested turn runs in the graph executor, which the ceiling does not+ reach, so it could act with any connected account."""+ from backend.blocks.autopilot import AUTOPILOT_BLOCK_ID++ session = make_session(user_id="user-1")+ graph = _graph(ProviderName.CODEX)+ graph.nodes = [MagicMock(block_id=AUTOPILOT_BLOCK_ID)]+ graph.sub_graphs = []+ token = _current_permissions.set(+ CopilotPermissions(providers=["github"], providers_exclude=True)+ )+ try:+ response = await RunAgentTool()._run_agent(+ "user-1", session, graph, {}, {}, False+ )+ finally:+ _current_permissions.reset(token)+ assert isinstance(response, ErrorResponse)+ assert "GitHub account" in response.message++[email protected](loop_scope="session")+async def test_an_autopilot_agent_runs_for_an_unrestricted_turn():+ from backend.blocks.autopilot import AUTOPILOT_BLOCK_ID+ from backend.copilot.permissions import denied_graph_providers++ graph = _graph(ProviderName.CODEX)+ graph.nodes = [MagicMock(block_id=AUTOPILOT_BLOCK_ID)]+ graph.sub_graphs = []+ assert denied_graph_providers(None, graph) == []+ assert denied_graph_providers(CopilotPermissions(), graph) == []diff --git a/autogpt_platform/backend/backend/copilot/tools/run_block_test.py b/autogpt_platform/backend/backend/copilot/tools/run_block_test.pyindex e8e530d115..718055d9ba 100644--- a/autogpt_platform/backend/backend/copilot/tools/run_block_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/run_block_test.py@@ -7,6 +7,7 @@ import pytestfrom backend.blocks._base import BlockTypefrom backend.copilot.context import _current_permissionsfrom backend.copilot.permissions import CopilotPermissions+from backend.integrations.providers import ProviderNamefrom ._test_data import make_sessionfrom .models import (@@ -176,6 +177,46 @@ class TestRunBlockFiltering:assert isinstance(response, ErrorResponse)assert "not permitted" in response.message+ @pytest.mark.asyncio(loop_scope="session")+ async def test_a_block_acting_with_a_provider_outside_the_ceiling_is_refused(+ self,+ ):+ """Denying ``github`` to the run denies its GitHub blocks too, not+ only the sandbox's placeholders."""+ session = make_session(user_id=_TEST_USER_ID)+ block_id = "11111111-2222-3333-4444-666666666666"+ github_block = make_mock_block(block_id, "Create PR", BlockType.STANDARD)+ github_block.input_schema.get_credentials_fields_info.return_value = {+ "credentials": MagicMock(provider=frozenset({ProviderName.GITHUB}))+ }++ perms = CopilotPermissions(providers=["github"], providers_exclude=True)+ token = _current_permissions.set(perms)+ try:+ with (+ patch(+ "backend.copilot.tools.helpers.get_block",+ return_value=github_block,+ ),+ patch(+ "backend.copilot.tools.helpers.match_credentials_to_requirements",+ return_value=({}, []),+ ),+ ):+ response = await RunBlockTool()._execute(+ user_id=_TEST_USER_ID,+ session=session,+ block_id=block_id,+ input_data={},+ dry_run=False,+ )+ finally:+ _current_permissions.reset(token)++ assert isinstance(response, ErrorResponse)+ assert "GitHub account" in response.message+ assert "not permitted" in response.message+@pytest.mark.asyncio(loop_scope="session")async def test_allowed_by_permissions_passes_guard(self):"""A block explicitly allowed by a whitelist CopilotPermissions passes the guard."""diff --git a/autogpt_platform/backend/backend/copilot/tools/start_desktop.py b/autogpt_platform/backend/backend/copilot/tools/start_desktop.pyindex 399ee7a87f..cc7701e4fd 100644--- a/autogpt_platform/backend/backend/copilot/tools/start_desktop.py+++ b/autogpt_platform/backend/backend/copilot/tools/start_desktop.py@@ -20,7 +20,9 @@ import loggingfrom typing import Anyfrom backend.blocks.desktop._api import DISPLAY, SHARED_PATH, WORKSPACE_PATH+from backend.copilot.context import get_current_permissionsfrom backend.copilot.model import ChatSession+from backend.copilot.permissions import allowed_providersfrom backend.copilot.sdk.env import config as chat_configfrom .base import BaseTool@@ -104,6 +106,9 @@ class StartDesktopTool(BaseTool):api_key,user_id=user_id,session_id=session_id,+ # The turn's own ceiling: a box this creates must not get+ # every provider (KEEP is for the UI, which has no turn).+ providers=allowed_providers(get_current_permissions()),)except Exception as exc:logger.error("[E2B] start_desktop failed: %s", exc, exc_info=True)diff --git a/autogpt_platform/backend/backend/copilot/tools/start_desktop_test.py b/autogpt_platform/backend/backend/copilot/tools/start_desktop_test.pyindex 3a72830bcf..bbbc4c122d 100644--- a/autogpt_platform/backend/backend/copilot/tools/start_desktop_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/start_desktop_test.py@@ -128,6 +128,9 @@ class TestStartDesktop:assert kwargs["volume_mounts"] == {WORKSPACE_PATH: user_volume_name(_USER)}assert kwargs["template"] == "agpt-desktop-1x2-004d6e73"assert kwargs["count_turn"] is False+ # The turn's own ceiling (none here), never KEEP: a box this creates+ # must not get every provider in a restricted turn.+ assert kwargs["providers"] is Noneassert kwargs["user_id"] == _USERassert kwargs["session_id"] == session.session_idbox.desktop.ensure_display.assert_awaited_once()@@ -144,6 +147,26 @@ class TestStartDesktop:assert "DISPLAY=:0" in result.messageassert "browser_* tools run elsewhere" in result.message+ @pytest.mark.asyncio(loop_scope="session")+ async def test_a_restricted_turn_opens_the_desktop_under_its_ceiling(self):+ from backend.copilot.context import _current_permissions+ from backend.copilot.permissions import CopilotPermissions++ box = _Box()+ token = _current_permissions.set(+ CopilotPermissions(providers=["github"], providers_exclude=True)+ )+ try:+ await _run(+ StartDesktopTool(),+ box,+ user_id=_USER,+ session=make_session(user_id=_USER),+ )+ finally:+ _current_permissions.reset(token)+ assert box.get_box.await_args.kwargs["providers"] == ()+@pytest.mark.asyncio(loop_scope="session")async def test_stored_result_carries_the_owner_link_not_the_password(self):tool = StartDesktopTool()diff --git a/autogpt_platform/backend/backend/util/e2b_network.py b/autogpt_platform/backend/backend/util/e2b_network.pyindex 20d3adb3b2..a0fc79b2da 100644--- a/autogpt_platform/backend/backend/util/e2b_network.py+++ b/autogpt_platform/backend/backend/util/e2b_network.py@@ -39,7 +39,7 @@ import hashlibimport jsonimport loggingimport secrets-from typing import Any, Literal, Optional, TypeVar, cast+from typing import Any, Awaitable, Callable, Literal, Optional, TypeVar, castfrom e2b import AsyncCommandHandle, AsyncSandboxfrom e2b.sandbox.sandbox_api import (@@ -53,9 +53,16 @@ from backend.data.redis_client import get_redis_asyncfrom backend.util.settings import Settingslogger = logging.getLogger(__name__)+# One JSON line per pin: which box, for whom, and which of the user's accounts+# its requests may be given. Never a value; the proxy's own audit has the+# requests themselves.+audit_logger = logging.getLogger("backend.egress_audit")_CREDENTIAL_KEY_PREFIX = "e2b:egress:cred:"_BOX_KEY_PREFIX = "e2b:egress:box:"+# The box's provider ceiling, apart from its credential: a pause revokes the+# credential, and the ceiling must outlive it (see ``recorded_providers``).+_CEILING_KEY_PREFIX = "e2b:egress:ceiling:"# A credential lives as long as a paused box can (E2B's paused-sandbox# lifetime); every reconnect replaces it anyway._CREDENTIAL_TTL = 48 * 3600@@ -78,6 +85,11 @@ class EgressOwner(BaseModel):(``swaps``): a block runs a graph someone else may have written, and amarketplace agent must not get to act with the GitHub account of whoeverruns it.++ *providers* is the ceiling on which of the user's providers the box may+ use (the turn's ``CopilotPermissions``); ``None`` means every one. It is+ recorded with the credential, and the backend's swap service refuses a+ value for any provider outside it."""model_config = ConfigDict(frozen=True)@@ -85,6 +97,7 @@ class EgressOwner(BaseModel):kind: Literal["session", "expert", "block"]id: struser_id: Optional[str] = None+ providers: Optional[tuple[str, ...]] = None@propertydef label(self) -> str:@@ -146,7 +159,7 @@ async def create_sandbox(sandbox_cls: type[S], owner: EgressOwner, **kwargs: Anyraisetry:await _remember(credential, owner, sandbox_id=sandbox.sandbox_id)- await _bind(sandbox.sandbox_id, credential.username)+ await _bind(sandbox.sandbox_id, credential.username, owner)except BaseException:# The box is on the meter and its handle is about to be lost: kill# it rather than leak it, and take its credential with it.@@ -160,6 +173,7 @@ async def create_sandbox(sandbox_cls: type[S], owner: EgressOwner, **kwargs: Anysandbox.sandbox_id,owner.label,)+ _audit_pin("created", sandbox.sandbox_id, owner)return sandbox@@ -169,6 +183,7 @@ async def connect_sandbox(owner: EgressOwner,*,apply_network: bool = True,+ owner_at_pin: Optional[Callable[[EgressOwner], Awaitable[EgressOwner]]] = None,**kwargs: Any,) -> S:"""Reconnect to a box, re-pinning its egress under a fresh credential.@@ -191,6 +206,10 @@ async def connect_sandbox(if address is None or not apply_network:return sandboxasync with _rotation_lock(sandbox_id):+ if owner_at_pin is not None:+ # Decided under the lock, so concurrent pins of one box apply in+ # the order they were decided: a later one sees every earlier one.+ owner = await owner_at_pin(owner)credential = _mint()await _remember(credential, owner, sandbox_id=sandbox_id)try:@@ -209,10 +228,11 @@ async def connect_sandbox(sandbox.pause(), timeout=_KILL_TIMEOUT_SECONDS)raise- await _bind(sandbox_id, credential.username)+ await _bind(sandbox_id, credential.username, owner)logger.info("[E2B] Reconnected %.12s for %s, egress re-pinned", sandbox_id, owner.label)+ _audit_pin("reconnected", sandbox_id, owner)return sandbox@@ -237,6 +257,10 @@ async def kill_sandbox(sandbox: AsyncSandbox) -> None:since the box may still be running."""await sandbox.kill()await forget_sandbox(sandbox.sandbox_id)+ # Nothing will re-pin a killed box; its ceiling would only wait out its TTL.+ with contextlib.suppress(Exception):+ redis = await get_redis_async()+ await redis.delete(_CEILING_KEY_PREFIX + sandbox.sandbox_id)async def forget_sandbox(sandbox_id: str) -> None:@@ -244,7 +268,8 @@ async def forget_sandbox(sandbox_id: str) -> None:Nothing will present it again (a resume mints a fresh one), and until itsTTL it would keep naming this owner to the proxy. Best effort: a failure- here must not fail the pause or kill it follows.+ here must not fail the pause or kill it follows. The box's provider+ ceiling stays: the next re-pin of a paused box keeps it."""# Not skipped while no proxy is configured: a box pinned before the# address was removed still has a credential on record.@@ -271,6 +296,53 @@ async def credential_record(username: str) -> Optional[dict[str, Any]]:return json.loads(raw.decode() if isinstance(raw, bytes) else raw)+async def recorded_providers(sandbox_id: str) -> Optional[tuple[str, ...]]:+ """The provider ceiling the box was last pinned with: what a re-pin that+ has no ceiling of its own to give keeps, so that it never widens one.++ Read from its own key, not the credential record, because a pause revokes+ the credential and the box's ceiling must survive that. With the proxy on,+ every box is pinned when it is created, so a missing record has expired (a+ box paused longer than its TTL) or cannot be read: either way no provider,+ the safe side, until the next turn pins the box with its own.+ """+ try:+ redis = await get_redis_async()+ raw = await redis.get(_CEILING_KEY_PREFIX + sandbox_id)+ except Exception:+ logger.warning(+ "[E2B] Could not read the ceiling of %.12s; keeping none",+ sandbox_id,+ exc_info=True,+ )+ return ()+ if not raw:+ return ()+ providers = json.loads(raw.decode() if isinstance(raw, bytes) else raw)+ return None if providers is None else tuple(providers)+++def _audit_pin(event: str, sandbox_id: str, owner: EgressOwner) -> None:+ """Record whose credentials a box's requests may be given from now on:+ the per-box half of the audit trail, the proxy's lines being the+ per-request half."""+ audit_logger.info(+ json.dumps(+ {+ "event": event,+ "sandbox_id": sandbox_id,+ "owner": owner.label,+ "user_id": owner.user_id,+ "swaps": owner.swaps,+ "providers": (+ sorted(owner.providers) if owner.providers is not None else "all"+ ),+ },+ sort_keys=True,+ )+ )++def _mint() -> ProxyCredential:return ProxyCredential(username=f"box-{secrets.token_hex(8)}", secret=secrets.token_urlsafe(32)@@ -303,6 +375,8 @@ async def _remember("user_id": owner.user_id,# Absent or false means the proxy swaps nothing for this box."swaps": owner.swaps,+ # None: every provider. Read by the backend, never by the box.+ "providers": list(owner.providers) if owner.providers is not None else None,"sandbox_id": sandbox_id,"secret_sha256": secret_digest(credential.secret),}@@ -350,11 +424,16 @@ async def _rotation_lock(sandbox_id: str):await lock.release()-async def _bind(sandbox_id: str, username: str) -> None:- """Make *username* the box's current credential and forget its previous one."""+async def _bind(sandbox_id: str, username: str, owner: EgressOwner) -> None:+ """Make *username* the box's current credential and forget its previous+ one, and record the ceiling it was pinned with."""redis = await get_redis_async()key = _BOX_KEY_PREFIX + sandbox_idprevious = await _bound_username(sandbox_id)await redis.set(key, username, ex=_CREDENTIAL_TTL)+ providers = list(owner.providers) if owner.providers is not None else None+ await redis.set(+ _CEILING_KEY_PREFIX + sandbox_id, json.dumps(providers), ex=_CREDENTIAL_TTL+ )if previous and previous != username:await redis.delete(_CREDENTIAL_KEY_PREFIX + previous)diff --git a/autogpt_platform/backend/backend/util/e2b_network_test.py b/autogpt_platform/backend/backend/util/e2b_network_test.pyindex 717d0b07d5..ff965bd7b3 100644--- a/autogpt_platform/backend/backend/util/e2b_network_test.py+++ b/autogpt_platform/backend/backend/util/e2b_network_test.py@@ -160,6 +160,7 @@ class TestPinned:"owner": "expert:exp-1","user_id": "user-1","swaps": True,+ "providers": None,"sandbox_id": "sb-1","secret_sha256": secret_digest("s3cr3t-256-bits"),}@@ -191,6 +192,88 @@ class TestPinned:record = await credential_record("box-a1")assert record and record["swaps"] is swaps+ @pytest.mark.asyncio+ async def test_the_runs_provider_ceiling_is_on_the_record_at_every_pin(self):+ """The swap service reads it from here: never from the box, and never+ from the proxy's word."""+ box, redis = _box("sb-1"), _redis()+ cls = _sdk(box)+ narrow = _OWNER.model_copy(update={"providers": ("github",)})+ none = _OWNER.model_copy(update={"providers": ()})+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await create_sandbox(cls, narrow, template="t")+ created = cls.create.await_args.kwargs["network"]["egress_proxy"]+ on_create = await credential_record(created["username"])+ await connect_sandbox(cls, "sb-1", none)+ repinned = box.update_network.await_args.args[0]["egress_proxy"]+ on_reconnect = await credential_record(repinned["username"])+ assert on_create and on_create["providers"] == ["github"]+ assert on_reconnect and on_reconnect["providers"] == []++ @pytest.mark.asyncio+ async def test_a_pins_owner_is_decided_under_the_boxs_lock(self):+ """Two concurrent reconnects apply in the order they decided, so a+ later pin always sees what an earlier one recorded."""+ box, redis = _box("sb-1"), _redis()+ held: list[bool] = []++ async def at_pin(owner):+ held.append("e2b:egress:lock:sb-1" in redis.store)+ return owner.model_copy(update={"providers": ("github",)})++ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await connect_sandbox(_sdk(box), "sb-1", _OWNER, owner_at_pin=at_pin)+ repinned = box.update_network.await_args.args[0]["egress_proxy"]+ record = await credential_record(repinned["username"])+ ceiling = await e2b_network.recorded_providers("sb-1")+ assert held == [True]+ assert record and record["providers"] == ["github"]+ assert ceiling == ("github",)++ @pytest.mark.asyncio+ async def test_every_pin_is_audited_without_a_secret(self, caplog):+ box, redis = _box("sb-1"), _redis()+ cls = _sdk(box)+ minted = ProxyCredential(username="box-a1", secret="s3cr3t-256-bits")+ with (+ _configured(_PROXY),+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ patch(f"{_M}._mint", return_value=minted),+ caplog.at_level("INFO", logger="backend.egress_audit"),+ ):+ await create_sandbox(cls, _OWNER, template="t")+ await connect_sandbox(+ cls, "sb-1", _OWNER.model_copy(update={"providers": ("github",)})+ )+ lines = [+ json.loads(r.message)+ for r in caplog.records+ if r.name == "backend.egress_audit"+ ]+ assert lines == [+ {+ "event": "created",+ "sandbox_id": "sb-1",+ "owner": "expert:exp-1",+ "user_id": "user-1",+ "swaps": True,+ "providers": "all",+ },+ {+ "event": "reconnected",+ "sandbox_id": "sb-1",+ "owner": "expert:exp-1",+ "user_id": "user-1",+ "swaps": True,+ "providers": ["github"],+ },+ ]+ assert "s3cr3t" not in caplog.text and "box-a1" not in caplog.text+@pytest.mark.asyncioasync def test_the_record_exists_before_the_box_does(self):"""The box's first connection must already resolve at the proxy."""@@ -378,7 +461,8 @@ class TestPinned:await create_sandbox(_sdk(box), _OWNER, template="t")assert redis.storeawait forget_sandbox("sb-1")- assert redis.store == {}+ # Only the ceiling stays, for the box's next re-pin to keep.+ assert set(redis.store) == {"e2b:egress:ceiling:sb-1"}await forget_sandbox("sb-never-pinned")@pytest.mark.asyncio@@ -478,3 +562,60 @@ def test_the_guard_sees_every_shape_of_a_direct_call(line, tmp_path, monkeypatchmonkeypatch.setattr(backend, "__file__", str(package / "__init__.py"))with pytest.raises(AssertionError, match="sneaky.py:2"):test_every_sdk_create_and_connect_goes_through_the_chokepoint()+++class TestRecordedProviders:+ """What a re-pin with no ceiling of its own keeps."""++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "providers, kept", [(("github",), ("github",)), ((), ()), (None, None)]+ )+ async def test_the_ceiling_on_record_is_kept(self, providers, kept):+ redis = _redis()+ owner = _OWNER.model_copy(update={"providers": providers})+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await create_sandbox(_sdk(_box("sb-1")), owner, template="t")+ assert await e2b_network.recorded_providers("sb-1") == kept++ @pytest.mark.asyncio+ async def test_the_ceiling_outlives_a_pause(self):+ """A pause revokes the credential; the desktop's re-pin after it must+ still keep the restricted turn's ceiling, not widen it to all."""+ redis = _redis()+ owner = _OWNER.model_copy(update={"providers": ("github",)})+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await create_sandbox(_sdk(_box("sb-1")), owner, template="t")+ await forget_sandbox("sb-1")+ assert await e2b_network.recorded_providers("sb-1") == ("github",)++ @pytest.mark.asyncio+ async def test_a_reconnect_replaces_the_ceiling(self):+ redis = _redis()+ box = _box("sb-1")+ narrow = _OWNER.model_copy(update={"providers": ("github",)})+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await create_sandbox(_sdk(box), _OWNER, template="t")+ assert await e2b_network.recorded_providers("sb-1") is None+ await connect_sandbox(_sdk(box), "sb-1", narrow)+ assert await e2b_network.recorded_providers("sb-1") == ("github",)++ @pytest.mark.asyncio+ async def test_a_box_with_no_record_keeps_no_provider(self):+ """Every box is pinned at create, so no record means it expired (a box+ paused past its TTL): keeping every provider would fail open."""+ with patch(f"{_M}.get_redis_async", AsyncMock(return_value=_redis())):+ assert await e2b_network.recorded_providers("sb-paused-for-days") == ()++ @pytest.mark.asyncio+ async def test_an_unreadable_record_keeps_no_provider(self):+ with patch(+ f"{_M}.get_redis_async", AsyncMock(side_effect=ConnectionError("down"))+ ):+ assert await e2b_network.recorded_providers("sb-1") == ()diff --git a/autogpt_platform/swap_proxy/README.md b/autogpt_platform/swap_proxy/README.mdindex 3ee119d9d6..2f80fd70b3 100644--- a/autogpt_platform/swap_proxy/README.md+++ b/autogpt_platform/swap_proxy/README.md@@ -46,7 +46,10 @@ users at once.credential) for providers that need it, and arrives with the bindingtable (SECRT-2616); nothing sets it yet. The backend is asked for valuesper user, per host and per box, and refuses hosts a credential is not bound- to (see "What a box holds").+ to (see "What a box holds"). It also holds the box to the run's ceiling on+ connected accounts (`providers` in `CopilotPermissions`, recorded when the+ box's egress is pinned): a provider outside it comes back with no host it+ may be sent to, scrubbed but never swapped.(`swap_proxy/swap.py`, `swap_proxy/source.py`)5. **Scrub.** A value echoed back in a text response, or in a websocket messagefrom the server, is turned back into its placeholder before the box sees it,@@ -349,6 +352,14 @@ run it by hand: `gh workflow run platform-swap-proxy-ci.yml --ref <branch>`.the case, it does not close it.- A body in gzip or zstd may hold at most 64 members or frames; more istreated as undecodable.+- A box's ceiling on connected accounts is per box, not per turn. On an+ expert's box, a turn's pin keeps only what every live session's ceiling+ allows, so turns sharing a box share the narrowest. Once a session's turn+ ends, its ceiling stops holding, so a process it left running gets the next+ turn's ceiling. A re-pin with no ceiling of its own (the desktop tool,+ turning the screen on from the UI) keeps the one on record, and so does a+ pause; with no record left (a box paused past its TTL) it keeps none until a+ turn pins its own.- A connection stays open after its box's credential is rotated (everyreconnect rotates it), but the backend no longer knows its box: from then onnothing is swapped into it, and its text responses from bound hosts arediff --git a/autogpt_platform/swap_proxy/swap_proxy/source.py b/autogpt_platform/swap_proxy/swap_proxy/source.pyindex bda90fe3b0..f43ef6af17 100644--- a/autogpt_platform/swap_proxy/swap_proxy/source.py+++ b/autogpt_platform/swap_proxy/swap_proxy/source.py@@ -84,7 +84,8 @@ class BackendCredentialSource:) -> Optional[Credential]:"""*user_id*'s credential *name* for *host*, asked for the box whoseproxy credential is *box*: the backend answers only for a live box of- that user's, with what was granted to that box."""+ that user's, with what was granted to that box, within the providers+ that box may use."""key = (user_id, name, host.lower(), box)now = time.monotonic()cached = self._credentials.get(key)diff --git a/docs/integrations/block-integrations/misc.md b/docs/integrations/block-integrations/misc.mdindex 0da6f55d9e..bb6bea3af7 100644--- a/docs/integrations/block-integrations/misc.md+++ b/docs/integrations/block-integrations/misc.md@@ -100,6 +100,8 @@ Tool and block identifiers provided in `tools` and `blocks` are validated at run| tools_exclude | Controls how the 'tools' list is interpreted. True (default): 'tools' is a deny-list — listed tools are blocked, all others are allowed. An empty 'tools' list means allow everything. False: 'tools' is an allow-list — only listed tools are permitted. | bool | No || blocks | Block identifiers to filter when the copilot runs blocks via run_capability. Each entry can be: a block name (e.g. 'HTTP Request'), a full block UUID, or the first 8 hex characters of the UUID (e.g. 'c069dc6b'). Works with blocks_exclude. Leave empty to apply no block filter. | List[str] | No || blocks_exclude | Controls how the 'blocks' list is interpreted. True (default): 'blocks' is a deny-list — listed blocks are blocked, all others are allowed. An empty 'blocks' list means allow everything. False: 'blocks' is an allow-list — only listed blocks are permitted. | bool | No |+| providers | Connected-account providers (e.g. 'github') whose accounts the run may use: from code in the sandbox, and through blocks that act with that provider's credentials. Works with providers_exclude. Leave empty to apply no provider filter. | List[str] | No |+| providers_exclude | Controls how the 'providers' list is interpreted. True (default): 'providers' is a deny-list — listed providers are blocked, all others are allowed. An empty 'providers' list means allow everything. False: 'providers' is an allow-list — of the supported connected-account providers, only listed ones are permitted. Other providers are not covered. | bool | No || dry_run | When enabled, run_capability and run_agent tool calls in this autopilot session are forced to use dry-run simulation mode. No real API calls, side effects, or credits are consumed by those tools. Useful for testing agent wiring and previewing outputs. Only applies when creating a new session (session_id is empty). When reusing an existing session_id, the session's original dry_run setting is preserved. | bool | No |### Outputs