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

77 lines
2.1 KiB
Python

import uuid
import pytest
from e2b import Sandbox
@pytest.mark.skip_debug()
def test_connect(sandbox_factory):
sbx = sandbox_factory(timeout=10)
assert sbx.is_running()
sbx_connection = Sandbox.connect(sbx.sandbox_id)
assert sbx_connection.is_running()
@pytest.mark.skip_debug()
def test_connect_with_secure(sandbox_factory):
dir_name = f"test_directory_{uuid.uuid4()}"
sbx = sandbox_factory(timeout=10, secure=True)
assert sbx.is_running()
sbx_connection = Sandbox.connect(sbx.sandbox_id)
sbx_connection.files.make_dir(dir_name)
files = sbx_connection.files.list(dir_name)
assert len(files) == 0
@pytest.mark.skip_debug()
def test_connect_to_paused_sandbox_resumes(sandbox):
sandbox.pause()
assert not sandbox.is_running()
resumed = Sandbox.connect(sandbox.sandbox_id)
assert resumed.is_running()
@pytest.mark.skip_debug()
def test_resume_does_not_shorten_timeout_on_running_sandbox(sandbox_factory):
# Create sandbox with a 300 second timeout
sbx = sandbox_factory(timeout=300)
assert sbx.is_running()
# Get initial info to check end_at
info_before = Sandbox.get_info(sbx.sandbox_id)
# Connect with a shorter timeout (10 seconds)
Sandbox.connect(sbx.sandbox_id, timeout=10)
# Get info after connection
info_after = Sandbox.get_info(sbx.sandbox_id)
# The end_at time should not have been shortened. It should be the same
assert info_after.end_at == info_before.end_at, (
f"Timeout was shortened: before={info_before.end_at}, after={info_after.end_at}"
)
@pytest.mark.skip_debug()
def test_connect_extends_timeout_on_running_sandbox(sandbox):
# Get initial info to check end_at
info_before = sandbox.get_info()
# Connect with a longer timeout
Sandbox.connect(sandbox.sandbox_id, timeout=600)
# Get info after connection
info_after = sandbox.get_info()
# The end_at time should have been extended
assert info_after.end_at > info_before.end_at, (
f"Timeout was not extended: before={info_before.end_at}, after={info_after.end_at}"
)