Skip to content

Commit 85f3acb

Browse files
authored
feat(python): expose sandbox status conditions in the SDK (ai-openshell/openshell#4436)
* feat(python): expose sandbox status conditions in the SDK Add SandboxConditionRef and surface status.conditions on SandboxStatusRef and SandboxRef. Callers need condition reasons and transition times to time terminal transitions and explain failures that happen before the main process starts, without reading the raw proto. Signed-off-by: Max Dubrinsky <mdubrinsky@nvidia.com> * test(python): use real gateway condition values in conditions fixtures Signed-off-by: Max Dubrinsky <mdubrinsky@nvidia.com> --------- Signed-off-by: Max Dubrinsky <mdubrinsky@nvidia.com>
1 parent 93b7140 commit 85f3acb

3 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎src/openshell/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Pager,
1717
Sandbox,
1818
SandboxClient,
19+
SandboxConditionRef,
1920
SandboxError,
2021
SandboxRef,
2122
SandboxSession,
@@ -51,6 +52,7 @@
5152
"ProviderCredentialValue",
5253
"Sandbox",
5354
"SandboxClient",
55+
"SandboxConditionRef",
5456
"SandboxError",
5557
"SandboxRef",
5658
"SandboxSession",

‎src/openshell/sandbox.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,15 @@ def _resolve_secret(self) -> str:
452452
return value
453453

454454

455+
@dataclass(frozen=True)
456+
class SandboxConditionRef:
457+
type: str
458+
status: str
459+
reason: str = ""
460+
message: str = ""
461+
transition_time_ms: int | None = None
462+
463+
455464
@dataclass(frozen=True)
456465
class SandboxStatusRef:
457466
phase: int
@@ -460,6 +469,7 @@ class SandboxStatusRef:
460469
restart_count: int = 0
461470
next_restart_at_ms: int | None = None
462471
main_process_started_at_ms: int | None = None
472+
conditions: tuple[SandboxConditionRef, ...] = ()
463473

464474

465475
class ServiceAuthorizationMode(IntEnum):
@@ -538,6 +548,10 @@ def phase(self) -> int:
538548
def current_policy_version(self) -> int:
539549
return self.status.current_policy_version
540550

551+
@property
552+
def conditions(self) -> tuple[SandboxConditionRef, ...]:
553+
return self.status.conditions
554+
541555

542556
@dataclass(frozen=True)
543557
class ExecChunk:
@@ -1772,6 +1786,25 @@ def _serialize_python_callable(
17721786
return base64.b64encode(payload).decode("ascii")
17731787

17741788

1789+
def _sandbox_conditions(
1790+
status: openshell_pb2.SandboxStatus | None,
1791+
) -> tuple[SandboxConditionRef, ...]:
1792+
if status is None:
1793+
return ()
1794+
return tuple(
1795+
SandboxConditionRef(
1796+
type=condition.type,
1797+
status=condition.status,
1798+
reason=condition.reason,
1799+
message=condition.message,
1800+
transition_time_ms=condition.transition_time.ToMilliseconds()
1801+
if condition.HasField("transition_time")
1802+
else None,
1803+
)
1804+
for condition in status.conditions
1805+
)
1806+
1807+
17751808
def _sandbox_ref(
17761809
sandbox: openshell_pb2.Sandbox,
17771810
service_urls: Mapping[str, str] | None = None,
@@ -1802,6 +1835,7 @@ def _sandbox_ref(
18021835
main_process_started_at_ms=status.main_process_started_time.ToMilliseconds()
18031836
if status is not None and status.HasField("main_process_started_time")
18041837
else None,
1838+
conditions=_sandbox_conditions(status),
18051839
),
18061840
labels=sandbox.metadata.labels if sandbox.metadata else {},
18071841
created_from_workload_template=provenance,

‎tests/sandbox_test.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
Pager,
2929
Sandbox,
3030
SandboxClient,
31+
SandboxConditionRef,
3132
SandboxError,
3233
SandboxRef,
3334
SandboxStatusRef,
@@ -2750,6 +2751,63 @@ def test_sandbox_ref_includes_main_process_result() -> None:
27502751
assert status.main_process_started_at_ms == 1_699_999_000_000
27512752

27522753

2754+
def test_sandbox_ref_includes_conditions() -> None:
2755+
proto = _make_sandbox_proto("sandbox-1", "job-1")
2756+
2757+
ready = proto.status.conditions.add()
2758+
ready.type = "Ready"
2759+
ready.status = "True"
2760+
ready.reason = "BackendReady"
2761+
ready.message = "Container is running"
2762+
ready.transition_time.FromMilliseconds(1_700_000_000_000)
2763+
2764+
failed = proto.status.conditions.add()
2765+
failed.type = "Ready"
2766+
failed.status = "Unknown"
2767+
failed.reason = "InspectFailed"
2768+
failed.message = "Container inspect failed: connection refused"
2769+
2770+
ref = _sandbox_ref(proto)
2771+
2772+
assert ref.status.conditions == (
2773+
SandboxConditionRef(
2774+
type="Ready",
2775+
status="True",
2776+
reason="BackendReady",
2777+
message="Container is running",
2778+
transition_time_ms=1_700_000_000_000,
2779+
),
2780+
SandboxConditionRef(
2781+
type="Ready",
2782+
status="Unknown",
2783+
reason="InspectFailed",
2784+
message="Container inspect failed: connection refused",
2785+
transition_time_ms=None,
2786+
),
2787+
)
2788+
# Surfaces the same conditions through SandboxRef directly.
2789+
assert ref.conditions == ref.status.conditions
2790+
2791+
2792+
def test_sandbox_ref_conditions_default_to_empty() -> None:
2793+
ref = _sandbox_ref(_make_sandbox_proto("sandbox-1", "job-1"))
2794+
2795+
assert ref.status.conditions == ()
2796+
assert ref.conditions == ()
2797+
2798+
2799+
def test_direct_sandbox_ref_construction_defaults_conditions() -> None:
2800+
ref = SandboxRef(
2801+
id="sandbox-1",
2802+
name="job-1",
2803+
workspace="default",
2804+
status=SandboxStatusRef(phase=2, current_policy_version=0),
2805+
)
2806+
2807+
assert ref.status.conditions == ()
2808+
assert ref.conditions == ()
2809+
2810+
27532811
def test_returned_labels_are_immutable() -> None:
27542812
proto = _make_sandbox_proto("sandbox-1", "job-1", {"aiq": "deep-research"})
27552813
ref = _sandbox_ref(proto)

0 commit comments

Comments
 (0)