Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

feat(sandbox): add timeout parameter to Sandbox.create/ephemeral (#1353)

r33drichards5c922d62c0ce
diff --git a/libs/python/cua-sandbox/cua_sandbox/sandbox.py b/libs/python/cua-sandbox/cua_sandbox/sandbox.py
index 32031f8e1b..147fff6342 100644
--- a/libs/python/cua-sandbox/cua_sandbox/sandbox.py
+++ b/libs/python/cua-sandbox/cua_sandbox/sandbox.py
@@ -407,6 +407,7 @@ class Sandbox:
memory_mb: Optional[int] = None,
disk_gb: Optional[int] = None,
region: str = "us-east-1",
+ time_to_start: Optional[float] = None,
telemetry_enabled: bool = True,
) -> "Sandbox":
"""Provision a new persistent sandbox and return it connected.
@@ -425,6 +426,8 @@ class Sandbox:
memory_mb: Memory in MB for the cloud sandbox.
disk_gb: Disk size in GB for the cloud sandbox.
region: Cloud region (default ``"us-east-1"``).
+ time_to_start: Max seconds to wait for the VM to become reachable
+ (default 600). Only applies to cloud sandboxes.
telemetry_enabled: Set to False to disable telemetry for this instance.
Example::
@@ -445,6 +448,7 @@ class Sandbox:
memory_mb=memory_mb,
disk_gb=disk_gb,
region=region,
+ time_to_start=time_to_start,
telemetry_enabled=telemetry_enabled,
)
@@ -521,6 +525,7 @@ class Sandbox:
memory_mb: Optional[int] = None,
disk_gb: Optional[int] = None,
region: str = "us-east-1",
+ time_to_start: Optional[float] = None,
telemetry_enabled: bool = True,
) -> AsyncIterator["Sandbox"]:
"""Create an ephemeral sandbox that is automatically destroyed on exit.
@@ -535,6 +540,8 @@ class Sandbox:
memory_mb: Memory in MB for the cloud sandbox.
disk_gb: Disk size in GB for the cloud sandbox.
region: Cloud region (default ``"us-east-1"``).
+ time_to_start: Max seconds to wait for the VM to become reachable
+ (default 600). Only applies to cloud sandboxes.
Example::
@@ -553,6 +560,7 @@ class Sandbox:
memory_mb=memory_mb,
disk_gb=disk_gb,
region=region,
+ time_to_start=time_to_start,
telemetry_enabled=telemetry_enabled,
)
try:
@@ -966,6 +974,7 @@ class Sandbox:
memory_mb: Optional[int] = None,
disk_gb: Optional[int] = None,
region: str = "us-east-1",
+ time_to_start: Optional[float] = None,
telemetry_enabled: bool = True,
) -> "Sandbox":
"""Internal workhorse — all public factories delegate here."""
@@ -1031,6 +1040,7 @@ class Sandbox:
memory_mb=memory_mb,
disk_gb=disk_gb,
region=region,
+ time_to_start=time_to_start,
)
sb = cls(
transport, name=name, _ephemeral=ephemeral, _telemetry_enabled=telemetry_enabled
diff --git a/libs/python/cua-sandbox/cua_sandbox/transport/cloud.py b/libs/python/cua-sandbox/cua_sandbox/transport/cloud.py
index a776067755..9bd3fdb7fe 100644
--- a/libs/python/cua-sandbox/cua_sandbox/transport/cloud.py
+++ b/libs/python/cua-sandbox/cua_sandbox/transport/cloud.py
@@ -41,6 +41,7 @@ class CloudTransport(Transport):
memory_mb: Optional[int] = None,
disk_gb: Optional[int] = None,
region: str = "us-east-1",
+ time_to_start: Optional[float] = None,
):
self._name = name
self._api_key_override = api_key
@@ -50,6 +51,7 @@ class CloudTransport(Transport):
self._memory_mb = memory_mb
self._disk_gb = disk_gb
self._region = region
+ self._time_to_start = time_to_start if time_to_start is not None else _POLL_TIMEOUT
self._inner: Optional[HTTPTransport] = None
self._api_client: Optional[httpx.AsyncClient] = None
@@ -102,7 +104,7 @@ class CloudTransport(Transport):
cs_url = ""
is_running = vm_info.get("status") in ("running", "ready")
- while elapsed < _POLL_TIMEOUT:
+ while elapsed < self._time_to_start:
# Try to extract a direct-IP endpoint from current vm_info
try:
url = self._resolve_endpoint(vm_info)
@@ -121,7 +123,7 @@ class CloudTransport(Transport):
if not is_running:
raise TimeoutError(
- f"VM {self._name!r} did not become running within {_POLL_TIMEOUT}s "
+ f"VM {self._name!r} did not become running within {self._time_to_start}s "
f"(last status: {vm_info.get('status')})"
)
if not cs_url:
@@ -153,7 +155,7 @@ class CloudTransport(Transport):
elapsed = 0.0
is_running = vm_info.get("status") in ("running", "ready")
- while elapsed < _POLL_TIMEOUT:
+ while elapsed < self._time_to_start:
# Try to extract a direct-IP endpoint
try:
url = self._resolve_endpoint(vm_info)
@@ -178,7 +180,7 @@ class CloudTransport(Transport):
if not is_running:
raise TimeoutError(
- f"VM {self._name!r} did not become running within {_POLL_TIMEOUT}s "
+ f"VM {self._name!r} did not become running within {self._time_to_start}s "
f"(last status: {vm_info.get('status')})"
)
url = resolved_url or self._resolve_endpoint(vm_info)
@@ -375,9 +377,9 @@ class CloudTransport(Transport):
"""Poll until the VM status is 'running' (or 'ready')."""
elapsed = 0.0
while vm_info.get("status") not in ("running", "ready"):
- if elapsed >= _POLL_TIMEOUT:
+ if elapsed >= self._time_to_start:
raise TimeoutError(
- f"VM {self._name!r} did not become running within {_POLL_TIMEOUT}s "
+ f"VM {self._name!r} did not become running within {self._time_to_start}s "
f"(last status: {vm_info.get('status')})"
)
await asyncio.sleep(_POLL_INTERVAL)
@@ -402,7 +404,7 @@ class CloudTransport(Transport):
# Phase 1: wait for HTTP port to accept connections (fast — just needs
# the Python process to start, not the emulator).
- while elapsed < _POLL_TIMEOUT:
+ while elapsed < self._time_to_start:
try:
resp = await self._inner._client.get("/", timeout=2.0)
# Any response means the server is up
@@ -427,7 +429,7 @@ class CloudTransport(Transport):
pass # Fall through to Phase 2
# Phase 2: wait for get_screen_size (needs emulator/display running)
- while elapsed < _POLL_TIMEOUT:
+ while elapsed < self._time_to_start:
try:
await self._inner.get_screen_size()
return # Fully ready
@@ -444,7 +446,7 @@ class CloudTransport(Transport):
await asyncio.sleep(_POLL_INTERVAL)
elapsed += _POLL_INTERVAL
raise TimeoutError(
- f"Computer-server for VM {self._name!r} not reachable within {_POLL_TIMEOUT}s: {last_err}"
+ f"Computer-server for VM {self._name!r} not reachable within {self._time_to_start}s: {last_err}"
)
async def _apply_image_layers(self) -> None: