Matt Brockman 7027f369a3
autoresume: lifecycle component in sdk (#1146)
Implements the `lifecycle` prop on `Sandbox.create`, taking over and
deprecating the `beta_pause` functionality.

Currently supports:
- `on_timeout`: `kill` (default) | `pause`. Controls what should happen
to the sandbox when it hits end of life. Pause allows for resuming
- `auto_resume`: False (default) | True. Whether the sandbox should
autoresume on traffic

Intended for additional functionality as we update the backend to
support additional props. Blocked from deploying until the API and
client-proxy are deployed but for pre-approval.

(Meant to be extended later as add more capabilities to the API)

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Changes the API contract and request payload shape for sandbox
auto-resume and alters lifecycle/timeout behavior mapping, which could
break older integrations if backend/client versions are mismatched.
> 
> **Overview**
> Adds a new `lifecycle` configuration on `Sandbox.create` (JS + Python)
to control what happens at timeout (`kill` vs `pause`) and whether
paused sandboxes auto-resume on traffic (`auto_resume`).
> 
> Deprecates `betaPause`/`beta_pause` and the JS `autoPause` create
option in favor of the new lifecycle semantics, updates connect/pause
call paths accordingly, and expands tests to cover resume-on-connect and
auto-resume behaviors.
> 
> Updates the OpenAPI contract and generated clients so `autoResume` is
now an object with an `enabled: boolean` flag (removing the previous
policy enum), and bumps SDK versions via a changeset.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
515f9b7fc13a5ec13db75450e8f6252e3c7bcf03. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
2026-03-04 13:23:08 -08:00

114 lines
3.4 KiB
Python

import asyncio
import httpx
import pytest
from e2b import AsyncSandbox, SandboxQuery, SandboxState
from e2b.api.client.models import (
NewSandbox,
SandboxAutoResumeConfig,
)
from e2b.sandbox.sandbox_api import get_auto_resume_enabled
@pytest.mark.skip_debug()
async def test_start(async_sandbox):
assert await async_sandbox.is_running()
assert async_sandbox._envd_version is not None
@pytest.mark.skip_debug()
async def test_metadata(async_sandbox_factory):
sbx = await async_sandbox_factory(timeout=5, metadata={"test-key": "test-value"})
paginator = AsyncSandbox.list(
query=SandboxQuery(metadata={"test-key": "test-value"})
)
sandboxes = await paginator.next_items()
for sbx_info in sandboxes:
if sbx.sandbox_id == sbx_info.sandbox_id:
assert sbx_info.metadata is not None
assert sbx_info.metadata["test-key"] == "test-value"
break
else:
assert False, "Sandbox not found"
def test_lifecycle_auto_resume_enabled_mapping():
assert get_auto_resume_enabled({"on_timeout": "pause", "auto_resume": True}) is True
assert (
get_auto_resume_enabled({"on_timeout": "pause", "auto_resume": False}) is False
)
assert get_auto_resume_enabled({"on_timeout": "pause"}) is False
assert get_auto_resume_enabled({"on_timeout": "kill", "auto_resume": False}) is None
assert get_auto_resume_enabled({"on_timeout": "kill"}) is None
assert get_auto_resume_enabled(None) is None
def test_create_payload_serializes_auto_resume_enabled():
body = NewSandbox(
template_id="template-id",
auto_pause=True,
auto_resume=SandboxAutoResumeConfig(enabled=True),
)
assert body.to_dict()["autoPause"] is True
assert body.to_dict()["autoResume"] == {"enabled": True}
def test_create_payload_deserializes_auto_resume_enabled():
body = NewSandbox.from_dict(
{
"templateID": "template-id",
"autoPause": False,
"autoResume": {"enabled": False},
}
)
assert isinstance(body.auto_resume, SandboxAutoResumeConfig)
assert body.auto_resume.to_dict() == {"enabled": False}
@pytest.mark.skip_debug()
async def test_auto_pause_without_auto_resume_requires_connect(async_sandbox_factory):
sandbox = await async_sandbox_factory(
timeout=3,
lifecycle={"on_timeout": "pause", "auto_resume": False},
)
await asyncio.sleep(5)
assert (await sandbox.get_info()).state == SandboxState.PAUSED
assert not await sandbox.is_running()
await sandbox.connect()
assert (await sandbox.get_info()).state == SandboxState.RUNNING
assert await sandbox.is_running()
@pytest.mark.skip_debug()
async def test_auto_resume_wakes_on_http_request(async_sandbox_factory):
sandbox = await async_sandbox_factory(
timeout=3,
lifecycle={"on_timeout": "pause", "auto_resume": True},
)
cmd = await sandbox.commands.run("python3 -m http.server 8000", background=True)
try:
await asyncio.sleep(5)
url = f"https://{sandbox.get_host(8000)}"
async with httpx.AsyncClient(timeout=15.0) as client:
res = await client.get(url)
assert res.status_code == 200
assert (await sandbox.get_info()).state == SandboxState.RUNNING
assert await sandbox.is_running()
finally:
try:
await cmd.kill()
except Exception:
pass