Skip to content

Commit 26e5a1a

Browse files
committed
fix(cli): explain sessionless topologies instead of surfacing a raw error
Creating a sandbox in a topology with no in-sandbox supervisor succeeded, then failed at the interactive-session step with a bare gRPC error. The sandbox was running and its network policy enforced, but the output read as a failed create. Detect the gateway's rejection and print what actually happened: the sandbox is running, sessions are unavailable for this topology, egress is unaffected, and which topologies to use when interactive access is required. When a command was passed to 'sandbox create', say plainly that it did not run and point at the containers.agent.command entrypoint override instead. The command still exits non-zero. A command that did not run must not report success, and callers should not have to parse output to find that out. Detection keys on a stable marker constant shared through openshell-core rather than on prose, so rewording the message cannot silently break it, and it searches the whole error chain because the marker arrives wrapped in a transport error. Signed-off-by: Russell Bryant <rbryant@redhat.com>
1 parent c85fb8c commit 26e5a1a

3 files changed

Lines changed: 127 additions & 3 deletions

File tree

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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,55 @@ fn validate_memory_quantity(value: &str) -> Result<String> {
333333
Ok(value.to_string())
334334
}
335335

336+
/// True when an error is the gateway rejecting a relay-backed operation because
337+
/// the sandbox's topology has no in-sandbox supervisor.
338+
fn is_no_supervisor_session_error(err: &miette::Report) -> bool {
339+
let marker = openshell_core::error::NO_SUPERVISOR_SESSION_MARKER;
340+
// Check the whole chain: the marker may sit in a wrapped transport error
341+
// rather than the outermost message.
342+
format!("{err}").contains(marker)
343+
|| err
344+
.chain()
345+
.any(|source| source.to_string().contains(marker))
346+
}
347+
348+
/// Explain a topology that cannot open sessions, instead of letting a raw gRPC
349+
/// error imply the sandbox failed to start.
350+
///
351+
/// The sandbox is running and its network policy is enforced; only the
352+
/// interactive path is unavailable. The command still exits non-zero, because
353+
/// a command passed to `sandbox create` did not run and callers must not read
354+
/// success from the exit code.
355+
fn report_no_supervisor_session(sandbox_name: &str, had_command: bool, persisted: bool) {
356+
eprintln!();
357+
eprintln!(
358+
"{} Sandbox '{}' is running, but this topology cannot open sessions.",
359+
"!".yellow().bold(),
360+
sandbox_name.bold()
361+
);
362+
eprintln!(" SSH, exec, port forwarding, and file transfer need a supervisor inside");
363+
eprintln!(" the sandbox, which this topology does not run.");
364+
eprintln!();
365+
if had_command {
366+
eprintln!(" {} your command did not run.", "Note:".bold());
367+
eprintln!(" Set the workload entrypoint instead, so it starts with the container:");
368+
eprintln!(
369+
" --driver-config-json '{{\"kubernetes\":{{\"containers\":{{\"agent\":{{\"command\":[...]}}}}}}}}'"
370+
);
371+
} else {
372+
eprintln!(" Policy-enforced network egress is unaffected.");
373+
}
374+
eprintln!();
375+
if persisted {
376+
eprintln!(" Inspect it with:");
377+
eprintln!(" openshell logs {sandbox_name}");
378+
eprintln!(" openshell sandbox list");
379+
}
380+
eprintln!(" Use the `combined`, `sidecar`, or `cni-sidecar` topology when you need");
381+
eprintln!(" interactive sessions.");
382+
}
383+
384+
#[allow(clippy::too_many_arguments)]
336385
async fn finalize_sandbox_create_session(
337386
server: &str,
338387
sandbox_name: &str,
@@ -341,8 +390,20 @@ async fn finalize_sandbox_create_session(
341390
workspace: &str,
342391
tls: &TlsOptions,
343392
gateway: &str,
393+
had_command: bool,
344394
) -> Result<()> {
395+
let sessionless = session_result
396+
.as_ref()
397+
.err()
398+
.is_some_and(is_no_supervisor_session_error);
399+
345400
if persist {
401+
if sessionless {
402+
report_no_supervisor_session(sandbox_name, had_command, true);
403+
return Err(miette::miette!(
404+
"sandbox '{sandbox_name}' is running but cannot open interactive sessions"
405+
));
406+
}
346407
return session_result;
347408
}
348409

@@ -354,6 +415,15 @@ async fn finalize_sandbox_create_session(
354415
eprintln!("Failed to delete sandbox {sandbox_name}: {err}");
355416
}
356417

418+
if sessionless {
419+
// The sandbox has already been deleted per --no-keep, so do not point
420+
// the reader at commands that would now fail.
421+
report_no_supervisor_session(sandbox_name, had_command, false);
422+
return Err(miette::miette!(
423+
"sandbox '{sandbox_name}' could not open an interactive session"
424+
));
425+
}
426+
357427
session_result
358428
}
359429

@@ -961,6 +1031,7 @@ pub async fn sandbox_create(
9611031
workspace,
9621032
&effective_tls,
9631033
gateway_name,
1034+
false,
9641035
)
9651036
.await;
9661037
}
@@ -1000,6 +1071,7 @@ pub async fn sandbox_create(
10001071
workspace,
10011072
&effective_tls,
10021073
gateway_name,
1074+
true,
10031075
)
10041076
.await
10051077
}
@@ -7658,6 +7730,39 @@ mod tests {
76587730
assert!(sandbox_should_persist(true, None));
76597731
}
76607732

