Skip to content

Commit edce0f9

Browse files
authored
feat(sandbox): add stop and start operations (ai-openshell/openshell#2653)
* feat(sandbox): add suspend and resume operations Signed-off-by: Seth Jennings <sjenning@redhat.com> * fix(server): preserve lifecycle work after cancellation Signed-off-by: Seth Jennings <sjenning@redhat.com> * fix(server): reconcile ambiguous lifecycle outcomes Signed-off-by: Seth Jennings <sjenning@redhat.com> * fix(server): complete suspended session cleanup Signed-off-by: Seth Jennings <sjenning@redhat.com> * fix(vm): preserve suspension state on resume failure Signed-off-by: Seth Jennings <sjenning@redhat.com> * fix(server): retry retained lifecycle transitions Signed-off-by: Seth Jennings <sjenning@redhat.com> * fix(server): clean sessions after suspend reconciliation Signed-off-by: Seth Jennings <sjenning@redhat.com> * test(sandbox): cover deleting suspended sandbox Signed-off-by: Seth Jennings <sjenning@redhat.com> * fix(kubernetes): preserve progressing sandbox suspension Signed-off-by: Seth Jennings <sjenning@redhat.com> * fix(kubernetes): bound suspend status polling Signed-off-by: Seth Jennings <sjenning@redhat.com> * fix(kubernetes): detect legacy sandbox suspension Signed-off-by: Seth Jennings <sjenning@redhat.com> * fix(tui): render suspended sandbox phases Signed-off-by: Seth Jennings <sjenning@redhat.com> * refactor(sandbox): rename suspend and resume lifecycle Signed-off-by: Seth Jennings <sjenning@redhat.com> * perf(server): clean stopped sessions on transition Signed-off-by: Seth Jennings <sjenning@redhat.com> * fix(kubernetes): fail fast on rejected stop Signed-off-by: Seth Jennings <sjenning@redhat.com> * fix(compute): fence stale restart lifecycle events Signed-off-by: Seth Jennings <sjenning@redhat.com> --------- Signed-off-by: Seth Jennings <sjenning@redhat.com>
1 parent f1adb20 commit edce0f9

2 files changed

Lines changed: 105 additions & 2 deletions

File tree

‎src/openshell/sandbox.py‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ def exec_python(
251251
def delete(self) -> bool:
252252
return self._client.delete(self.sandbox.name, workspace=self._workspace)
253253

254+
def stop(self) -> SandboxRef:
255+
self.sandbox = self._client.stop(self.sandbox.name, workspace=self._workspace)
256+
return self.sandbox
257+
258+
def start(self) -> SandboxRef:
259+
self.sandbox = self._client.start(self.sandbox.name, workspace=self._workspace)
260+
return self.sandbox
261+
254262

255263
class SandboxClient:
256264
"""gRPC client for sandbox CRUD and command execution."""
@@ -547,6 +555,20 @@ def delete(self, sandbox_name: str, *, workspace: str) -> bool:
547555
)
548556
return bool(response.deleted)
549557

558+
def stop(self, sandbox_name: str, *, workspace: str) -> SandboxRef:
559+
response = self._stub.StopSandbox(
560+
openshell_pb2.StopSandboxRequest(name=sandbox_name, workspace=workspace),
561+
timeout=self._timeout,
562+
)
563+
return _sandbox_ref(response.sandbox)
564+
565+
def start(self, sandbox_name: str, *, workspace: str) -> SandboxRef:
566+
response = self._stub.StartSandbox(
567+
openshell_pb2.StartSandboxRequest(name=sandbox_name, workspace=workspace),
568+
timeout=self._timeout,
569+
)
570+
return _sandbox_ref(response.sandbox)
571+
550572
def wait_deleted(
551573
self, sandbox_name: str, *, workspace: str, timeout_seconds: float = 60.0
552574
) -> None:
@@ -566,16 +588,46 @@ def wait_deleted(
566588

567589
def wait_ready(
568590
self, sandbox_name: str, *, workspace: str, timeout_seconds: float = 300.0
591+
) -> SandboxRef:
592+
return self._wait_for_phase(
593+
sandbox_name,
594+
workspace=workspace,
595+
target_phase=openshell_pb2.SANDBOX_PHASE_READY,
596+
target_name="ready",
597+
timeout_seconds=timeout_seconds,
598+
)
599+
600+
def wait_stopped(
601+
self, sandbox_name: str, *, workspace: str, timeout_seconds: float = 300.0
602+
) -> SandboxRef:
603+
return self._wait_for_phase(
604+
sandbox_name,
605+
workspace=workspace,
606+
target_phase=openshell_pb2.SANDBOX_PHASE_STOPPED,
607+
target_name="stopped",
608+
timeout_seconds=timeout_seconds,
609+
)
610+
611+
def _wait_for_phase(
612+
self,
613+
sandbox_name: str,
614+
*,
615+
workspace: str,
616+
target_phase: int,
617+
target_name: str,
618+
timeout_seconds: float,
569619
) -> SandboxRef:
570620
deadline = time.time() + timeout_seconds
571621
while time.time() < deadline:
572622
sandbox = self.get(sandbox_name, workspace=workspace)
573-
if sandbox.status.phase == openshell_pb2.SANDBOX_PHASE_READY:
623+
if sandbox.status.phase == target_phase:
574624
return sandbox
575625
if sandbox.status.phase == openshell_pb2.SANDBOX_PHASE_ERROR:
576626
raise SandboxError(f"sandbox {sandbox_name} entered error phase")
577627
time.sleep(1)
578-
raise SandboxError(f"sandbox {sandbox_name} was not ready within timeout")
628+
raise SandboxError(
629+
f"sandbox {sandbox_name} was not {target_name} within timeout"
630+
)
579631

580632
def exec_stream(
581633
self,

‎tests/sandbox_test.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,8 @@ def __init__(self, listed: list[openshell_pb2.Sandbox] | None = None) -> None:
15431543
self.list_request: openshell_pb2.ListSandboxesRequest | None = None
15441544
self.get_request: openshell_pb2.GetSandboxRequest | None = None
15451545
self.delete_request: openshell_pb2.DeleteSandboxRequest | None = None
1546+
self.stop_request: openshell_pb2.StopSandboxRequest | None = None
1547+
self.start_request: openshell_pb2.StartSandboxRequest | None = None
15461548
self._listed = listed or []
15471549

15481550
def GetSandbox(
@@ -1567,6 +1569,38 @@ def DeleteSandbox(
15671569
_ = timeout
15681570
return SimpleNamespace(deleted=True)
15691571

1572+
def StopSandbox(
1573+
self,
1574+
request: openshell_pb2.StopSandboxRequest,
1575+
timeout: float | None = None,
1576+
) -> Any:
1577+
self.stop_request = request
1578+
_ = timeout
1579+
return SimpleNamespace(
1580+
sandbox=_make_sandbox_proto(
1581+
"sandbox-1",
1582+
request.name,
1583+
phase=openshell_pb2.SANDBOX_PHASE_STOPPED,
1584+
workspace=request.workspace,
1585+
)
1586+
)
1587+
1588+
def StartSandbox(
1589+
self,
1590+
request: openshell_pb2.StartSandboxRequest,
1591+
timeout: float | None = None,
1592+
) -> Any:
1593+
self.start_request = request
1594+
_ = timeout
1595+
return SimpleNamespace(
1596+
sandbox=_make_sandbox_proto(
1597+
"sandbox-1",
1598+
request.name,
1599+
phase=openshell_pb2.SANDBOX_PHASE_STARTING,
1600+
workspace=request.workspace,
1601+
)
1602+
)
1603+
15701604
def CreateSandbox(
15711605
self,
15721606
request: openshell_pb2.CreateSandboxRequest,
@@ -1641,6 +1675,23 @@ def test_create_forwards_name_and_labels() -> None:
16411675
assert dict(ref.labels) == {"aiq": "deep-research"}
16421676

16431677

1678+
def test_stop_and_start_forward_workspace_and_return_phase() -> None:
1679+
stub = _FakeSandboxStub()
1680+
client = _client_with_fake_stub(stub)
1681+
1682+
stopped = client.stop("job-1", workspace="team-a")
1683+
assert stub.stop_request is not None
1684+
assert stub.stop_request.name == "job-1"
1685+
assert stub.stop_request.workspace == "team-a"
1686+
assert stopped.phase == openshell_pb2.SANDBOX_PHASE_STOPPED
1687+
1688+
starting = client.start("job-1", workspace="team-a")
1689+
assert stub.start_request is not None
1690+
assert stub.start_request.name == "job-1"
1691+
assert stub.start_request.workspace == "team-a"
1692+
assert starting.phase == openshell_pb2.SANDBOX_PHASE_STARTING
1693+
1694+
16441695
def test_create_without_args_sends_empty_metadata() -> None:
16451696
stub = _FakeSandboxStub()
16461697
client = _client_with_fake_stub(stub)

0 commit comments

Comments
 (0)