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): run copilot sandboxes on our own 1 vCPU / 2 GiB desktop image (#14379)
Nicholas Tindle2df5e80c8697
diff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.pyindex 63773c7988..66698b0526 100644--- a/autogpt_platform/backend/backend/copilot/config.py+++ b/autogpt_platform/backend/backend/copilot/config.py@@ -670,8 +670,12 @@ class ChatConfig(BaseSettings):description="E2B API key. Falls back to E2B_API_KEY environment variable.",)e2b_sandbox_template: str = Field(- default="base",- description="E2B sandbox template to use for copilot sessions.",+ default="agpt-desktop-1x2",+ description="E2B sandbox template for copilot sessions. The default is our "+ "own image (E2B's desktop image at 1 vCPU / 2 GiB, ~$0.08/h running, "+ "no display started), built on the team automatically the first time "+ "it is needed; see backend.util.e2b_template. Any other value is used "+ "as-is and must already exist on the team.",)e2b_sandbox_timeout: int = Field(default=420, # 7 min safety net — allows headroom for compaction retriesdiff --git a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.pyindex 038158fc41..6e8eb1cdb1 100644--- a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py+++ b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py@@ -52,6 +52,7 @@ from typing import Any, Awaitable, Callable, Literalfrom e2b import AsyncSandbox, SandboxLifecyclefrom backend.data.redis_client import get_redis_async+from backend.util.e2b_template import ensure_template, forget_templatelogger = logging.getLogger(__name__)@@ -174,7 +175,13 @@ async def get_or_create_sandbox(await asyncio.sleep(_WAIT_INTERVAL_SECONDS)continue- # No sandbox and no active creation — atomically claim the creation slot.+ # No sandbox and no active creation. Our own image is built on the+ # team the first time it is needed; that happens before the creation+ # slot is claimed because a build can outlive the slot's TTL. Cached+ # per process after the first check, so the repeat is free.+ await ensure_template(template, api_key)++ # Atomically claim the creation slot.claimed = await redis.set(key, _CREATING_SENTINEL, nx=True, ex=_CREATION_LOCK_TTL)@@ -228,6 +235,9 @@ async def get_or_create_sandbox(await asyncio.sleep(2 ** (attempt - 1)) # 1 s, 2 sif last_exc is not None:+ # The template may have gone away since this process last+ # confirmed it; make the next attempt look again.+ forget_template(template, api_key)raise last_excassert sandbox is not None # guaranteed: last_exc is None iff break was hitdiff --git a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.pyindex 7eb8b78ec6..4adfe0094d 100644--- a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py@@ -167,6 +167,45 @@ class TestGetOrCreateSandbox:# sandbox_id should be saved to Redisredis.set.assert_awaited()+ def test_create_ensures_our_template_exists_first(self):+ """The managed image is built on the team before the first create."""+ order: list[str] = []+ new_sb = _mock_sandbox("sb-new")+ redis = _mock_redis(set_nx_result=True, stored_sandbox_id=None)++ async def fake_ensure(template: str, api_key: str) -> None:+ order.append(f"ensure:{template}:{api_key}")++ async def fake_create(**kwargs):+ order.append("create")+ return new_sb++ async def fake_set(key, value, **kwargs):+ if value == _CREATING_SENTINEL:+ order.append("claim")+ return True++ redis.set = AsyncMock(side_effect=fake_set)++ with (+ patch("backend.copilot.tools.e2b_sandbox.AsyncSandbox") as mock_cls,+ patch(+ "backend.copilot.tools.e2b_sandbox.ensure_template",+ side_effect=fake_ensure,+ ),+ _patch_redis(redis),+ ):+ mock_cls.create = AsyncMock(side_effect=fake_create)+ asyncio.run(+ get_or_create_sandbox(+ _SESSION_ID, _API_KEY, timeout=_TIMEOUT, template="agpt-desktop-1x2"+ )+ )++ # The build can take longer than the creation slot's TTL, so it must+ # finish before the slot is claimed.+ assert order == [f"ensure:agpt-desktop-1x2:{_API_KEY}", "claim", "create"]+def test_create_with_on_timeout_kill(self):"""on_timeout='kill' disables auto_resume automatically."""new_sb = _mock_sandbox("sb-new")@@ -196,12 +235,17 @@ class TestGetOrCreateSandbox:_patch_redis(redis),):mock_cls.create = AsyncMock(side_effect=RuntimeError("quota"))- with pytest.raises(RuntimeError, match="quota"):+ with (+ pytest.raises(RuntimeError, match="quota"),+ patch("backend.copilot.tools.e2b_sandbox.forget_template") as forget,+ ):asyncio.run(get_or_create_sandbox(_SESSION_ID, _API_KEY, timeout=_TIMEOUT))redis.delete.assert_awaited_once()+ # The template is re-checked next time in case it went away.+ forget.assert_called_once_with("base", _API_KEY)def test_redis_save_failure_kills_sandbox_and_releases_slot(self):"""If Redis save fails after creation, sandbox is killed and slot released."""diff --git a/autogpt_platform/backend/backend/util/e2b_template.py b/autogpt_platform/backend/backend/util/e2b_template.pynew file mode 100644index 0000000000..c8509dd1fc--- /dev/null+++ b/autogpt_platform/backend/backend/util/e2b_template.py@@ -0,0 +1,290 @@+"""The sandbox image CoPilot boxes run on, built on the E2B team on demand.++E2B fixes a sandbox's vCPU / RAM at template build time. Its public+``desktop`` image is 8 vCPU / 8 GiB (about $0.53 an hour running); we+re-snapshot it at 1 vCPU / 2 GiB under our own alias, which is cheaper to+run than E2B's ``base`` (2 vCPU / 512 MiB) with four times the RAM, and it+already carries XFCE, Chrome, Firefox and VS Code, so a box can later turn a+screen on without changing image. Nothing graphical starts at boot: a shell+box on this image idles at about 90 MiB.++Template aliases live per E2B team, so the first sandbox on a new team (or+key) has to build it. ``ensure_template`` checks the alias and builds it+from ``desktop`` when missing (12-25 s, once per team), serialised through+Redis so parallel first turns don't each start a build. Templates we don't+manage are left alone.++"Exists" is not "ready": E2B registers an alias the moment a build is+requested, before the build has run, and a failed build leaves the alias in+place with nothing usable behind it. Readiness therefore means the alias+resolves to a template with at least one ``ready`` build.+"""++import asyncio+import enum+import hashlib+import hmac+import logging+import uuid++from e2b import AsyncTemplate, Template+from e2b.api.client.api.templates import (+ get_templates_aliases_alias,+ get_templates_template_id,+)+from e2b.api.client.models import (+ TemplateAliasResponse,+ TemplateBuildStatus,+ TemplateWithBuilds,+)+from e2b.api.client_async import get_api_client+from e2b.connection_config import ConnectionConfig+from e2b.template.types import BuildInfo+from pydantic import BaseModel, ConfigDict++from backend.data.redis_client import get_redis_async++logger = logging.getLogger(__name__)+++class TemplateSpec(BaseModel):+ """A template we build ourselves: *source* re-snapshotted at a size."""++ model_config = ConfigDict(frozen=True)++ alias: str+ source: str+ cpu_count: int+ memory_mb: int++ @property+ def tags(self) -> list[str]:+ """Image-side metadata. ``default`` is what a bare alias resolves to."""+ # E2B tags take Docker-tag characters only, so no colons.+ return [+ "default",+ f"{self.cpu_count}x{self.memory_mb // 1024}",+ f"from-{self.source}",+ "built-by-platform",+ ]+++DESKTOP_IMAGE = TemplateSpec(+ alias="agpt-desktop-1x2", source="desktop", cpu_count=1, memory_mb=2048+)+MANAGED_TEMPLATES: dict[str, TemplateSpec] = {DESKTOP_IMAGE.alias: DESKTOP_IMAGE}++# A build takes 12-25 s. The build is cut off before the lock can expire, so+# the lock is only ever released by its owner (or by the TTL after a crash).+# Followers wait as long as the lock can live, so they never give up on a+# build that is still allowed to finish.+_BUILD_LOCK_TTL_SECONDS = 300+_BUILD_TIMEOUT_SECONDS = 240+_BUILD_WAIT_SECONDS = _BUILD_LOCK_TTL_SECONDS+_BUILD_POLL_SECONDS = 2.0++# Release or extend only if we still own the lock: a build that outlived the+# TTL must not touch the lock a later builder took.+_UNLOCK_SCRIPT = (+ 'if redis.call("get", KEYS[1]) == ARGV[1] then '+ 'return redis.call("del", KEYS[1]) else return 0 end'+)+_EXTEND_SCRIPT = (+ 'if redis.call("get", KEYS[1]) == ARGV[1] then '+ 'return redis.call("expire", KEYS[1], ARGV[2]) else return 0 end'+)++# Templates this process has already confirmed ready, keyed by alias and+# team: one round of API calls per alias per team per process lifetime.+_ready: set[str] = set()+# Checks in flight in this process, so concurrent first turns share one.+_inflight: dict[str, "asyncio.Future[None]"] = {}+++class TemplateState(enum.Enum):+ READY = "ready" # a build finished; sandboxes can be created+ BUILDING = "building" # a build is queued or running+ MISSING = "missing" # no alias, or an alias with no usable build+++async def ensure_template(template: str, api_key: str) -> None:+ """Make sure *template* is ready on the team before a sandbox is created from it.++ Only templates in ``MANAGED_TEMPLATES`` are ever built; anything else is+ assumed to be provisioned out of band and returns immediately.+ """+ spec = MANAGED_TEMPLATES.get(template)+ if spec is None:+ return+ cache_key = _scoped_key(spec, api_key)+ if cache_key in _ready:+ return+ check = _inflight.get(cache_key)+ if check is None:+ check = _inflight[cache_key] = asyncio.ensure_future(+ _check_or_provision(spec, api_key, cache_key)+ )+ try:+ await asyncio.shield(check)+ finally:+ _inflight.pop(cache_key, None)+ else:+ await asyncio.shield(check)+++async def _check_or_provision(spec: TemplateSpec, api_key: str, cache_key: str) -> None:+ try:+ if await get_template_state(spec, api_key) is not TemplateState.READY:+ await _provision(spec, api_key)+ except Exception:+ logger.error(+ "[E2B] Template %s is not available on this team and could not be "+ "built. Building it needs an E2B plan that allows template builds "+ "with a 20 GiB build disk. Set CHAT_E2B_SANDBOX_TEMPLATE=base to "+ "use E2B's stock image instead.",+ spec.alias,+ )+ raise+ _ready.add(cache_key)+++async def _provision(spec: TemplateSpec, api_key: str) -> None:+ """Build *spec* on the team, or wait for whichever process is building it."""+ redis = await get_redis_async()+ lock_key = f"e2b:template:{_scoped_key(spec, api_key)}:build"+ token = uuid.uuid4().hex+ if not await redis.set(lock_key, token, nx=True, ex=_BUILD_LOCK_TTL_SECONDS):+ if await _wait_until_ready(spec, api_key, lock_key) is not TemplateState.READY:+ raise RuntimeError(+ f"E2B template {spec.alias} was not built by the process "+ "holding the build lock"+ )+ return+ try:+ # Re-check under the lock: the previous holder may have just finished,+ # or a build started elsewhere (the CLI, a crashed holder) may be+ # running. Only a genuinely missing template gets a new build.+ state = await get_template_state(spec, api_key)+ if state is TemplateState.BUILDING:+ state = await _wait_until_ready(spec, api_key, builder_lock=None)+ if state is TemplateState.MISSING:+ # Watching that build may have used up most of the lock. Take a+ # fresh TTL so the build cannot outlive it; if the lock is no+ # longer ours, someone else is provisioning, so start over as a+ # follower (or the next holder).+ if not await redis.eval(+ _EXTEND_SCRIPT, 1, lock_key, token, _BUILD_LOCK_TTL_SECONDS+ ):+ return await _provision(spec, api_key)+ await asyncio.wait_for(+ build_template(spec, api_key), timeout=_BUILD_TIMEOUT_SECONDS+ )+ finally:+ await redis.eval(_UNLOCK_SCRIPT, 1, lock_key, token)+++async def build_template(spec: TemplateSpec, api_key: str) -> BuildInfo:+ """Build *spec* on the team the key belongs to (blocks until ready)."""+ logger.info(+ "[E2B] Building template %s (%d vCPU / %d MiB) from %s",+ spec.alias,+ spec.cpu_count,+ spec.memory_mb,+ spec.source,+ )+ info = await AsyncTemplate.build(+ Template().from_template(spec.source),+ spec.alias,+ tags=spec.tags,+ cpu_count=spec.cpu_count,+ memory_mb=spec.memory_mb,+ api_key=api_key,+ )+ logger.info("[E2B] Built template %s (%s)", spec.alias, info.template_id)+ return info+++async def get_template_state(spec: TemplateSpec, api_key: str) -> TemplateState:+ """What the team has behind *spec*'s alias right now."""+ async with get_api_client(ConnectionConfig(api_key=api_key)) as client:+ alias = await get_templates_aliases_alias.asyncio_detailed(+ alias=spec.alias, client=client+ )+ if alias.status_code == 404:+ return TemplateState.MISSING+ if alias.status_code == 403:+ # The alias exists on another team: we can neither inspect its+ # builds nor build our own under that name, so use it as-is.+ return TemplateState.READY+ if not isinstance(alias.parsed, TemplateAliasResponse):+ raise RuntimeError(+ f"E2B alias lookup for {spec.alias} failed: HTTP {alias.status_code}"+ )+ template = await get_templates_template_id.asyncio_detailed(+ template_id=alias.parsed.template_id, client=client+ )+ if not isinstance(template.parsed, TemplateWithBuilds):+ raise RuntimeError(+ f"E2B template lookup for {spec.alias} failed: HTTP {template.status_code}"+ )+ statuses = {build.status for build in template.parsed.builds}+ if TemplateBuildStatus.READY in statuses:+ return TemplateState.READY+ if statuses & {TemplateBuildStatus.BUILDING, TemplateBuildStatus.WAITING}:+ return TemplateState.BUILDING+ return TemplateState.MISSING+++def forget_ready_templates() -> None:+ """Drop the process-level cache (tests, or after a template is deleted)."""+ _ready.clear()+ _inflight.clear()+++def forget_template(template: str, api_key: str) -> None:+ """Re-check *template* on the next ``ensure_template``.++ Called when creating a sandbox from it failed: the template may have+ been deleted out of band since this process confirmed it.+ """+ spec = MANAGED_TEMPLATES.get(template)+ if spec is not None:+ _ready.discard(_scoped_key(spec, api_key))+++def _scoped_key(spec: TemplateSpec, api_key: str) -> str:+ """Alias plus a fingerprint of the team's key: aliases live per team.++ The fingerprint only namespaces cache and lock keys. It is a keyed MAC+ of a fixed label, so the key is never hashed as data and the value does+ not lead back to it.+ """+ team = hmac.new(api_key.encode(), b"e2b-template", hashlib.sha256).hexdigest()+ return f"{spec.alias}@{team[:16]}"+++async def _wait_until_ready(+ spec: TemplateSpec, api_key: str, builder_lock: str | None+) -> TemplateState:+ """Wait for a build this process did not start.++ *builder_lock* is the lock the building process holds, or ``None`` when+ this process holds the lock itself and is watching a build started+ elsewhere. Returns READY, or MISSING once the watched build has failed:+ for a lock holder that is the moment the state drops out of BUILDING, for+ a follower it is MISSING with the builder's lock gone. Waiting any longer+ would only run out the clock.+ """+ redis = await get_redis_async()+ for _ in range(int(_BUILD_WAIT_SECONDS / _BUILD_POLL_SECONDS)):+ state = await get_template_state(spec, api_key)+ if state is TemplateState.READY:+ return state+ if state is TemplateState.MISSING and (+ builder_lock is None or not await redis.exists(builder_lock)+ ):+ return state+ await asyncio.sleep(_BUILD_POLL_SECONDS)+ raise TimeoutError(+ f"E2B template {spec.alias} was not ready within {_BUILD_WAIT_SECONDS}s"+ )diff --git a/autogpt_platform/backend/backend/util/e2b_template_test.py b/autogpt_platform/backend/backend/util/e2b_template_test.pynew file mode 100644index 0000000000..e60dcfff47--- /dev/null+++ b/autogpt_platform/backend/backend/util/e2b_template_test.py@@ -0,0 +1,432 @@+import asyncio+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from e2b.api.client.models import TemplateAliasResponse, TemplateBuildStatus++from backend.copilot.config import ChatConfig+from backend.util import e2b_template+from backend.util.e2b_template import (+ DESKTOP_IMAGE,+ TemplateSpec,+ TemplateState,+ ensure_template,+ forget_ready_templates,+ get_template_state,+)++_M = "backend.util.e2b_template"+_KEY = "e2b_test"+_OTHER_KEY = "e2b_other_team"++[email protected](autouse=True)+def _fresh_cache():+ forget_ready_templates()+ yield+ forget_ready_templates()+++def _redis(lock_acquired: bool, lock_present: bool = True) -> MagicMock:+ redis = MagicMock()+ redis.set = AsyncMock(return_value=lock_acquired)+ redis.eval = AsyncMock(return_value=1)+ redis.exists = AsyncMock(return_value=lock_present)+ return redis+++def _states(*states: TemplateState) -> AsyncMock:+ return AsyncMock(side_effect=list(states))+++class TestSpec:+ def test_the_managed_image_is_the_copilot_default(self):+ assert ChatConfig().e2b_sandbox_template == DESKTOP_IMAGE.alias+ assert DESKTOP_IMAGE.cpu_count == 1 and DESKTOP_IMAGE.memory_mb == 2048++ def test_tags_are_docker_safe_and_resolve_the_bare_alias(self):+ tags = TemplateSpec(+ alias="x", source="desktop", cpu_count=2, memory_mb=4096+ ).tags+ assert tags[0] == "default"+ assert "2x4" in tags and "from-desktop" in tags+ assert all(":" not in t for t in tags)+++class TestEnsureTemplate:+ @pytest.mark.asyncio+ async def test_unmanaged_template_is_left_alone(self):+ with patch(f"{_M}.get_template_state", AsyncMock()) as state:+ await ensure_template("base", _KEY)+ state.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_ready_template_is_checked_once_per_team_per_process(self):+ with patch(+ f"{_M}.get_template_state", _states(*[TemplateState.READY] * 2)+ ) as st:+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)+ await ensure_template(DESKTOP_IMAGE.alias, _OTHER_KEY)+ # Same alias, other team: the alias may not exist there, so check again.+ assert st.await_count == 2+ assert st.await_args_list[1].args == (DESKTOP_IMAGE, _OTHER_KEY)++ @pytest.mark.asyncio+ async def test_missing_template_is_built_at_the_managed_size(self):+ redis = _redis(lock_acquired=True)+ with (+ patch(f"{_M}.get_template_state", _states(*[TemplateState.MISSING] * 2)),+ patch(f"{_M}.AsyncTemplate") as tpl,+ patch(f"{_M}.Template") as template_cls,+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ ):+ tpl.build = AsyncMock(return_value=MagicMock(template_id="t1"))+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)++ template_cls.return_value.from_template.assert_called_once_with("desktop")+ kwargs = tpl.build.await_args.kwargs+ assert tpl.build.await_args.args[1] == DESKTOP_IMAGE.alias+ assert kwargs["cpu_count"] == 1 and kwargs["memory_mb"] == 2048+ assert kwargs["tags"][0] == "default" and kwargs["api_key"] == _KEY+ assert f"{DESKTOP_IMAGE.alias}@" in next(iter(e2b_template._ready))++ @pytest.mark.asyncio+ async def test_lock_is_scoped_to_the_team_and_released_by_token(self):+ redis = _redis(lock_acquired=True)+ with (+ patch(f"{_M}.get_template_state", _states(*[TemplateState.MISSING] * 4)),+ patch(f"{_M}.AsyncTemplate") as tpl,+ patch(f"{_M}.Template"),+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ ):+ tpl.build = AsyncMock(return_value=MagicMock(template_id="t1"))+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)+ await ensure_template(DESKTOP_IMAGE.alias, _OTHER_KEY)++ (key_a, token_a), (key_b, token_b) = [+ c.args[:2] for c in redis.set.await_args_list+ ]+ assert key_a != key_b and _KEY not in key_a and _OTHER_KEY not in key_b+ assert redis.set.await_args_list[0].kwargs == {"nx": True, "ex": 300}+ # The TTL is re-asserted with our token right before building, and the+ # release is a compare-and-delete with the same token, never a bare DEL.+ evals = [c.args for c in redis.eval.await_args_list]+ assert evals == [+ (e2b_template._EXTEND_SCRIPT, 1, key_a, token_a, 300),+ (e2b_template._UNLOCK_SCRIPT, 1, key_a, token_a),+ (e2b_template._EXTEND_SCRIPT, 1, key_b, token_b, 300),+ (e2b_template._UNLOCK_SCRIPT, 1, key_b, token_b),+ ]++ @pytest.mark.asyncio+ async def test_lock_winner_rechecks_before_building(self):+ redis = _redis(lock_acquired=True)+ with (+ patch(+ f"{_M}.get_template_state",+ _states(TemplateState.MISSING, TemplateState.READY),+ ),+ patch(f"{_M}.AsyncTemplate") as tpl,+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ ):+ tpl.build = AsyncMock()+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)+ tpl.build.assert_not_awaited()+ redis.eval.assert_awaited_once()++ @pytest.mark.asyncio+ async def test_lock_winner_waits_for_a_build_already_in_flight(self):+ redis = _redis(lock_acquired=True)+ with (+ patch(+ f"{_M}.get_template_state",+ _states(+ TemplateState.MISSING,+ TemplateState.BUILDING,+ TemplateState.BUILDING,+ TemplateState.READY,+ ),+ ),+ patch(f"{_M}.AsyncTemplate") as tpl,+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ patch(f"{_M}.asyncio.sleep", AsyncMock()),+ ):+ tpl.build = AsyncMock()+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)+ tpl.build.assert_not_awaited()+ redis.eval.assert_awaited_once()++ @pytest.mark.asyncio+ async def test_lock_winner_builds_when_the_watched_build_fails(self):+ redis = _redis(lock_acquired=True)+ with (+ patch(+ f"{_M}.get_template_state",+ _states(+ TemplateState.MISSING,+ TemplateState.BUILDING,+ TemplateState.BUILDING,+ TemplateState.MISSING,+ ),+ ),+ patch(f"{_M}.AsyncTemplate") as tpl,+ patch(f"{_M}.Template"),+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ patch(f"{_M}.asyncio.sleep", AsyncMock()) as sleep,+ ):+ tpl.build = AsyncMock(return_value=MagicMock(template_id="t1"))+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)+ # The holder owns the lock, so failure is read from the build state+ # alone: BUILDING dropping to MISSING means build now, not wait 180 s.+ tpl.build.assert_awaited_once()+ assert sleep.await_count == 1+ redis.exists.assert_not_awaited()++ def test_followers_wait_as_long_as_the_lock_can_live(self):+ assert e2b_template._BUILD_WAIT_SECONDS >= e2b_template._BUILD_LOCK_TTL_SECONDS+ assert (+ e2b_template._BUILD_TIMEOUT_SECONDS < e2b_template._BUILD_LOCK_TTL_SECONDS+ )++ @pytest.mark.asyncio+ async def test_fingerprint_is_not_a_plain_hash_of_the_key(self):+ import hashlib++ key = e2b_template._scoped_key(DESKTOP_IMAGE, _KEY)+ assert key.startswith(f"{DESKTOP_IMAGE.alias}@") and _KEY not in key+ assert hashlib.sha256(_KEY.encode()).hexdigest()[:16] not in key+ assert key != e2b_template._scoped_key(DESKTOP_IMAGE, _OTHER_KEY)++ @pytest.mark.asyncio+ async def test_lost_lock_is_not_built_on(self):+ """A holder whose lock lapsed while it watched a foreign build steps+ back: it becomes a follower of whoever holds the lock now."""+ redis = _redis(lock_acquired=True)+ redis.set = AsyncMock(side_effect=[True, False])++ async def eval_(script, *args):+ return 0 if script is e2b_template._EXTEND_SCRIPT else 1++ redis.eval = AsyncMock(side_effect=eval_)+ with (+ patch(+ f"{_M}.get_template_state",+ _states(+ TemplateState.MISSING,+ TemplateState.BUILDING,+ TemplateState.MISSING,+ TemplateState.READY,+ ),+ ),+ patch(f"{_M}.AsyncTemplate") as tpl,+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ patch(f"{_M}.asyncio.sleep", AsyncMock()),+ ):+ tpl.build = AsyncMock()+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)+ tpl.build.assert_not_awaited()+ assert redis.set.await_count == 2+ assert len(e2b_template._ready) == 1++ @pytest.mark.asyncio+ async def test_concurrent_first_turns_share_one_check(self):+ gate = asyncio.Event()++ async def slow_state(*_args):+ await gate.wait()+ return TemplateState.READY++ with patch(f"{_M}.get_template_state", AsyncMock(side_effect=slow_state)) as st:+ first = asyncio.ensure_future(ensure_template(DESKTOP_IMAGE.alias, _KEY))+ second = asyncio.ensure_future(ensure_template(DESKTOP_IMAGE.alias, _KEY))+ await asyncio.sleep(0)+ gate.set()+ await asyncio.gather(first, second)+ st.assert_awaited_once()+ assert not e2b_template._inflight++ @pytest.mark.asyncio+ async def test_failure_names_the_opt_out(self, caplog):+ with (+ patch(f"{_M}.get_template_state", AsyncMock(side_effect=OSError("403"))),+ caplog.at_level("ERROR"),+ ):+ with pytest.raises(OSError):+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)+ assert "CHAT_E2B_SANDBOX_TEMPLATE=base" in caplog.text+ assert not e2b_template._inflight++ def test_forget_template_forces_a_recheck(self):+ e2b_template._ready.add(e2b_template._scoped_key(DESKTOP_IMAGE, _KEY))+ e2b_template.forget_template("base", _KEY)+ assert e2b_template._ready+ e2b_template.forget_template(DESKTOP_IMAGE.alias, _KEY)+ assert not e2b_template._ready++ @pytest.mark.asyncio+ async def test_build_failure_releases_the_lock_and_propagates(self):+ redis = _redis(lock_acquired=True)+ with (+ patch(f"{_M}.get_template_state", _states(*[TemplateState.MISSING] * 2)),+ patch(f"{_M}.AsyncTemplate") as tpl,+ patch(f"{_M}.Template"),+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ ):+ tpl.build = AsyncMock(side_effect=RuntimeError("build failed"))+ with pytest.raises(RuntimeError):+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)+ assert redis.eval.await_args.args[0] is e2b_template._UNLOCK_SCRIPT+ assert not e2b_template._ready++ @pytest.mark.asyncio+ async def test_stalled_build_is_cut_off_under_the_lock_ttl(self):+ redis = _redis(lock_acquired=True)++ async def never_finishes(*_args, **_kwargs):+ await asyncio.Event().wait()++ with (+ patch(f"{_M}.get_template_state", _states(*[TemplateState.MISSING] * 2)),+ patch(f"{_M}.AsyncTemplate") as tpl,+ patch(f"{_M}.Template"),+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ patch(f"{_M}._BUILD_TIMEOUT_SECONDS", 0.01),+ ):+ tpl.build = AsyncMock(side_effect=never_finishes)+ with pytest.raises(asyncio.TimeoutError):+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)+ assert (+ e2b_template._BUILD_TIMEOUT_SECONDS < e2b_template._BUILD_LOCK_TTL_SECONDS+ )+ assert redis.eval.await_args.args[0] is e2b_template._UNLOCK_SCRIPT+ assert not e2b_template._ready++ @pytest.mark.asyncio+ async def test_follower_waits_for_the_other_builder(self):+ redis = _redis(lock_acquired=False)+ with (+ patch(+ f"{_M}.get_template_state",+ _states(+ TemplateState.MISSING,+ TemplateState.MISSING,+ TemplateState.BUILDING,+ TemplateState.READY,+ ),+ ) as st,+ patch(f"{_M}.AsyncTemplate") as tpl,+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ patch(f"{_M}.asyncio.sleep", AsyncMock()),+ ):+ tpl.build = AsyncMock()+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)+ tpl.build.assert_not_awaited()+ redis.eval.assert_not_awaited()+ assert st.await_count == 4+ assert len(e2b_template._ready) == 1++ @pytest.mark.asyncio+ async def test_follower_stops_when_the_builder_gave_up(self):+ redis = _redis(lock_acquired=False, lock_present=False)+ with (+ patch(f"{_M}.get_template_state", _states(*[TemplateState.MISSING] * 2)),+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ patch(f"{_M}.asyncio.sleep", AsyncMock()) as sleep,+ ):+ with pytest.raises(RuntimeError, match="was not built"):+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)+ sleep.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_follower_gives_up_eventually(self):+ redis = _redis(lock_acquired=False)+ with (+ patch(+ f"{_M}.get_template_state",+ AsyncMock(return_value=TemplateState.BUILDING),+ ),+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ patch(f"{_M}.asyncio.sleep", AsyncMock()),+ ):+ with pytest.raises(TimeoutError):+ await ensure_template(DESKTOP_IMAGE.alias, _KEY)+ assert not e2b_template._ready+++def _client() -> MagicMock:+ client = MagicMock()+ client.__aenter__ = AsyncMock(return_value=client)+ client.__aexit__ = AsyncMock(return_value=False)+ return client+++def _response(status_code: int, parsed=None) -> MagicMock:+ return MagicMock(status_code=status_code, parsed=parsed)+++def _template(*statuses: TemplateBuildStatus) -> MagicMock:+ from e2b.api.client.models import TemplateWithBuilds++ template = MagicMock(spec=TemplateWithBuilds)+ template.builds = [MagicMock(status=s) for s in statuses]+ return template+++class TestGetTemplateState:+ async def _state(self, alias_response, template_response=None) -> TemplateState:+ with (+ patch(f"{_M}.get_api_client", return_value=_client()),+ patch(+ f"{_M}.get_templates_aliases_alias.asyncio_detailed",+ AsyncMock(return_value=alias_response),+ ),+ patch(+ f"{_M}.get_templates_template_id.asyncio_detailed",+ AsyncMock(return_value=template_response),+ ) as by_id,+ ):+ state = await get_template_state(DESKTOP_IMAGE, _KEY)+ if template_response is not None:+ by_id.assert_awaited_once()+ assert by_id.await_args.kwargs["template_id"] == "tpl-1"+ return state++ @pytest.mark.asyncio+ async def test_unknown_alias_is_missing(self):+ assert await self._state(_response(404)) is TemplateState.MISSING++ @pytest.mark.asyncio+ async def test_another_teams_alias_is_used_as_is(self):+ assert await self._state(_response(403)) is TemplateState.READY++ @pytest.mark.asyncio+ async def test_alias_lookup_failure_raises(self):+ with pytest.raises(RuntimeError, match="alias lookup"):+ await self._state(_response(500))++ @pytest.mark.asyncio+ async def test_a_finished_build_means_ready(self):+ alias = _response(200, TemplateAliasResponse(template_id="tpl-1", public=False))+ template = _response(+ 200, _template(TemplateBuildStatus.ERROR, TemplateBuildStatus.READY)+ )+ assert await self._state(alias, template) is TemplateState.READY++ @pytest.mark.asyncio+ async def test_a_registered_alias_with_a_running_build_is_not_ready(self):+ alias = _response(200, TemplateAliasResponse(template_id="tpl-1", public=False))+ template = _response(200, _template(TemplateBuildStatus.WAITING))+ assert await self._state(alias, template) is TemplateState.BUILDING++ @pytest.mark.asyncio+ async def test_a_failed_build_leaves_the_template_missing(self):+ alias = _response(200, TemplateAliasResponse(template_id="tpl-1", public=False))+ template = _response(200, _template(TemplateBuildStatus.ERROR))+ assert await self._state(alias, template) is TemplateState.MISSING++ @pytest.mark.asyncio+ async def test_template_lookup_failure_raises(self):+ alias = _response(200, TemplateAliasResponse(template_id="tpl-1", public=False))+ with pytest.raises(RuntimeError, match="template lookup"):+ await self._state(alias, _response(500))diff --git a/autogpt_platform/backend/poetry.lock b/autogpt_platform/backend/poetry.lockindex 30b28f1c90..61bb1c164b 100644--- a/autogpt_platform/backend/poetry.lock+++ b/autogpt_platform/backend/poetry.lock@@ -1053,6 +1053,22 @@ files = []markers = {dev = "platform_system == \"Windows\" or sys_platform == \"win32\""}+[[package]]+name = "connectrpc"+version = "0.11.1"+description = "Server and client runtime library for Connect RPC"+optional = false+python-versions = ">=3.10"+groups = ["main"]+files = [+ {file = "connectrpc-0.11.1-py3-none-any.whl", hash = "sha256:8a52e2e92a485fa9681c1101a79a5ebeb31807e3ea3d5aabd41f484a6398bc7b"},+ {file = "connectrpc-0.11.1.tar.gz", hash = "sha256:18277f7838847b4271ca38d40c7d2387b5a2ea6a29f240689c19e1ec84aaff66"},+]++[package.dependencies]+protobuf-py = "0.1.1"+pyqwest = ">=0.5.1"+[[package]]name = "coverage"version = "7.13.5"@@ -1455,43 +1471,45 @@ pgp = ["gpg"][[package]]name = "e2b"-version = "2.15.2"+version = "2.46.4"description = "E2B SDK that give agents cloud environments"optional = false-python-versions = "<4.0,>=3.10"+python-versions = ">=3.10"groups = ["main"]files = [- {file = "e2b-2.15.2-py3-none-any.whl", hash = "sha256:19a56fbdea25974dc81426ed48337eae6cea91d404f5bcf8861a5a2c6e4d982a"},- {file = "e2b-2.15.2.tar.gz", hash = "sha256:414379d2421d6827eeb2eb50a4d6b3fdb7d691b39ff73b5ea05ca4b532819831"},+ {file = "e2b-2.46.4-py3-none-any.whl", hash = "sha256:5d77e9fee8571a0d24a179f14244984127d18bd93a99a5e90226c876087035b5"},+ {file = "e2b-2.46.4.tar.gz", hash = "sha256:777d35beb00194b401a6c4ac2498aa836628839649f17aae86e85920382c8f6c"},][package.dependencies]attrs = ">=23.2.0"-dockerfile-parse = ">=2.0.1,<3.0.0"-httpcore = ">=1.0.5,<2.0.0"+connectrpc = ">=0.11.1,<0.12"+dockerfile-parse = ">=2.0.1,<3"+h2 = ">=4.4.1,<5"httpx = ">=0.27.0,<1.0.0"packaging = ">=24.1"-protobuf = ">=4.21.0"+protobuf-py = ">=0.1.1,<0.2"+pyqwest = ">=0.10.0,<0.11"python-dateutil = ">=2.8.2"rich = ">=14.0.0"-typing-extensions = ">=4.1.0"-wcmatch = ">=10.1,<11.0"+typing-extensions = ">=4.10.0"+wcmatch = ">=10.1,<12"[[package]]name = "e2b-code-interpreter"-version = "2.4.1"+version = "2.9.2"description = "E2B Code Interpreter - Stateful code execution"optional = false-python-versions = "<4.0,>=3.9"+python-versions = "<4.0,>=3.10"groups = ["main"]files = [- {file = "e2b_code_interpreter-2.4.1-py3-none-any.whl", hash = "sha256:15d35f025b4a15033e119f2e12e7ac65657ad2b5a013fa9149e74581fbee778a"},- {file = "e2b_code_interpreter-2.4.1.tar.gz", hash = "sha256:4b15014ee0d0dfcdc3072e1f409cbb87ca48f48d53d75629b7257e5513b9e7dd"},+ {file = "e2b_code_interpreter-2.9.2-py3-none-any.whl", hash = "sha256:1acf7be290050939ead3c1bae7f55c73433c2ee8e55c5e0e75d6d8e168b6bca0"},+ {file = "e2b_code_interpreter-2.9.2.tar.gz", hash = "sha256:e04d3bbed56af32649f2c24c4f24419d1588f23b73ead60c8086f591e7f6f76c"},][package.dependencies]attrs = ">=21.3.0"-e2b = ">=2.7.0,<3.0.0"+e2b = ">=2.39.1,<3.0.0"httpx = ">=0.20.0,<1.0.0"[[package]]@@ -2632,30 +2650,30 @@ files = [[[package]]name = "h2"-version = "4.3.0"+version = "4.4.1"description = "Pure-Python HTTP/2 protocol implementation"optional = false-python-versions = ">=3.9"+python-versions = ">=3.10"groups = ["main"]files = [- {file = "h2-4.3.0-py3-none-any.whl", hash = "sha256:c438f029a25f7945c69e0ccf0fb951dc3f73a5f6412981daee861431b70e2bdd"},- {file = "h2-4.3.0.tar.gz", hash = "sha256:6c59efe4323fa18b47a632221a1888bd7fde6249819beda254aeca909f221bf1"},+ {file = "h2-4.4.1-py3-none-any.whl", hash = "sha256:0e25f1462b23c9cb82d9eb02e28bc706dac2a68cb457c6a0d74d63c8a2a5d0e6"},+ {file = "h2-4.4.1.tar.gz", hash = "sha256:4e866ffb1a869ae14dd9b5e6beb5c24a13da0495ad72b65925ded182521c1516"},][package.dependencies]-hpack = ">=4.1,<5"+hpack = ">=4.2,<5"hyperframe = ">=6.1,<7"[[package]]name = "hpack"-version = "4.1.0"+version = "4.2.0"description = "Pure-Python HPACK header encoding"optional = false-python-versions = ">=3.9"+python-versions = ">=3.10"groups = ["main"]files = [- {file = "hpack-4.1.0-py3-none-any.whl", hash = "sha256:157ac792668d995c657d93111f46b4535ed114f0c9c8d672271bbec7eae1b496"},- {file = "hpack-4.1.0.tar.gz", hash = "sha256:ec5eca154f7056aa06f196a557655c5b009b382873ac8d1e66e79e87535f1dca"},+ {file = "hpack-4.2.0-py3-none-any.whl", hash = "sha256:858ac0b02280fa582b5080d68db0899c62a80375e0e5413a74970c5e518b6986"},+ {file = "hpack-4.2.0.tar.gz", hash = "sha256:0895cfa3b5531fc65fe439c05eb65144f123bf7a394fcaa56aa423548d8e45c0"},][[package]]@@ -5164,6 +5182,60 @@ files = [{file = "protobuf-5.29.6.tar.gz", hash = "sha256:da9ee6a5424b6b30fd5e45c5ea663aef540ca95f9ad99d1e887e819cdf9b8723"},]+[[package]]+name = "protobuf-py"+version = "0.1.1"+description = "Idiomatic Protocol Buffers for Python."+optional = false+python-versions = ">=3.10"+groups = ["main"]+files = [+ {file = "protobuf_py-0.1.1-py3-none-any.whl", hash = "sha256:efc4f50f275ed6dae10a1f30bb81ad1a75368557b3ff22a532b7a472050368f1"},+ {file = "protobuf_py-0.1.1.tar.gz", hash = "sha256:6bd08ac4d8f1661965bbe2685429d79043704cdd1ee720a7a89617331742240b"},+]++[package.dependencies]+protobuf-py-ext = {version = "0.1.1", markers = "(platform_machine == \"AMD64\" or platform_machine == \"ARM64\") and platform_python_implementation == \"CPython\" and sys_platform == \"win32\" or (platform_machine == \"aarch64\" or platform_machine == \"x86_64\") and platform_python_implementation == \"CPython\" and sys_platform == \"linux\" or platform_machine == \"arm64\" and platform_python_implementation == \"CPython\" and sys_platform == \"darwin\""}++[[package]]+name = "protobuf-py-ext"+version = "0.1.1"+description = "Native extension for protobuf-py"+optional = false+python-versions = ">=3.10"+groups = ["main"]+markers = "(platform_machine == \"AMD64\" or platform_machine == \"ARM64\") and platform_python_implementation == \"CPython\" and sys_platform == \"win32\" or (platform_machine == \"aarch64\" or platform_machine == \"x86_64\") and platform_python_implementation == \"CPython\" and sys_platform == \"linux\" or platform_machine == \"arm64\" and platform_python_implementation == \"CPython\" and sys_platform == \"darwin\""+files = [+ {file = "protobuf_py_ext-0.1.1-cp310-abi3-macosx_11_0_arm64.whl", hash = "sha256:310039e03cb15181781a0b78017419f6d4ee302e988c3c70b87f1facdf05532d"},+ {file = "protobuf_py_ext-0.1.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:89ec8348d1ba045f79b2fedd14e40cca36f2a41b52f2c4fdf55a60c58add2353"},+ {file = "protobuf_py_ext-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:182798e4861aba72d05855bd06febe4926aa7265e6f444a1b8af5252beee4f7e"},+ {file = "protobuf_py_ext-0.1.1-cp310-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:9eb25c3a329c0551cc86b209a5e5d8ecb8d834b9924a3aa019377853a703b6d3"},+ {file = "protobuf_py_ext-0.1.1-cp310-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:aaecbde82bef10c7c40578cbb61b7a19896bf7fa450972050a3bb302acb7d5d6"},+ {file = "protobuf_py_ext-0.1.1-cp310-abi3-win_amd64.whl", hash = "sha256:6b0c615c48e95acc53cf33e9310eeaff8b30d2d7555bf93e7bca8fb4f40e9a5c"},+ {file = "protobuf_py_ext-0.1.1-cp310-abi3-win_arm64.whl", hash = "sha256:72956cd0af5dee24b41c6f5ba5e42622d17e6d555002b5efc1634e27a1446de2"},+ {file = "protobuf_py_ext-0.1.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:c6f0cd58620f415a3534d195358338f4999a774e12510f91c592b38d13d37388"},+ {file = "protobuf_py_ext-0.1.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:21572764f625d829604fc4d83635f533f35775f11c6da142345b3f3c8d64bd09"},+ {file = "protobuf_py_ext-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e7f14f00c2678bfbe7ad46f057b9f9938c1677bcf39e405e163e272c6f9814b8"},+ {file = "protobuf_py_ext-0.1.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:1610865622e2e27568277ca63d8d2d23dcc55eaa767398865c21539ddf4ce24d"},+ {file = "protobuf_py_ext-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:8995efba9476e1ea18ef9873306871dba697308994bc2766558fec3387acc3de"},+ {file = "protobuf_py_ext-0.1.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:ba61d49dace8f874361583030a7c48139b42eb37c9ffbb1e7e8a227a51576f44"},+ {file = "protobuf_py_ext-0.1.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:9a9c2f026096ca4c595c89a297067ef371e241d3ff8e1f6d7c779aa8419cdca7"},+ {file = "protobuf_py_ext-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:cf78707a040b9294e5e1ec4a1875f0046acfe52e92150cea27de4e9fc9db39bb"},+ {file = "protobuf_py_ext-0.1.1-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:ae4373845cbb85bdade3ef5368cc2f4b5f80bf173383afc1ab063f95644e5599"},+ {file = "protobuf_py_ext-0.1.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:2a4cc478eef7a2acc1daaebcde479a3ac2396d47e6bdc7e776ca4c4147ba8b4c"},+ {file = "protobuf_py_ext-0.1.1-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:359dccdc1c3eafed2a913c570bb082b8df848a1d27d548ce8385f246a7d68be2"},+ {file = "protobuf_py_ext-0.1.1-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:91c38b4a10a306366443273ee03ca554537a0965bf05b7ada8e7dbdee04cc93e"},+ {file = "protobuf_py_ext-0.1.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:79eee3bcbb289d6ea114eb8fe3a1469c5b59bf53e207238469f567d9c53ba56f"},+ {file = "protobuf_py_ext-0.1.1-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:10992141a8282a71ac3e3530d1a489efb27618d84000b9a47918cf70e5816d9b"},+ {file = "protobuf_py_ext-0.1.1-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:4411ffe0e06a774b83c5c71c546ce097640a25f596c45f95f53d3e3148e3f22d"},+ {file = "protobuf_py_ext-0.1.1-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:55a17d80ea419501ff221a6627523f38a431bb33e6aa3de81ae3a7f271c49c75"},+ {file = "protobuf_py_ext-0.1.1-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:9dbb518b5638403ceaa08ac4fc7dac626f45ed9b856b3517de3906cf3de4d632"},+ {file = "protobuf_py_ext-0.1.1-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8a4adc65ab6a5e4885c67fc808bcacb83d755d05b566d312a0e10c2a873f2ad4"},+ {file = "protobuf_py_ext-0.1.1-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:e97f45c49676efacfb8e95bfcb1a002bc337e618a6780be1e55df2ba5ebd2f1e"},+ {file = "protobuf_py_ext-0.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:53a6b3590f6aa7f97b8ed60f62fef9b096babfeae82f7283fd3a4c405827d4f8"},+ {file = "protobuf_py_ext-0.1.1.tar.gz", hash = "sha256:e85bfdfdb3ed50634db8ccc7429dd9286520109489c735463971a418707b4fef"},+]+[[package]]name = "psutil"version = "7.2.2"@@ -5869,6 +5941,62 @@ files = [{file = "pyproject_hooks-1.2.0.tar.gz", hash = "sha256:1e859bd5c40fae9448642dd871adf459e5e2084186e8d2c2a79a824c970da1f8"},]+[[package]]+name = "pyqwest"+version = "0.10.0"+description = "A modern, high-performance HTTP client for Python and Rust."+optional = false+python-versions = ">=3.10"+groups = ["main"]+files = [+ {file = "pyqwest-0.10.0-cp310-abi3-macosx_10_12_x86_64.whl", hash = "sha256:aa492d5777dd145a60795ed95d9d4707a3cd1091fdcdfc93a82ac7fdc43ebacd"},+ {file = "pyqwest-0.10.0-cp310-abi3-macosx_11_0_arm64.whl", hash = "sha256:59f3f16628e518c674102e7b5fcff2101bba6abb4f6737ec5fade9b9278e6a53"},+ {file = "pyqwest-0.10.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d6e7db305a8318b1f3218053e87501f8f245ca8bd63e948e0282d04bf0883470"},+ {file = "pyqwest-0.10.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a5c757cfac5f53c8671dcb4850d5fc4c4339ea3e90636331c9318f8e3ddabc06"},+ {file = "pyqwest-0.10.0-cp310-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:234b3f71e3f314d997c203d8cf829b7117edd041153f9c277d0060ab90134148"},+ {file = "pyqwest-0.10.0-cp310-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:5637256a0dac0ef57e0eaa02b032014965e4a4c995e1deca1b1b97e6d1765f78"},+ {file = "pyqwest-0.10.0-cp310-abi3-win_amd64.whl", hash = "sha256:7ea761937acf3a00d1a7e70e982949d18946e5471d1419266ab3a78bbfa19759"},+ {file = "pyqwest-0.10.0-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:a21f1f15252a8303623b4f17b9c6de595ace11b3ade07f2adb6d07121e8191aa"},+ {file = "pyqwest-0.10.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:eb472c6e5d6833ebfec79db310e426eb17b01ac64c0e2c251bd9192c0d2ee0c5"},+ {file = "pyqwest-0.10.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:83578e24cccd5e0dc04d60a0af7bfb43325b5f22d03ff74ff79ed0ecf553b50d"},+ {file = "pyqwest-0.10.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6bb511c434f79c641efb5573e5795e56dc972252f4b96e52a9636d4ece5231a4"},+ {file = "pyqwest-0.10.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:1aaccd8a9db9430b2aedb5bad8ead80742cbc056b85c229516c70dc80539f906"},+ {file = "pyqwest-0.10.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:73d9eb438ab4a957a1ce0619d3af8c1c1126bfb9181033b123d792fcf4224531"},+ {file = "pyqwest-0.10.0-cp312-cp312-win_amd64.whl", hash = "sha256:317a74d633abe3bc5bccabf479e069c515dab9e6a755274b0ccb1d8a5bbfede3"},+ {file = "pyqwest-0.10.0-cp313-cp313-macosx_10_12_x86_64.whl", hash = "sha256:3978e794b9cfd8eaa500fb5d7aee63bc6172c605efa0abc1f62d85485bc049e1"},+ {file = "pyqwest-0.10.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:715991fd4f04862cd7a9d7452daabcdbd74dff4dff55eb20c22d60382dc2a4ed"},+ {file = "pyqwest-0.10.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c04798bed79c1dfa0e5b0e30fb137124311083490d44d6dfbe068d3dd254349e"},+ {file = "pyqwest-0.10.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:35b472877e73dd63fed089c2bc8fa198407f005c8c19e0a93f025ebefde01a81"},+ {file = "pyqwest-0.10.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:564ec360b7848b35e009038ffbca00466305a9708ab21829477f64aa8cad4c64"},+ {file = "pyqwest-0.10.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:b5c80e88a5967c1cadb3237c450f91a84a3683f8838c8dca96f09fee3612e762"},+ {file = "pyqwest-0.10.0-cp313-cp313-win_amd64.whl", hash = "sha256:dc3d80b402fb59dbe015e25993ac8147456fb231a4c949f92a89f31315ad50f9"},+ {file = "pyqwest-0.10.0-cp314-cp314-macosx_10_12_x86_64.whl", hash = "sha256:23a28beb55fa6d975949bffae4adfb69378f3229bb5cbd71231e95bf66f5b26c"},+ {file = "pyqwest-0.10.0-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:e4415ae40b8eedb1713dab14d7f9fecc3f79d26f3206c561087b88b99d5ce24b"},+ {file = "pyqwest-0.10.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:14b875d2273212d7fa8e4b755d8d736ffd226b1c707a9c0017dfdc8393a96eca"},+ {file = "pyqwest-0.10.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b5071491e416ea54e3b95bf9ffbed0bd065b093cb96e10a75c3d8f2cbe3c9823"},+ {file = "pyqwest-0.10.0-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:b68b5e68d513a4c63a072f8f40e38015160cf90bfbf7e8ef7c3935ca87e9e022"},+ {file = "pyqwest-0.10.0-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:c48910d27820b9c46fcd001b0fe514a3cf47d4784f59512dcdb8c91c395f82e4"},+ {file = "pyqwest-0.10.0-cp314-cp314-win_amd64.whl", hash = "sha256:d03ba2cd17948b623a6210981d342eb122546d8a8e910ec77511aff4b1acdd00"},+ {file = "pyqwest-0.10.0-cp314-cp314t-macosx_10_12_x86_64.whl", hash = "sha256:07a0eb595f4096232c2d22549b6e4612c1ecada7934e46462c2c37ce14a89cfb"},+ {file = "pyqwest-0.10.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:26401baf7dafc71c8d12d2e8389519d141e6f7c14094d0dd4cf9ec1d3b5555bd"},+ {file = "pyqwest-0.10.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e5e3c436e041d8873ce5bb0fdcf9f9e86f5604e8f0ef9e03149efebd8cb474f6"},+ {file = "pyqwest-0.10.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:09364115761579eabfc79d1e954cdb3ded508dac1903fac7285d4c6f058c683f"},+ {file = "pyqwest-0.10.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:559674a98a8b1217e1830ecd41c9905bf2b60983c6b8017063dfac199f00727c"},+ {file = "pyqwest-0.10.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:f399a696392fff3db3eef0a18ef65b8a3b8396d129193487d966b8eb11006376"},+ {file = "pyqwest-0.10.0-cp314-cp314t-win_amd64.whl", hash = "sha256:0f9163d6dd991bf1bf27308ba38ba021af660b15fffa47ebca98e41cf6f00309"},+ {file = "pyqwest-0.10.0-pp311-pypy311_pp73-macosx_10_12_x86_64.whl", hash = "sha256:cfcc7ba0229baa17831582befb046ace167b368140dae022d0b89b8d586ba12c"},+ {file = "pyqwest-0.10.0-pp311-pypy311_pp73-macosx_11_0_arm64.whl", hash = "sha256:eff9ccf427604d34c635954def07b6113d4754f968073eef2df40bdb80b05bf5"},+ {file = "pyqwest-0.10.0-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:78662158093f9d5c742368f4dd9956aa595f44c6ac0860777c98853ddd5e1610"},+ {file = "pyqwest-0.10.0-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0879a3f0b37876372aa328b9fee956165db174c1ab72464146fe515f49399ddb"},+ {file = "pyqwest-0.10.0-pp311-pypy311_pp73-musllinux_1_2_aarch64.whl", hash = "sha256:850b8de6ade09a60bdb2f969871a177c2c304b594b2034ba3f5962c7bea75551"},+ {file = "pyqwest-0.10.0-pp311-pypy311_pp73-musllinux_1_2_x86_64.whl", hash = "sha256:399802647ea646c6ac9b5460e541b7c209b7a13563c667b2690ded2060185f2e"},+ {file = "pyqwest-0.10.0-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:c26f3de1feb5d066d7a66802a47407a93ba043696064ad80beda4a0a4bf10056"},+ {file = "pyqwest-0.10.0.tar.gz", hash = "sha256:6c1a693be17d57d2c2eca4085e32c2809c53090c16719a907c90ebcf1f40dc01"},+]++[package.dependencies]+opentelemetry-api = ">=1.39.1"+[[package]]name = "pyrfc3339"version = "2.1.0"diff --git a/autogpt_platform/backend/pyproject.toml b/autogpt_platform/backend/pyproject.tomlindex a8f0555ab5..11ff0891d4 100644--- a/autogpt_platform/backend/pyproject.toml+++ b/autogpt_platform/backend/pyproject.toml@@ -139,6 +139,7 @@ platform-linking-manager = "backend.platform_linking_manager:main"executor = "backend.exec:main"analytics-setup = "scripts.generate_views:main_setup"analytics-views = "scripts.generate_views:main_views"+build-desktop-template = "scripts.build_desktop_template:main"copilot-executor = "backend.copilot.executor.__main__:main"batch-executor = "backend.batch_executor:main"copilot-bot = "backend.copilot.bot.__main__:main"diff --git a/autogpt_platform/backend/scripts/build_desktop_template.py b/autogpt_platform/backend/scripts/build_desktop_template.pynew file mode 100644index 0000000000..fafc1cd52c--- /dev/null+++ b/autogpt_platform/backend/scripts/build_desktop_template.py@@ -0,0 +1,53 @@+"""Build the platform's sandbox image on an E2B team ahead of first use.++The backend builds ``agpt-desktop-1x2`` itself the first time a team needs+it (see ``backend.util.e2b_template``). Run this to do it up front, or to+build an experimental size under another alias:++ poetry run build-desktop-template+ poetry run build-desktop-template --alias agpt-desktop-2x2 --cpu 2 --mem 2048++Reads ``E2B_API_KEY`` from the environment or ``backend/.env``; the template+lands on whichever team that key belongs to.+"""++import argparse+import asyncio+import os+from pathlib import Path++from dotenv import load_dotenv++from backend.util.e2b_template import DESKTOP_IMAGE, TemplateSpec, build_template+++def main() -> None:+ args = _parse_args()+ load_dotenv(Path(__file__).resolve().parents[1] / ".env")+ api_key = os.getenv("E2B_API_KEY")+ if not api_key:+ raise SystemExit("E2B_API_KEY must be set in the environment or backend/.env")+ spec = TemplateSpec(+ alias=args.alias,+ source=DESKTOP_IMAGE.source,+ cpu_count=args.cpu,+ memory_mb=args.mem,+ )+ info = asyncio.run(build_template(spec, api_key))+ print(f"Built {info.name} ({info.template_id}) tags={info.tags}")+ if spec != DESKTOP_IMAGE:+ print(f"Set CHAT_E2B_SANDBOX_TEMPLATE={info.name} to use it.")+++def _parse_args() -> argparse.Namespace:+ parser = argparse.ArgumentParser(+ description="Build the platform's sandbox image on an E2B team."+ )+ parser.add_argument("--alias", default=DESKTOP_IMAGE.alias)+ parser.add_argument("--cpu", type=int, default=DESKTOP_IMAGE.cpu_count)+ parser.add_argument("--mem", type=int, default=DESKTOP_IMAGE.memory_mb)+ return parser.parse_args()+++if __name__ == "__main__":+ main()