7733+
use crate::run::is_no_supervisor_session_error;
7734+
7735+
#[test]
7736+
fn detects_the_no_supervisor_session_rejection() {
7737+
let err = miette::miette!("{}", openshell_core::error::no_supervisor_session_message());
7738+
assert!(is_no_supervisor_session_error(&err));
7739+
}
7740+
7741+
#[test]
7742+
fn other_errors_are_not_mistaken_for_a_sessionless_topology() {
7743+
for message in [
7744+
"supervisor session not connected",
7745+
"sandbox not found",
7746+
"timed out waiting for the sandbox to become ready",
7747+
] {
7748+
let err = miette::miette!("{message}");
7749+
assert!(
7750+
!is_no_supervisor_session_error(&err),
7751+
"{message} must not be treated as a sessionless topology"
7752+
);
7753+
}
7754+
}
7755+
7756+
/// The marker travels through gRPC as part of the status message, so
7757+
/// detection has to survive the wrapping the transport and CLI add.
7758+
#[test]
7759+
fn detection_survives_error_wrapping() {
7760+
let inner =
7761+
Status::failed_precondition(openshell_core::error::no_supervisor_session_message());
7762+
let err = miette::miette!("failed to open session: {inner}");
7763+
assert!(is_no_supervisor_session_error(&err));
7764+
}
7765+
76617766
#[test]
76627767
fn sandbox_should_not_persist_when_no_keep_is_set() {
76637768
assert!(!sandbox_should_persist(false, None));

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,24 @@ impl From<ComputeDriverError> for tonic::Status {
138138
}
139139
}
140140
}
141+
142+
/// Stable marker embedded in the gateway's rejection of relay-backed RPCs for
143+
/// sandboxes whose topology has no in-sandbox process supervisor.
144+
///
145+
/// SSH, `exec`, port forwarding, and file transfer all travel over the
146+
/// supervisor session, which such a topology never opens. The CLI matches on
147+
/// this marker to explain the situation rather than surfacing a raw gRPC
148+
/// error, so callers must keep the two in sync. It is deliberately a distinct
149+
/// token rather than prose so rewording the message cannot break detection.
150+
pub const NO_SUPERVISOR_SESSION_MARKER: &str = "openshell:no-supervisor-session";
151+
152+
/// Full message returned for relay-backed RPCs against such a sandbox.
153+
#[must_use]
154+
pub fn no_supervisor_session_message() -> String {
155+
format!(
156+
"this sandbox's topology runs no supervisor inside the sandbox, so SSH, exec, port \
157+
forwarding, and file transfer are unavailable. Policy-enforced network egress is \
158+
unaffected. Use the `combined`, `sidecar`, or `cni-sidecar` topology when interactive \
159+
sessions are required. [{NO_SUPERVISOR_SESSION_MARKER}]"
160+
)
161+
}

‎crates/openshell-server/src/supervisor_session.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ impl SupervisorSessionRegistry {
190190
// burning the caller's timeout on a wait that cannot succeed.
191191
if self.is_sessionless(sandbox_id) {
192192
return Err(Status::failed_precondition(
193-
"this sandbox runs a topology with no in-sandbox supervisor, so SSH, exec, \
194-
port forwarding, and file transfer are unavailable; use the `combined` or \
195-
`sidecar` topology when those are required",
193+
openshell_core::error::no_supervisor_session_message(),
196194
));
197195
}
198196

0 commit comments

Comments
 (0)