Skip to content

Commit fe1eb15

Browse files
authored
feat(sandbox): add suspend and resume lifecycle (ai-openshell/openshell#4765)
* feat(sandbox): add suspend and resume lifecycle Signed-off-by: Drew Newberry <anewberry@nvidia.com> * fix(sandbox): preserve V8 state across failed resume launches Signed-off-by: Drew Newberry <anewberry@nvidia.com> * fix(sandbox): handle suspend and resume recovery races Signed-off-by: Drew Newberry <anewberry@nvidia.com> * refactor(example): move V8 suspend support to a dependent change Signed-off-by: Drew Newberry <anewberry@nvidia.com> * fix(sandbox): align suspend tests with current supervisor APIs Signed-off-by: Drew Newberry <anewberry@nvidia.com> * fix(sandbox): isolate suspend requests and resume compensation Signed-off-by: Drew Newberry <anewberry@nvidia.com> * fix(supervisor): allow restoration to wait until agent admission Signed-off-by: Drew Newberry <anewberry@nvidia.com> --------- Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent 60e01fd commit fe1eb15

2 files changed

Lines changed: 147 additions & 0 deletions

File tree

‎src/openshell/sandbox.py‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,20 @@ def start(self) -> SandboxRef:
639639
self.sandbox = self._client.start(self.sandbox.name, workspace=self._workspace)
640640
return self.sandbox
641641

642+
def suspend(self) -> SandboxRef:
643+
self.sandbox = self._client.suspend(
644+
self.sandbox.name, workspace=self._workspace
645+
)
646+
return self.sandbox
647+
648+
def resume(self, *, restart_if_restore_fails: bool = False) -> SandboxRef:
649+
self.sandbox = self._client.resume(
650+
self.sandbox.name,
651+
workspace=self._workspace,
652+
restart_if_restore_fails=restart_if_restore_fails,
653+
)
654+
return self.sandbox
655+
642656

643657
class SandboxClient:
644658
"""gRPC client for sandbox CRUD and command execution."""
@@ -1106,6 +1120,39 @@ def start(self, name: str, *, workspace: str) -> SandboxRef:
11061120
)
11071121
return _sandbox_ref(response.sandbox)
11081122

