Skip to content

Commit 45f537a

Browse files
authored
feat(sandbox): expose Stop/Start RPCs in gateway API + CLI (#3)
The driver-layer stop_sandbox already exists for every compute backend (podman, docker, kubernetes, vm) and the internal compute_driver.proto exposes a StopSandbox RPC. But the gateway-facing openshell.proto only surfaces DeleteSandbox, which destroys the workspace volume alongside the container. Operators who want to halt a sandbox for resource reasons have to delete it and lose state. This patch adds: - StopSandbox and StartSandbox RPCs to proto/openshell.proto with matching Request/Response messages. - Compute-runtime wrappers (stop_sandbox, start_sandbox) that look up the sandbox by name, forward stop to the driver, and forward start to the StartupResume hook so the existing Docker resume path is reused. - A resume_sandbox method on PodmanComputeDriver mirroring the Docker implementation, plus a StartupResume impl wired in new_podman so the Start verb works on podman too. - gRPC handlers (handle_stop_sandbox, handle_start_sandbox), trait wires, and matching authz entries (both gated on sandbox:write). - CLI verbs: openshell sandbox stop [--all] and openshell sandbox start with completion for sandbox names and the same forward-cleanup behavior as delete. Phase is left to the existing watch loop to update as containers transition; no new SandboxPhase variant is required. Restart-from- stopped works mid-session via Start and at gateway boot via the existing resume_persisted_sandboxes sweep. Tests: - Podman driver: resume_sandbox returns Ok(false) when container is missing, is a no-op when already running, and issues POST /start when stopped. - Compute wrappers: NotFound for missing sandboxes, Unimplemented when no StartupResume hook is configured, forwards to the hook with the correct id/name, surfaces the hook's Ok(false) verdict. - gRPC handlers: empty-name InvalidArgument and missing-sandbox NotFound for both Stop and Start. The seven mock OpenShell impls in integration tests are updated to satisfy the expanded trait surface.
1 parent fb9f4ff commit 45f537a

16 files changed

Lines changed: 673 additions & 21 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,31 @@ enum SandboxCommands {
13101310
all: bool,
13111311
},
13121312

1313+
/// Stop a sandbox container without deleting it.
1314+
///
1315+
/// Workspace volume, provider links, and the sandbox record survive.
1316+
/// Use `sandbox start` to bring it back live.
1317+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1318+
Stop {
1319+
/// Sandbox names.
1320+
#[arg(required_unless_present = "all", num_args = 1.., value_name = "NAME", add = ArgValueCompleter::new(completers::complete_sandbox_names))]
1321+
names: Vec<String>,
1322+
1323+
/// Stop all sandboxes.
1324+
#[arg(long, conflicts_with = "names")]
1325+
all: bool,
1326+
},
1327+
1328+
/// Start a previously-stopped sandbox container.
1329+
///
1330+
/// Idempotent: succeeds when the sandbox is already running.
1331+
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1332+
Start {
1333+
/// Sandbox names.
1334+
#[arg(required = true, num_args = 1.., value_name = "NAME", add = ArgValueCompleter::new(completers::complete_sandbox_names))]
1335+
names: Vec<String>,
1336+
},
1337+
13131338
/// Execute a command in a running sandbox.
13141339
///
13151340
/// Runs a command inside an existing sandbox using the gRPC exec endpoint.
@@ -2672,6 +2697,12 @@ async fn main() -> Result<()> {
26722697
SandboxCommands::Delete { names, all } => {
26732698
run::sandbox_delete(endpoint, &names, all, &tls, &ctx.name).await?;
26742699
}
2700+
SandboxCommands::Stop { names, all } => {
2701+
run::sandbox_stop(endpoint, &names, all, &tls).await?;
2702+
}
2703+
SandboxCommands::Start { names } => {
2704+
run::sandbox_start(endpoint, &names, &tls).await?;
2705+
}
26752706
SandboxCommands::Connect { name, editor } => {
26762707
let name = resolve_sandbox_name(name, &ctx.name)?;
26772708
if let Some(editor) = editor.map(Into::into) {

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

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ use openshell_core::proto::{
4848
ProviderProfileDiagnostic, ProviderProfileImportItem, RejectDraftChunkRequest,
4949
RevokeSshSessionRequest, RotateProviderCredentialRequest, Sandbox, SandboxPhase, SandboxPolicy,
5050
SandboxSpec, SandboxTemplate, ServiceEndpointResponse, SetClusterInferenceRequest,
51-
SettingScope, SettingValue, TcpForwardFrame, TcpForwardInit, TcpRelayTarget,
52-
UpdateConfigRequest, UpdateProviderRequest, WatchSandboxRequest, exec_sandbox_event,
53-
setting_value, tcp_forward_init,
51+
SettingScope, SettingValue, StartSandboxRequest, StopSandboxRequest, TcpForwardFrame,
52+
TcpForwardInit, TcpRelayTarget, UpdateConfigRequest, UpdateProviderRequest,
53+
WatchSandboxRequest, exec_sandbox_event, setting_value, tcp_forward_init,
5454
};
5555
use openshell_core::settings::{self, SettingValueKind};
5656
use openshell_core::{ObjectId, ObjectName};
@@ -3428,6 +3428,80 @@ pub async fn sandbox_delete(
34283428
Ok(())
34293429
}
34303430

3431+
/// Stop a sandbox by name without deleting it. Workspace volume and
3432+
/// provider links survive; use `sandbox start` to bring it back live.
3433+
pub async fn sandbox_stop(
3434+
server: &str,
3435+
names: &[String],
3436+
all: bool,
3437+
tls: &TlsOptions,
3438+
) -> Result<()> {
3439+
let mut client = grpc_client(server, tls).await?;
3440+
3441+
let names_to_stop: Vec<String> = if all {
3442+
let response = client
3443+
.list_sandboxes(ListSandboxesRequest {
3444+
limit: 1000,
3445+
offset: 0,
3446+
label_selector: String::new(),
3447+
})
3448+
.await
3449+
.into_diagnostic()?;
3450+
let sandboxes = response.into_inner().sandboxes;
3451+
if sandboxes.is_empty() {
3452+
println!("No sandboxes to stop.");
3453+
return Ok(());
3454+
}
3455+
sandboxes
3456+
.into_iter()
3457+
.map(|s| s.object_name().to_string())
3458+
.collect()
3459+
} else {
3460+
names.to_vec()
3461+
};
3462+
3463+
for name in &names_to_stop {
3464+
if let Ok(stopped) = stop_forwards_for_sandbox(name) {
3465+
for port in stopped {
3466+
eprintln!(
3467+
"{} Stopped forward of port {port} for sandbox {name}",
3468+
"✓".green().bold(),
3469+
);
3470+
}
3471+
}
3472+
3473+
client
3474+
.stop_sandbox(StopSandboxRequest { name: name.clone() })
3475+
.await
3476+
.into_diagnostic()?;
3477+
println!("{} Stopped sandbox {name}", "✓".green().bold());
3478+
}
3479+
3480+
Ok(())
3481+
}
3482+
3483+
/// Start a previously-stopped sandbox by name. Idempotent: succeeds when
3484+
/// the sandbox is already running. Fails if the backend resource has
3485+
/// been pruned (e.g. by manual container removal).
3486+
pub async fn sandbox_start(server: &str, names: &[String], tls: &TlsOptions) -> Result<()> {
3487+
let mut client = grpc_client(server, tls).await?;
3488+
for name in names {
3489+
let response = client
3490+
.start_sandbox(StartSandboxRequest { name: name.clone() })
3491+
.await
3492+
.into_diagnostic()?;
3493+
if response.into_inner().started {
3494+
println!("{} Started sandbox {name}", "✓".green().bold());
3495+
} else {
3496+
println!(
3497+
"{} Sandbox {name} record exists but backend resource is missing",
3498+
"!".yellow(),
3499+
);
3500+
}
3501+
}
3502+
Ok(())
3503+
}
3504+
34313505
/// Return the provider type inferred from the trailing command, if any.
34323506
fn inferred_provider_type(command: &[String]) -> Option<String> {
34333507
detect_provider_from_command(command).map(str::to_string)

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use openshell_core::proto::{
2525
ListProvidersRequest, ListProvidersResponse, ListSandboxProvidersRequest,
2626
ListSandboxProvidersResponse, ListSandboxesRequest, ListSandboxesResponse, Provider,
2727
ProviderResponse, RevokeSshSessionRequest, RevokeSshSessionResponse, SandboxResponse,
28-
SandboxStreamEvent, ServiceStatus, SupervisorMessage, UpdateProviderRequest,
28+
SandboxStreamEvent, ServiceStatus, StartSandboxRequest, StartSandboxResponse,
29+
StopSandboxRequest, StopSandboxResponse, SupervisorMessage, UpdateProviderRequest,
2930
WatchSandboxRequest,
3031
};
3132
use openshell_core::{ObjectId, ObjectName};
@@ -134,6 +135,20 @@ impl OpenShell for TestOpenShell {
134135
Ok(Response::new(DeleteSandboxResponse { deleted: true }))
135136
}
136137

138+
async fn stop_sandbox(
139+
&self,
140+
_request: tonic::Request<StopSandboxRequest>,
141+
) -> Result<Response<StopSandboxResponse>, Status> {
142+
Ok(Response::new(StopSandboxResponse {}))
143+
}
144+
145+
async fn start_sandbox(
146+
&self,
147+
_request: tonic::Request<StartSandboxRequest>,
148+
) -> Result<Response<StartSandboxResponse>, Status> {
149+
Ok(Response::new(StartSandboxResponse { started: true }))
150+
}
151+
137152
async fn get_sandbox_config(
138153
&self,
139154
_request: tonic::Request<GetSandboxConfigRequest>,

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ impl OpenShell for TestOpenShell {
102102
))
103103
}
104104

105+
async fn stop_sandbox(
106+
&self,
107+
_request: tonic::Request<openshell_core::proto::StopSandboxRequest>,
108+
) -> Result<Response<openshell_core::proto::StopSandboxResponse>, Status> {
109+
Ok(Response::new(openshell_core::proto::StopSandboxResponse {}))
110+
}
111+
112+
async fn start_sandbox(
113+
&self,
114+
_request: tonic::Request<openshell_core::proto::StartSandboxRequest>,
115+
) -> Result<Response<openshell_core::proto::StartSandboxResponse>, Status> {
116+
Ok(Response::new(openshell_core::proto::StartSandboxResponse {
117+
started: true,
118+
}))
119+
}
120+
105121
async fn get_sandbox_config(
106122
&self,
107123
_request: tonic::Request<openshell_core::proto::GetSandboxConfigRequest>,

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use openshell_core::proto::{
2525
ProviderCredentialRefreshStrategy, ProviderProfile, ProviderProfileCredential,
2626
ProviderResponse, RevokeSshSessionRequest, RevokeSshSessionResponse,
2727
RotateProviderCredentialRequest, RotateProviderCredentialResponse, Sandbox, SandboxResponse,
28-
SandboxStreamEvent, ServiceStatus, SupervisorMessage, UpdateProviderRequest,
28+
SandboxStreamEvent, ServiceStatus, StartSandboxRequest, StartSandboxResponse,
29+
StopSandboxRequest, StopSandboxResponse, SupervisorMessage, UpdateProviderRequest,
2930
WatchSandboxRequest,
3031
};
3132
use openshell_core::{ObjectId, ObjectName};
@@ -259,6 +260,20 @@ impl OpenShell for TestOpenShell {
259260
Ok(Response::new(DeleteSandboxResponse { deleted: true }))
260261
}
261262

263+
async fn stop_sandbox(
264+
&self,
265+
_request: tonic::Request<StopSandboxRequest>,
266+
) -> Result<Response<StopSandboxResponse>, Status> {
267+
Ok(Response::new(StopSandboxResponse {}))
268+
}
269+
270+
async fn start_sandbox(
271+
&self,
272+
_request: tonic::Request<StartSandboxRequest>,
273+
) -> Result<Response<StartSandboxResponse>, Status> {
274+
Ok(Response::new(StartSandboxResponse { started: true }))
275+
}
276+
262277
async fn get_sandbox_config(
263278
&self,
264279
_request: tonic::Request<GetSandboxConfigRequest>,

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ use openshell_core::proto::{
2323
ListSandboxProvidersResponse, ListSandboxesRequest, ListSandboxesResponse, PlatformEvent,
2424
ProviderResponse, RevokeSshSessionRequest, RevokeSshSessionResponse, Sandbox, SandboxCondition,
2525
SandboxLogLine, SandboxPhase, SandboxResponse, SandboxStatus, SandboxStreamEvent,
26-
ServiceStatus, SupervisorMessage, UpdateProviderRequest, WatchSandboxRequest,
26+
ServiceStatus, StartSandboxRequest, StartSandboxResponse, StopSandboxRequest,
27+
StopSandboxResponse, SupervisorMessage, UpdateProviderRequest, WatchSandboxRequest,
2728
sandbox_stream_event,
2829
};
2930
use std::collections::HashMap;
@@ -154,6 +155,20 @@ impl OpenShell for TestOpenShell {
154155
Ok(Response::new(DeleteSandboxResponse { deleted: true }))
155156
}
156157

158+
async fn stop_sandbox(
159+
&self,
160+
_request: tonic::Request<StopSandboxRequest>,
161+
) -> Result<Response<StopSandboxResponse>, Status> {
162+
Ok(Response::new(StopSandboxResponse {}))
163+
}
164+
165+
async fn start_sandbox(
166+
&self,
167+
_request: tonic::Request<StartSandboxRequest>,
168+
) -> Result<Response<StartSandboxResponse>, Status> {
169+
Ok(Response::new(StartSandboxResponse { started: true }))
170+
}
171+
157172
async fn get_sandbox_config(
158173
&self,
159174
_request: tonic::Request<GetSandboxConfigRequest>,

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ use openshell_core::proto::{
2121
GetSandboxProviderEnvironmentResponse, GetSandboxRequest, HealthRequest, HealthResponse,
2222
ListProvidersRequest, ListProvidersResponse, ListSandboxProvidersRequest,
2323
ListSandboxProvidersResponse, ListSandboxesRequest, ListSandboxesResponse, ProviderResponse,
24-
Sandbox, SandboxPolicy, SandboxResponse, SandboxStreamEvent, ServiceStatus, SupervisorMessage,
25-
UpdateProviderRequest, WatchSandboxRequest,
24+
Sandbox, SandboxPolicy, SandboxResponse, SandboxStreamEvent, ServiceStatus,
25+
StartSandboxRequest, StartSandboxResponse, StopSandboxRequest, StopSandboxResponse,
26+
SupervisorMessage, UpdateProviderRequest, WatchSandboxRequest,
2627
};
2728
use std::sync::Arc;
2829
use tempfile::TempDir;
@@ -119,6 +120,20 @@ impl OpenShell for TestOpenShell {
119120
Ok(Response::new(DeleteSandboxResponse { deleted: true }))
120121
}
121122

123+
async fn stop_sandbox(
124+
&self,
125+
_request: tonic::Request<StopSandboxRequest>,
126+
) -> Result<Response<StopSandboxResponse>, Status> {
127+
Ok(Response::new(StopSandboxResponse {}))
128+
}
129+
130+
async fn start_sandbox(
131+
&self,
132+
_request: tonic::Request<StartSandboxRequest>,
133+
) -> Result<Response<StartSandboxResponse>, Status> {
134+
Ok(Response::new(StartSandboxResponse { started: true }))
135+
}
136+
122137
async fn get_sandbox_config(
123138
&self,
124139
request: tonic::Request<GetSandboxConfigRequest>,

0 commit comments

Comments
 (0)