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

113 lines
3.2 KiB
Python

from time import sleep
import httpx
import pytest
from e2b import Sandbox, SandboxState
from e2b.api.client.models import (
NewSandbox,
SandboxAutoResumeConfig,
)
from e2b.sandbox.sandbox_api import SandboxQuery, get_auto_resume_enabled
@pytest.mark.skip_debug()
def test_start(sandbox_factory):
sbx = sandbox_factory(timeout=5)
assert sbx.is_running()
assert sbx._envd_version is not None
@pytest.mark.skip_debug()
def test_metadata(sandbox_factory):
sbx = sandbox_factory(timeout=5, metadata={"test-key": "test-value"})
paginator = Sandbox.list(query=SandboxQuery(metadata={"test-key": "test-value"}))
sandboxes = 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()
def test_auto_pause_without_auto_resume_requires_connect(sandbox_factory):
sandbox = sandbox_factory(
timeout=3,
lifecycle={"on_timeout": "pause", "auto_resume": False},
)
sleep(5)
assert sandbox.get_info().state == SandboxState.PAUSED
assert not sandbox.is_running()
sandbox.connect()
assert sandbox.get_info().state == SandboxState.RUNNING
assert sandbox.is_running()
@pytest.mark.skip_debug()
def test_auto_resume_wakes_on_http_request(sandbox_factory):
sandbox = sandbox_factory(
timeout=3,
lifecycle={"on_timeout": "pause", "auto_resume": True},
)
cmd = sandbox.commands.run("python3 -m http.server 8000", background=True)
try:
sleep(5)
url = f"https://{sandbox.get_host(8000)}"
res = httpx.get(url, timeout=15.0)
assert res.status_code == 200
assert sandbox.get_info().state == SandboxState.RUNNING
assert sandbox.is_running()
finally:
try:
cmd.kill()
except Exception:
pass