Skip to content

Commit ad20dea

Browse files
authored
feat(api): return typed deletion outcomes with explicit missing-target semantics (ai-openshell/openshell#3317)
* feat(api): return typed deletion outcomes with explicit missing-target semantics Implement phase 2 of ai-openshell/openshell#3051 across the public gateway API and first-party SDKs. Preserve asynchronous sandbox deletion and observed resource identities, reserve legacy wire fields, and document the coordinated migration. Signed-off-by: Mrunal Patel <mrunalp@gmail.com> * fix(sdk): bind deletion waits to sandbox identity Track accepted deletions by original sandbox identity in Rust and TypeScript. Preserve name-only waits and cover replacement races and lookup failures. Merge the phase-one cleanup fix and adapt its interceptor regressions to typed deletion outcomes. Refs ai-openshell/openshell#3051. Signed-off-by: Mrunal Patel <mrunalp@gmail.com> * test(e2e): accept asynchronous stopped sandbox deletion Allow either completed or accepted deletion output, then continue polling for actual sandbox absence. This preserves the lifecycle assertion under the phase-two deletion contract. Refs ai-openshell/openshell#3051. Signed-off-by: Mrunal Patel <mrunalp@gmail.com> --------- Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
1 parent ae430bc commit ad20dea

5 files changed

Lines changed: 125 additions & 37 deletions

File tree

‎src/openshell/__init__.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from __future__ import annotations
77

88
from .errors import ErrorInfo, FieldViolation, GatewayError, from_grpc_error
9+
from .mutations import DeletionOutcome, DeletionResult
910
from .sandbox import (
1011
ClientCredentialsAuth,
1112
ExecChunk,
@@ -34,6 +35,8 @@
3435

3536
__all__ = [
3637
"ClientCredentialsAuth",
38+
"DeletionOutcome",
39+
"DeletionResult",
3740
"ErrorInfo",
3841
"ExecChunk",
3942
"ExecResult",

‎src/openshell/mutations.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Typed mutation results, separate from transport success."""
5+
6+
from dataclasses import dataclass
7+
from enum import IntEnum
8+
9+
10+
class DeletionOutcome(IntEnum):
11+
"""Only COMPLETED and ALREADY_ABSENT establish logical deletion completion."""
12+
13+
UNSPECIFIED = 0
14+
COMPLETED = 1
15+
ACCEPTED = 2
16+
ALREADY_ABSENT = 3
17+
18+
@classmethod
19+
def _missing_(cls, value):
20+
if not isinstance(value, int):
21+
return None
22+
member = int.__new__(cls, value)
23+
member._name_ = f"UNKNOWN_{value}"
24+
member._value_ = value
25+
return member
26+
27+
28+
@dataclass(frozen=True)
29+
class DeletionResult:
30+
"""Outcome for the original target; unknown outcomes never imply completion."""
31+
32+
outcome: DeletionOutcome
33+
sandbox_id: str | None = None

‎src/openshell/sandbox.py‎

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
openshell_pb2,
2929
openshell_pb2_grpc,
3030
)
31-
from .errors import GatewayError, _error_mapping_channel
31+
from .errors import _error_mapping_channel
32+
from .mutations import DeletionOutcome, DeletionResult
3233

3334
_ClientCallDetailsBase = namedtuple(
3435
"_ClientCallDetailsBase",
@@ -534,8 +535,10 @@ def exec_python(
534535
timeout_seconds=timeout_seconds,
535536
)
536537

537-
def delete(self) -> bool:
538-
return self._client.delete(self.sandbox.name, workspace=self._workspace)
538+
def delete(self, *, allow_missing: bool = False) -> DeletionResult:
539+
return self._client.delete(
540+
self.sandbox.name, workspace=self._workspace, allow_missing=allow_missing
541+
)
539542

540543
def stop(self) -> SandboxRef:
541544
self.sandbox = self._client.stop(self.sandbox.name, workspace=self._workspace)
@@ -956,14 +959,20 @@ def list_ids_for_all_workspaces(
956959
)
957960
]
958961

959-
def delete(self, sandbox_name: str, *, workspace: str) -> bool:
962+
def delete(
963+
self, sandbox_name: str, *, workspace: str, allow_missing: bool = False
964+
) -> DeletionResult:
960965
response = self._stub.DeleteSandbox(
961966
openshell_pb2.DeleteSandboxRequest(
962-
name=sandbox_name, workspace_scope=_workspace_scope(workspace)
967+
name=sandbox_name,
968+
workspace_scope=_workspace_scope(workspace),
969+
allow_missing=allow_missing,
963970
),
964971
timeout=self._timeout,
965972
)
966-
return bool(response.deleted)
973+
return DeletionResult(
974+
DeletionOutcome(response.outcome), response.sandbox_id or None
975+
)
967976

968977
def stop(self, sandbox_name: str, *, workspace: str) -> SandboxRef:
969978
response = self._stub.StopSandbox(
@@ -984,19 +993,25 @@ def start(self, sandbox_name: str, *, workspace: str) -> SandboxRef:
984993
return _sandbox_ref(response.sandbox)
985994

986995
def wait_deleted(
987-
self, sandbox_name: str, *, workspace: str, timeout_seconds: float = 60.0
996+
self,
997+
sandbox_name: str,
998+
*,
999+
workspace: str,
1000+
timeout_seconds: float = 60.0,
1001+
expected_sandbox_id: str | None = None,
9881002
) -> None:
9891003
deadline = time.time() + timeout_seconds
9901004
while time.time() < deadline:
9911005
try:
992-
self.get(sandbox_name, workspace=workspace)
993-
except grpc.RpcError as exc:
994-
call = exc.raw_error if isinstance(exc, GatewayError) else exc
1006+
current = self.get(sandbox_name, workspace=workspace)
9951007
if (
996-
isinstance(call, grpc.Call)
997-
and call.code() == grpc.StatusCode.NOT_FOUND
1008+
expected_sandbox_id is not None
1009+
and current.id != expected_sandbox_id
9981010
):
9991011
return
1012+
except grpc.RpcError as exc:
1013+
if getattr(exc, "code", lambda: None)() == grpc.StatusCode.NOT_FOUND:
1014+
return
10001015
raise
10011016
time.sleep(1)
10021017
raise SandboxError(f"sandbox {sandbox_name} was not deleted within timeout")
@@ -1330,14 +1345,18 @@ def list_all_for_all_workspaces(
13301345
label_selector=label_selector,
13311346
).all()
13321347

1333-
def delete(self, name: str, *, workspace: str) -> bool:
1348+
def delete(
1349+
self, name: str, *, workspace: str, allow_missing: bool = False
1350+
) -> DeletionResult:
13341351
response = self._stub.DeleteSandboxTemplate(
13351352
openshell_pb2.DeleteSandboxTemplateRequest(
1336-
name=name, workspace_scope=_workspace_scope(workspace)
1353+
name=name,
1354+
workspace_scope=_workspace_scope(workspace),
1355+
allow_missing=allow_missing,
13371356
),
13381357
timeout=self._timeout,
13391358
)
1340-
return bool(response.deleted)
1359+
return DeletionResult(DeletionOutcome(response.outcome))
13411360

13421361

13431362
@dataclass(frozen=True)
@@ -1425,12 +1444,14 @@ def list_all(
14251444
label_selector=label_selector,
14261445
).all()
14271446

1428-
def delete(self, name: str) -> bool:
1447+
def delete(self, name: str, *, allow_missing: bool = False) -> DeletionResult:
14291448
response = self._stub.DeleteWorkspace(
1430-
openshell_pb2.DeleteWorkspaceRequest(name=name),
1449+
openshell_pb2.DeleteWorkspaceRequest(
1450+
name=name, allow_missing=allow_missing
1451+
),
14311452
timeout=self._timeout,
14321453
)
1433-
return response.deleted
1454+
return DeletionResult(DeletionOutcome(response.outcome))
14341455

14351456

14361457
class Sandbox:
@@ -1556,20 +1577,20 @@ def __exit__(self, *args: object) -> None:
15561577
and self._session is not None
15571578
and self._client is not None
15581579
):
1559-
try:
1560-
deleted = self._session.delete()
1561-
if deleted:
1562-
self._client.wait_deleted(
1563-
self._session.sandbox.name,
1564-
workspace=self._workspace,
1565-
)
1566-
except grpc.RpcError as exc:
1567-
call = exc.raw_error if isinstance(exc, GatewayError) else exc
1568-
if (
1569-
not isinstance(call, grpc.Call)
1570-
or call.code() != grpc.StatusCode.NOT_FOUND
1571-
):
1572-
raise
1580+
result = self._session.delete(allow_missing=True)
1581+
if result.outcome == DeletionOutcome.ACCEPTED:
1582+
self._client.wait_deleted(
1583+
self._session.sandbox.name,
1584+
workspace=self._workspace,
1585+
expected_sandbox_id=result.sandbox_id,
1586+
)
1587+
elif result.outcome not in (
1588+
DeletionOutcome.COMPLETED,
1589+
DeletionOutcome.ALREADY_ABSENT,
1590+
):
1591+
raise SandboxError(
1592+
f"unsupported deletion outcome: {result.outcome}"
1593+
)
15731594
finally:
15741595
if self._client is not None:
15751596
self._client.close()

‎tests/sandbox_cleanup_test.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ def delete(request, context):
4545
state.calls.append("DeleteSandbox")
4646
assert request.name == "cleanup-test"
4747
assert request.workspace_scope.workspace == "default"
48+
assert request.allow_missing
49+
if state.code == grpc.StatusCode.NOT_FOUND:
50+
return openshell_pb2.DeleteSandboxResponse(
51+
outcome=openshell_pb2.DELETION_OUTCOME_ALREADY_ABSENT
52+
)
4853
fail(context)
4954

5055
server = grpc.server(futures.ThreadPoolExecutor(max_workers=1))
@@ -125,7 +130,9 @@ def test_wait_deleted_handles_intercepted_status(cleanup_client, code):
125130
grpc.StatusCode.UNAVAILABLE,
126131
],
127132
)
128-
def test_context_cleanup_handles_intercepted_status(cleanup_client, monkeypatch, code):
133+
def test_context_cleanup_handles_absence_and_intercepted_errors(
134+
cleanup_client, monkeypatch, code
135+
):
129136
client, state = cleanup_client
130137
state.code = code
131138
state.exists = True

‎tests/sandbox_test.py‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import openshell.sandbox as sandbox_module
2020
from openshell._proto import openshell_pb2
21+
from openshell.mutations import DeletionOutcome
2122
from openshell.sandbox import (
2223
_OIDC_TOKEN_EXPIRY_GRACE_SECONDS,
2324
_PYTHON_CLOUDPICKLE_BOOTSTRAP,
@@ -2013,7 +2014,7 @@ def DeleteSandbox(
20132014
) -> Any:
20142015
self.delete_request = request
20152016
_ = timeout
2016-
return SimpleNamespace(deleted=True)
2017+
return SimpleNamespace(outcome=1, sandbox_id="sb-1")
20172018

20182019
def StopSandbox(
20192020
self,
@@ -2122,7 +2123,7 @@ def DeleteSandboxTemplate(
21222123
) -> Any:
21232124
self.delete_template_request = request
21242125
_ = timeout
2125-
return SimpleNamespace(deleted=True)
2126+
return SimpleNamespace(outcome=1, sandbox_id="sb-1")
21262127

21272128

21282129
class _RecordingHighLevelClient:
@@ -2421,7 +2422,7 @@ def test_sandbox_template_client_crud_forwards_requests() -> None:
24212422
assert stub.list_template_request.label_selector == "team=runtime"
24222423
assert not _request_selects_all_workspaces(stub.list_template_request)
24232424

2424-
assert client.delete("gpu-kata", workspace="default") is True
2425+
assert client.delete("gpu-kata", workspace="default").outcome == 1
24252426
assert stub.delete_template_request is not None
24262427
assert stub.delete_template_request.name == "gpu-kata"
24272428
assert _request_workspace(stub.delete_template_request) == "default"
@@ -2835,9 +2836,32 @@ def test_delete_passes_workspace_to_proto() -> None:
28352836

28362837
result = client.delete("job-1", workspace="staging")
28372838

2838-
assert result is True
2839+
assert result.outcome == 1
28392840
assert stub.delete_request is not None
28402841
assert _request_workspace(stub.delete_request) == "staging"
2842+
assert not stub.delete_request.allow_missing
2843+
2844+
2845+
@pytest.mark.parametrize("outcome", [0, 1, 2, 3, 99])
2846+
def test_delete_preserves_outcome_and_identity(outcome: int) -> None:
2847+
class Stub:
2848+
def DeleteSandbox(self, request: Any, **_kwargs: Any) -> Any:
2849+
assert request.allow_missing
2850+
return openshell_pb2.DeleteSandboxResponse(
2851+
outcome=cast("openshell_pb2.DeletionOutcome", outcome),
2852+
sandbox_id="original-id",
2853+
)
2854+
2855+
result = _client_with_fake_stub(Stub()).delete(
2856+
"job", workspace="default", allow_missing=True
2857+
)
2858+
assert int(result.outcome) == outcome
2859+
assert result.sandbox_id == "original-id"
2860+
if outcome == 99:
2861+
assert result.outcome not in (
2862+
DeletionOutcome.COMPLETED,
2863+
DeletionOutcome.ALREADY_ABSENT,
2864+
)
28412865

28422866

28432867
def test_list_for_all_workspaces_sets_flag() -> None:

0 commit comments

Comments
 (0)