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(cua-sandbox): accept native claim specs

r33drichardsc9b665b2a963
diff --git a/libs/python/cua-sandbox/cua_sandbox/__init__.py b/libs/python/cua-sandbox/cua_sandbox/__init__.py
index 9bd1c19546..ee6eadacf0 100644
--- a/libs/python/cua-sandbox/cua_sandbox/__init__.py
+++ b/libs/python/cua-sandbox/cua_sandbox/__init__.py
@@ -31,12 +31,14 @@ from cua_sandbox.runtime.compat import (
from cua_sandbox.sandbox import Sandbox, SandboxInfo, sandbox
from cua_sandbox.transport.cloud import CloudTransport
from fleet_sdk import (
+ ClaimSpec,
CreatePoolRequest,
Firmware,
PoolSpec,
PoolTemplate,
RuntimeKind,
SandboxService,
+ SandboxTemplateRef,
ServiceProtocol,
)
@@ -47,6 +49,8 @@ __all__ = [
"Image",
"Pool",
"CreatePoolRequest",
+ "ClaimSpec",
+ "SandboxTemplateRef",
"PoolSpec",
"RuntimeKind",
"PoolTemplate",
diff --git a/libs/python/cua-sandbox/cua_sandbox/pool.py b/libs/python/cua-sandbox/cua_sandbox/pool.py
index c1fd3877b8..d84090bb41 100644
--- a/libs/python/cua-sandbox/cua_sandbox/pool.py
+++ b/libs/python/cua-sandbox/cua_sandbox/pool.py
@@ -10,7 +10,11 @@ from typing import Any, cast
from cua_sandbox.sandbox import Sandbox
from cua_sandbox.transport.fleet import FleetTransport
from cua_sandbox.transport.fleet_cloud import _FleetClient
-from fleet_sdk import CreateClaimRequest, CreatePoolRequest
+from fleet_sdk import (
+ ClaimSpec,
+ CreateClaimRequest,
+ CreatePoolRequest,
+)
logger = logging.getLogger(__name__)
@@ -58,7 +62,7 @@ class Pool:
await client.close()
@asynccontextmanager
- async def claim(self) -> AsyncIterator[Sandbox]:
+ async def claim(self, *, spec: ClaimSpec | None = None) -> AsyncIterator[Sandbox]:
"""Lease a sandbox and release its Fleet claim when the block exits.
A claim is released after both normal and exceptional block exits. If
@@ -73,7 +77,7 @@ class Pool:
cleanup_error: Exception | None = None
try:
- claim = await client.create_claim(CreateClaimRequest(pool=self._resource, spec=None))
+ claim = await client.create_claim(CreateClaimRequest(pool=self._resource, spec=spec))
bound = await client.wait_claim(claim)
sandbox = Sandbox(
FleetTransport(sdk=client, bound=bound, service_name="server"), name=bound.name
diff --git a/libs/python/cua-sandbox/cua_sandbox/sync/__init__.py b/libs/python/cua-sandbox/cua_sandbox/sync/__init__.py
index d01925a3a3..c7ec3462e4 100644
--- a/libs/python/cua-sandbox/cua_sandbox/sync/__init__.py
+++ b/libs/python/cua-sandbox/cua_sandbox/sync/__init__.py
@@ -24,7 +24,7 @@ from cua_sandbox.image import Image
from cua_sandbox.localhost import Localhost as _AsyncLocalhost
from cua_sandbox.pool import Pool as _AsyncPool
from cua_sandbox.sandbox import Sandbox as _AsyncSandbox
-from fleet_sdk import CreatePoolRequest
+from fleet_sdk import ClaimSpec, CreatePoolRequest
def _get_or_create_loop() -> asyncio.AbstractEventLoop:
@@ -95,9 +95,9 @@ class Pool:
return cls(_run(_AsyncPool.reconcile(request)))
@contextmanager
- def claim(self) -> Iterator[_SyncProxy]:
+ def claim(self, *, spec: ClaimSpec | None = None) -> Iterator[_SyncProxy]:
"""Synchronously lease a sandbox and release the claim on exit."""
- context = self._async_pool.claim()
+ context = self._async_pool.claim(spec=spec)
sandbox = _run(context.__aenter__())
try:
yield _SyncProxy(sandbox)
diff --git a/libs/python/cua-sandbox/tests/test_pool.py b/libs/python/cua-sandbox/tests/test_pool.py
index 025d10b9b8..0ec0f26c83 100644
--- a/libs/python/cua-sandbox/tests/test_pool.py
+++ b/libs/python/cua-sandbox/tests/test_pool.py
@@ -4,6 +4,7 @@ from types import SimpleNamespace
import pytest
from cua_sandbox import (
+ ClaimSpec,
CreatePoolRequest,
Firmware,
Pool,
@@ -11,6 +12,7 @@ from cua_sandbox import (
PoolTemplate,
RuntimeKind,
SandboxService,
+ SandboxTemplateRef,
ServiceProtocol,
)
from cua_sandbox.sync import Pool as SyncPool
@@ -262,9 +264,16 @@ def test_sync_pool_matches_blocking_context_manager(monkeypatch):
monkeypatch.setattr("cua_sandbox.pool._FleetClient", lambda: next(clients))
pool = SyncPool.reconcile(pool_request())
- with pool.claim() as sandbox:
+ spec = ClaimSpec(
+ sandbox_template_ref=SandboxTemplateRef(name=pool.name),
+ warmpool=None,
+ bind_deadline=900,
+ lifecycle=None,
+ )
+ with pool.claim(spec=spec) as sandbox:
assert sandbox.name == "sandbox-1"
+ assert claim_client.claims[0].spec is spec
assert claim_client.released == ["claim-1"]
assert claim_client.closed is True
@@ -338,3 +347,26 @@ async def test_claim_exposes_named_service_requests(monkeypatch):
_, service, path, request = claim_client.service_requests[0]
assert (service, path, request.method) == ("mcp", "/mcp", "POST")
assert request.body == b'{"jsonrpc": "2.0", "method": "tools/list", "id": 1}'
+
+
+async def test_claim_forwards_native_claim_spec_unchanged(monkeypatch):
+ reconcile_client = FakeFleetClient()
+ claim_client = FakeFleetClient()
+ clients = iter([reconcile_client, claim_client])
+ monkeypatch.setattr("cua_sandbox.pool._FleetClient", lambda: next(clients))
+ pool = await Pool.reconcile(pool_request())
+
+ spec = ClaimSpec(
+ sandbox_template_ref=SandboxTemplateRef(name=pool.name),
+ warmpool=None,
+ bind_deadline=900,
+ lifecycle=None,
+ )
+
+ async with pool.claim(spec=spec) as sandbox:
+ assert sandbox.name == "sandbox-1"
+
+ request = claim_client.claims[0]
+ assert request.pool is pool.resource
+ assert request.spec is spec