Skip to content

Commit 8334767

Browse files
refactor(proto): move phase and current_policy_version into SandboxStatus
Move phase and current_policy_version from SandboxSpec into SandboxStatus to correctly model mutable runtime state. Update all callers in the gateway server, TUI, and Python SDK to read and write these fields through SandboxStatus accessors. Signed-off-by: Derek Carr <[email protected]>
1 parent cd70249 commit 8334767

16 files changed

Lines changed: 258 additions & 221 deletions

File tree

‎crates/openshell-cli/src/run.rs‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,11 +1842,11 @@ pub async fn sandbox_create(
18421842
.into_diagnostic()?
18431843
.into_inner();
18441844

1845-
let mut last_phase = sandbox.phase;
1845+
let mut last_phase = sandbox.phase();
18461846
let mut last_error_reason = String::new();
18471847
let mut last_condition_message = ready_false_condition_message(sandbox.status.as_ref());
18481848
// Track whether we have seen a non-Ready phase during the watch.
1849-
let mut saw_non_ready = SandboxPhase::try_from(sandbox.phase) != Ok(SandboxPhase::Ready);
1849+
let mut saw_non_ready = SandboxPhase::try_from(sandbox.phase()) != Ok(SandboxPhase::Ready);
18501850
let provision_timeout = Duration::from_secs(
18511851
std::env::var("OPENSHELL_PROVISION_TIMEOUT")
18521852
.ok()
@@ -1899,8 +1899,8 @@ pub async fn sandbox_create(
18991899
let evt = item.into_diagnostic()?;
19001900
match evt.payload {
19011901
Some(openshell_core::proto::sandbox_stream_event::Payload::Sandbox(s)) => {
1902-
let phase = SandboxPhase::try_from(s.phase).unwrap_or(SandboxPhase::Unknown);
1903-
last_phase = s.phase;
1902+
let phase = SandboxPhase::try_from(s.phase()).unwrap_or(SandboxPhase::Unknown);
1903+
last_phase = s.phase();
19041904
if let Some(message) = ready_false_condition_message(s.status.as_ref()) {
19051905
last_condition_message = Some(message);
19061906
}
@@ -2451,7 +2451,7 @@ pub async fn sandbox_get(
24512451
};
24522452
println!(" {} {}", "Id:".dimmed(), id);
24532453
println!(" {} {}", "Name:".dimmed(), name);
2454-
println!(" {} {}", "Phase:".dimmed(), phase_name(sandbox.phase));
2454+
println!(" {} {}", "Phase:".dimmed(), phase_name(sandbox.phase()));
24552455
println!(
24562456
" {} {}",
24572457
"Resource version:".dimmed(),
@@ -2535,11 +2535,11 @@ pub async fn sandbox_exec_grpc(
25352535
.ok_or_else(|| miette::miette!("sandbox not found"))?;
25362536

25372537
// Verify the sandbox is ready before issuing the exec.
2538-
if SandboxPhase::try_from(sandbox.phase) != Ok(SandboxPhase::Ready) {
2538+
if SandboxPhase::try_from(sandbox.phase()) != Ok(SandboxPhase::Ready) {
25392539
return Err(miette::miette!(
25402540
"sandbox '{}' is not ready (phase: {}); wait for it to reach Ready state",
25412541
name,
2542-
phase_name(sandbox.phase)
2542+
phase_name(sandbox.phase())
25432543
));
25442544
}
25452545

@@ -2747,11 +2747,11 @@ async fn fetch_ready_sandbox_for_forward(
27472747
.sandbox
27482748
.ok_or_else(|| miette::miette!("sandbox '{name}' not found"))?;
27492749

2750-
if SandboxPhase::try_from(sandbox.phase) != Ok(SandboxPhase::Ready) {
2750+
if SandboxPhase::try_from(sandbox.phase()) != Ok(SandboxPhase::Ready) {
27512751
return Err(miette::miette!(
27522752
"sandbox '{}' is no longer ready (phase: {}); stopping service forward",
27532753
name,
2754-
phase_name(sandbox.phase)
2754+
phase_name(sandbox.phase())
27552755
));
27562756
}
27572757

@@ -3195,8 +3195,8 @@ pub async fn sandbox_list(
31953195

31963196
// Print rows
31973197
for sandbox in sandboxes {
3198-
let phase = phase_name(sandbox.phase);
3199-
let phase_colored = match SandboxPhase::try_from(sandbox.phase) {
3198+
let phase = phase_name(sandbox.phase());
3199+
let phase_colored = match SandboxPhase::try_from(sandbox.phase()) {
32003200
Ok(SandboxPhase::Ready) => phase.green().to_string(),
32013201
Ok(SandboxPhase::Error) => phase.red().to_string(),
32023202
Ok(SandboxPhase::Provisioning) => phase.yellow().to_string(),
@@ -3224,8 +3224,8 @@ fn sandbox_to_json(sandbox: &Sandbox) -> serde_json::Value {
32243224
"labels": labels,
32253225
"resource_version": meta.map_or(0, |m| m.resource_version),
32263226
"created_at": format_epoch_ms(meta.map_or(0, |m| m.created_at_ms)),
3227-
"phase": phase_name(sandbox.phase),
3228-
"current_policy_version": sandbox.current_policy_version,
3227+
"phase": phase_name(sandbox.phase()),
3228+
"current_policy_version": sandbox.current_policy_version(),
32293229
})
32303230
}
32313231

@@ -7596,15 +7596,14 @@ mod tests {
75967596
let status = SandboxStatus {
75977597
sandbox_name: "gpu".to_string(),
75987598
agent_pod: "gpu-pod".to_string(),
7599-
agent_fd: String::new(),
7600-
sandbox_fd: String::new(),
76017599
conditions: vec![SandboxCondition {
76027600
r#type: "Ready".to_string(),
76037601
status: "False".to_string(),
76047602
reason: "Unschedulable".to_string(),
76057603
message: "Another GPU sandbox may already be using the available GPU.".to_string(),
76067604
last_transition_time: String::new(),
76077605
}],
7606+
..Default::default()
76087607
};
76097608

76107609
assert_eq!(
@@ -7618,15 +7617,14 @@ mod tests {
76187617
let status = SandboxStatus {
76197618
sandbox_name: "gpu".to_string(),
76207619
agent_pod: "gpu-pod".to_string(),
7621-
agent_fd: String::new(),
7622-
sandbox_fd: String::new(),
76237620
conditions: vec![SandboxCondition {
76247621
r#type: "Scheduled".to_string(),
76257622
status: "True".to_string(),
76267623
reason: "Scheduled".to_string(),
76277624
message: "Sandbox scheduled".to_string(),
76287625
last_transition_time: String::new(),
76297626
}],
7627+
..Default::default()
76307628
};
76317629

76327630
assert!(ready_false_condition_message(Some(&status)).is_none());

‎crates/openshell-cli/tests/provider_commands_integration.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ impl OpenShell for TestOpenShell {
126126
}),
127127
spec: None,
128128
status: None,
129-
phase: 0,
130-
current_policy_version: 0,
131129
}),
132130
}))
133131
}

‎crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs‎

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,19 @@ impl OpenShell for TestOpenShell {
7979
name
8080
};
8181

82-
Ok(Response::new(SandboxResponse {
83-
sandbox: Some(Sandbox {
84-
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {
85-
id: format!("id-{sandbox_name}"),
86-
name: sandbox_name,
87-
created_at_ms: 0,
88-
labels: HashMap::new(),
89-
resource_version: 0,
90-
}),
91-
phase: SandboxPhase::Provisioning as i32,
92-
..Sandbox::default()
82+
let mut sandbox = Sandbox {
83+
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {
84+
id: format!("id-{sandbox_name}"),
85+
name: sandbox_name,
86+
created_at_ms: 0,
87+
labels: HashMap::new(),
88+
resource_version: 0,
9389
}),
90+
..Sandbox::default()
91+
};
92+
sandbox.set_phase(SandboxPhase::Provisioning as i32);
93+
Ok(Response::new(SandboxResponse {
94+
sandbox: Some(sandbox),
9495
}))
9596
}
9697

@@ -99,18 +100,19 @@ impl OpenShell for TestOpenShell {
99100
request: tonic::Request<GetSandboxRequest>,
100101
) -> Result<Response<SandboxResponse>, Status> {
101102
let name = request.into_inner().name;
102-
Ok(Response::new(SandboxResponse {
103-
sandbox: Some(Sandbox {
104-
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {
105-
id: format!("id-{name}"),
106-
name,
107-
created_at_ms: 0,
108-
labels: HashMap::new(),
109-
resource_version: 0,
110-
}),
111-
phase: SandboxPhase::Ready as i32,
112-
..Sandbox::default()
103+
let mut sandbox = Sandbox {
104+
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {
105+
id: format!("id-{name}"),
106+
name,
107+
created_at_ms: 0,
108+
labels: HashMap::new(),
109+
resource_version: 0,
113110
}),
111+
..Sandbox::default()
112+
};
113+
sandbox.set_phase(SandboxPhase::Ready as i32);
114+
Ok(Response::new(SandboxResponse {
115+
sandbox: Some(sandbox),
114116
}))
115117
}
116118

@@ -351,38 +353,34 @@ impl OpenShell for TestOpenShell {
351353
let vm_log_churn_before_ready = self.state.vm_log_churn_before_ready.load(Ordering::SeqCst);
352354

353355
tokio::spawn(async move {
354-
let provisioning = Sandbox {
356+
let mut provisioning = Sandbox {
355357
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {
356358
id: sandbox_id.clone(),
357359
name: sandbox_id.trim_start_matches("id-").to_string(),
358360
created_at_ms: 0,
359361
labels: HashMap::new(),
360362
resource_version: 0,
361363
}),
362-
phase: SandboxPhase::Provisioning as i32,
363364
..Sandbox::default()
364365
};
365-
let error = Sandbox {
366-
phase: SandboxPhase::Error as i32,
366+
provisioning.set_phase(SandboxPhase::Provisioning as i32);
367+
let mut error = Sandbox {
367368
status: Some(SandboxStatus {
368369
sandbox_name: sandbox_id.trim_start_matches("id-").to_string(),
369-
agent_pod: String::new(),
370-
agent_fd: String::new(),
371-
sandbox_fd: String::new(),
372370
conditions: vec![SandboxCondition {
373371
r#type: "Ready".to_string(),
374372
status: "False".to_string(),
375373
reason: "ProcessExited".to_string(),
376374
message: "VM process exited with status 0".to_string(),
377375
last_transition_time: String::new(),
378376
}],
377+
..Default::default()
379378
}),
380379
..provisioning.clone()
381380
};
382-
let ready = Sandbox {
383-
phase: SandboxPhase::Ready as i32,
384-
..provisioning.clone()
385-
};
381+
error.set_phase(SandboxPhase::Error as i32);
382+
let mut ready = provisioning.clone();
383+
ready.set_phase(SandboxPhase::Ready as i32);
386384

387385
let _ = tx
388386
.send(Ok(SandboxStreamEvent {

‎crates/openshell-core/src/metadata.rs‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! These traits provide uniform access to `ObjectMeta` fields across all resource types.
77
88
use crate::proto::{
9-
InferenceRoute, ObjectForTest, Provider, Sandbox, ServiceEndpoint, SshSession,
9+
InferenceRoute, ObjectForTest, Provider, Sandbox, SandboxStatus, ServiceEndpoint, SshSession,
1010
StoredProviderCredentialRefreshState, StoredProviderProfile,
1111
};
1212
use std::collections::HashMap;
@@ -69,6 +69,26 @@ impl GetResourceVersion for Sandbox {
6969
}
7070
}
7171

72+
impl Sandbox {
73+
pub fn phase(&self) -> i32 {
74+
self.status.as_ref().map_or(0, |s| s.phase)
75+
}
76+
77+
pub fn set_phase(&mut self, phase: i32) {
78+
self.status.get_or_insert_with(SandboxStatus::default).phase = phase;
79+
}
80+
81+
pub fn current_policy_version(&self) -> u32 {
82+
self.status.as_ref().map_or(0, |s| s.current_policy_version)
83+
}
84+
85+
pub fn set_current_policy_version(&mut self, version: u32) {
86+
self.status
87+
.get_or_insert_with(SandboxStatus::default)
88+
.current_policy_version = version;
89+
}
90+
}
91+
7292
// Implementations for Provider
7393
impl ObjectId for Provider {
7494
fn object_id(&self) -> &str {

0 commit comments

Comments
 (0)