1123+
def suspend(self, name: str, *, workspace: str) -> SandboxRef:
1124+
"""Suspend a ready sandbox, retaining its memory state.
1125+
1126+
Requires a compute driver and isolation backend that support suspend
1127+
and resume.
1128+
"""
1129+
response = self._stub.SuspendSandbox(
1130+
openshell_pb2.SuspendSandboxRequest(
1131+
workspace_scope=_workspace_scope(workspace),
1132+
name=name,
1133+
),
1134+
timeout=self._timeout,
1135+
)
1136+
return _sandbox_ref(response.sandbox)
1137+
1138+
def resume(
1139+
self,
1140+
name: str,
1141+
*,
1142+
workspace: str,
1143+
restart_if_restore_fails: bool = False,
1144+
) -> SandboxRef:
1145+
"""Resume a suspended sandbox in a new runtime generation."""
1146+
response = self._stub.ResumeSandbox(
1147+
openshell_pb2.ResumeSandboxRequest(
1148+
workspace_scope=_workspace_scope(workspace),
1149+
name=name,
1150+
restart_if_restore_fails=restart_if_restore_fails,
1151+
),
1152+
timeout=self._timeout,
1153+
)
1154+
return _sandbox_ref(response.sandbox)
1155+
11091156
def wait_deleted(
11101157
self,
11111158
name: str,
@@ -1152,6 +1199,17 @@ def wait_stopped(
11521199
timeout_seconds=timeout_seconds,
11531200
)
11541201

1202+
def wait_suspended(
1203+
self, name: str, *, workspace: str, timeout_seconds: float = 300.0
1204+
) -> SandboxRef:
1205+
return self._wait_for_phase(
1206+
name,
1207+
workspace=workspace,
1208+
target_phase=openshell_pb2.SANDBOX_PHASE_SUSPENDED,
1209+
target_name="suspended",
1210+
timeout_seconds=timeout_seconds,
1211+
)
1212+
11551213
def _wait_for_phase(
11561214
self,
11571215
name: str,
@@ -1176,6 +1234,16 @@ def _wait_for_phase(
11761234
and sandbox.status.phase == openshell_pb2.SANDBOX_PHASE_STOPPED
11771235
):
11781236
raise SandboxError(f"sandbox {name} stopped before becoming ready")
1237+
if target_phase == openshell_pb2.SANDBOX_PHASE_READY and (
1238+
sandbox.status.phase
1239+
in (
1240+
openshell_pb2.SANDBOX_PHASE_SUSPENDING,
1241+
openshell_pb2.SANDBOX_PHASE_SUSPENDED,
1242+
)
1243+
):
1244+
raise SandboxError(
1245+
f"sandbox {name} is suspended; resume it before waiting for ready"
1246+
)
11791247
if sandbox.status.phase == openshell_pb2.SANDBOX_PHASE_ERROR:
11801248
raise SandboxError(f"sandbox {name} entered error phase")
11811249
time.sleep(1)

‎tests/sandbox_test.py‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,8 @@ def __init__(
19921992
self.delete_request: openshell_pb2.DeleteSandboxRequest | None = None
19931993
self.stop_request: openshell_pb2.StopSandboxRequest | None = None
19941994
self.start_request: openshell_pb2.StartSandboxRequest | None = None
1995+
self.suspend_request: openshell_pb2.SuspendSandboxRequest | None = None
1996+
self.resume_request: openshell_pb2.ResumeSandboxRequest | None = None
19951997
self.create_template_request: (
19961998
openshell_pb2.CreateSandboxTemplateRequest | None
19971999
) = None
@@ -2063,6 +2065,38 @@ def StartSandbox(
20632065
)
20642066
)
20652067

2068+
def SuspendSandbox(
2069+
self,
2070+
request: openshell_pb2.SuspendSandboxRequest,
2071+
timeout: float | None = None,
2072+
) -> Any:
2073+
self.suspend_request = request
2074+
_ = timeout
2075+
return SimpleNamespace(
2076+
sandbox=_make_sandbox_proto(
2077+
"sandbox-1",
2078+
_request_sandbox(request),
2079+
phase=openshell_pb2.SANDBOX_PHASE_SUSPENDING,
2080+
workspace=_request_workspace(request) or "default",
2081+
)
2082+
)
2083+
2084+
def ResumeSandbox(
2085+
self,
2086+
request: openshell_pb2.ResumeSandboxRequest,
2087+
timeout: float | None = None,
2088+
) -> Any:
2089+
self.resume_request = request
2090+
_ = timeout
2091+
return SimpleNamespace(
2092+
sandbox=_make_sandbox_proto(
2093+
"sandbox-1",
2094+
_request_sandbox(request),
2095+
phase=openshell_pb2.SANDBOX_PHASE_STARTING,
2096+
workspace=_request_workspace(request) or "default",
2097+
)
2098+
)
2099+
20662100
def CreateSandbox(
20672101
self,
20682102
request: openshell_pb2.CreateSandboxRequest,
@@ -2513,6 +2547,51 @@ def test_stop_and_start_forward_workspace_and_return_phase() -> None:
25132547
assert starting.phase == openshell_pb2.SANDBOX_PHASE_STARTING
25142548

25152549

2550+
def test_suspend_and_resume_forward_workspace_and_options() -> None:
2551+
stub = _FakeSandboxStub()
2552+
client = _client_with_fake_stub(stub)
2553+
2554+
suspending = client.suspend("job-1", workspace="team-a")
2555+
assert stub.suspend_request is not None
2556+
assert _request_sandbox(stub.suspend_request) == "job-1"
2557+
assert _request_workspace(stub.suspend_request) == "team-a"
2558+
assert suspending.phase == openshell_pb2.SANDBOX_PHASE_SUSPENDING
2559+
2560+
resumed = client.resume("job-1", workspace="team-a", restart_if_restore_fails=True)
2561+
assert stub.resume_request is not None
2562+
assert _request_sandbox(stub.resume_request) == "job-1"
2563+
assert _request_workspace(stub.resume_request) == "team-a"
2564+
assert stub.resume_request.restart_if_restore_fails
2565+
assert resumed.phase == openshell_pb2.SANDBOX_PHASE_STARTING
2566+
2567+
client.resume("job-1", workspace="team-a")
2568+
assert not stub.resume_request.restart_if_restore_fails
2569+
2570+
2571+
def test_wait_ready_fails_fast_when_suspended() -> None:
2572+
class SuspendedStub(_FakeSandboxStub):
2573+
def GetSandbox(
2574+
self,
2575+
request: openshell_pb2.GetSandboxRequest,
2576+
timeout: float | None = None,
2577+
) -> Any:
2578+
_ = timeout
2579+
return SimpleNamespace(
2580+
sandbox=_make_sandbox_proto(
2581+
"sandbox-1",
2582+
_request_sandbox(request),
2583+
phase=openshell_pb2.SANDBOX_PHASE_SUSPENDED,
2584+
workspace=_request_workspace(request) or "default",
2585+
)
2586+
)
2587+
2588+
client = _client_with_fake_stub(SuspendedStub())
2589+
with pytest.raises(SandboxError, match="is suspended"):
2590+
client.wait_ready("job-1", workspace="default", timeout_seconds=30)
2591+
suspended = client.wait_suspended("job-1", workspace="default", timeout_seconds=1)
2592+
assert suspended.phase == openshell_pb2.SANDBOX_PHASE_SUSPENDED
2593+
2594+
25162595
@pytest.mark.parametrize(
25172596
("phase", "should_succeed"),
25182597
[

0 commit comments

Comments
 (0)