Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions crates/openshell-cli/src/commands/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use super::provider_readiness::{
ProviderMutationExpectation, ProviderWaitOptions, finish_provider_mutation,
warn_frozen_sandboxes_withholding,
};
use crate::color::Colorize;
use crate::commands::common::{
Expand Down Expand Up @@ -1784,6 +1785,8 @@ pub async fn provider_profile_import(
"s"
}
);
// Warnings, such as frozen sandboxes that keep their captured profile.
print_profile_diagnostics(&diagnostics);
return Ok(());
}
}
Expand Down Expand Up @@ -1831,6 +1834,8 @@ pub async fn provider_profile_update(
diagnostics.extend(response.diagnostics);
if response.updated {
println!("Updated provider profile.");
// Warnings, such as frozen sandboxes that keep their captured profile.
print_profile_diagnostics(&diagnostics);
return Ok(());
}
}
Expand Down Expand Up @@ -2957,7 +2962,15 @@ pub async fn provider_update(options: ProviderUpdateOptions<'_>) -> Result<()> {
"gateway did not return a provider mutation receipt; saved credentials cannot establish readiness"
));
}
finish_provider_mutation(
// Only sandboxes the caller selected: this workspace and this provider.
let attached_sandboxes = response
.target_receipts
.iter()
.filter(|receipt| receipt.workspace == workspace && receipt.provider == name)
.filter_map(|receipt| receipt.desired.as_ref())
.map(|desired| desired.sandbox.clone())
.collect();
let finished = finish_provider_mutation(
&client,
&response.mutation_id,
response.target_receipts,
Expand All @@ -2970,7 +2983,10 @@ pub async fn provider_update(options: ProviderUpdateOptions<'_>) -> Result<()> {
},
readiness,
)
.await
.await;
// The update is saved either way. A frozen sandbox may now be cut off.
warn_frozen_sandboxes_withholding(server, tls, workspace, name, attached_sandboxes).await;
finished
}

pub async fn provider_delete(
Expand Down
62 changes: 62 additions & 0 deletions crates/openshell-cli/src/commands/provider_readiness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use std::time::Duration;
use tokio::time::Instant;

const PROVIDER_WAIT_CONCURRENCY: usize = 16;
/// Bound on one freeze read behind the frozen-sandbox warning.
const FROZEN_WARNING_TIMEOUT: Duration = Duration::from_secs(10);

/// Output and deadline choices shared by provider mutation and status commands.
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -256,6 +258,62 @@ async fn wait_for_mutation_statuses(
results.into_iter().map(|(_, result)| result).collect()
}

/// Name the frozen sandboxes that no longer receive this provider's credentials.
///
/// A frozen sandbox keeps the provider authorization it captured. A provider
/// change that no longer matches it succeeds, and the gateway then withholds
/// the credentials from that sandbox. The readiness reason is the generic
/// `credentials_withheld`, so say which sandbox, why, and what repairs it.
/// Best effort: the mutation or status this follows has already succeeded.
pub(super) async fn warn_frozen_sandboxes_withholding(
server: &str,
tls: &TlsOptions,
workspace: &str,
provider: &str,
sandboxes: Vec<String>,
) {
if sandboxes.is_empty() {
return;
}
let Ok(Some(client)) = crate::tls::frozen_policies_client(server, tls).await else {
return;
};
let mut warnings = stream::iter(sandboxes.into_iter().map(|sandbox| {
let mut client = client.clone();
async move {
// Best effort: a slow gateway costs the warning, not the command.
let state = tokio::time::timeout(
FROZEN_WARNING_TIMEOUT,
client.get_policy(openshell_core::proto::GetFrozenSandboxPolicyRequest {
workspace_scope: Some(openshell_core::proto::workspace_selector(workspace)),
name: sandbox.clone(),
}),
)
.await
.ok()?
.ok()?
.into_inner();
let withheld = state
.withheld_providers
.into_iter()
.filter(|withheld| withheld.provider == provider)
.collect::<Vec<_>>();
let line = crate::run::withheld_provider_lines(&withheld)
.into_iter()
.next()?;
Some(format!("frozen sandbox '{workspace}/{sandbox}': {line}"))
}
}))
.buffer_unordered(PROVIDER_WAIT_CONCURRENCY)
.filter_map(|warning| async move { warning })
.collect::<Vec<_>>()
.await;
warnings.sort();
for warning in warnings {
eprintln!("Warning: {warning}");
}
}

/// Show desired/observed provider state, optionally waiting for that exact state.
pub async fn sandbox_provider_status(
server: &str,
Expand Down Expand Up @@ -313,6 +371,10 @@ pub async fn sandbox_provider_status(
};
let complete = result.complete;
print_statuses(&mutation_id, &[result], options.output)?;
// The readiness state lags until the supervisor reports. The freeze
// record already knows whether this provider is withheld.
warn_frozen_sandboxes_withholding(server, tls, workspace, provider, vec![name.to_string()])
.await;
if options.wait && !complete {
return Err(miette!(
"provider readiness wait did not complete; inspect the reported outcome"
Expand Down
53 changes: 53 additions & 0 deletions crates/openshell-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,14 @@ enum SandboxCommands {
#[arg(long, value_hint = ValueHint::FilePath)]
policy: Option<String>,

/// Freeze the first accepted policy and provider authorization until the sandbox is deleted.
///
/// Policy edits, proposal approvals, and provider attach or detach
/// then fail; recreate the sandbox to change them. Every gateway
/// replica must support frozen policies.
#[arg(long)]
frozen: bool,

/// Forward a local port to the sandbox before the initial command or shell starts.
/// Accepts [`bind_address`:]port (e.g. 8080, 0.0.0.0:8080). Keeps the sandbox alive.
#[arg(long, conflicts_with = "no_keep")]
Expand Down Expand Up @@ -1767,6 +1775,11 @@ enum SandboxCommands {
#[arg(long)]
selector: Option<String>,

/// Show only sandboxes created with `--frozen`, with their freeze state.
/// Pages without a frozen sandbox are skipped.
#[arg(long)]
frozen: bool,

/// Output format.
#[arg(short = 'o', long = "output", value_enum, default_value_t = OutputFormat::Table, conflicts_with_all = ["ids", "names"])]
output: OutputFormat,
Expand Down Expand Up @@ -3637,6 +3650,7 @@ async fn run_async() -> Result<()> {
driver_config_json,
providers,
policy,
frozen,
forward,
expose,
expose_authorization_mode,
Expand Down Expand Up @@ -3753,6 +3767,7 @@ async fn run_async() -> Result<()> {
restart_policy: &restart_policy,
provision_timeout_secs: timeout.map(std::num::NonZeroU32::get),
create_retry_timeout_secs,
frozen,
},
&cli.workspace,
&tls,
Expand Down Expand Up @@ -3837,6 +3852,7 @@ async fn run_async() -> Result<()> {
ids,
names,
selector,
frozen,
output,
all_workspaces,
} => {
Expand All @@ -3847,6 +3863,11 @@ async fn run_async() -> Result<()> {
ids,
names,
selector.as_deref(),
if frozen {
run::SandboxListFilter::Frozen
} else {
run::SandboxListFilter::All
},
output.as_str(),
&cli.workspace,
all_workspaces,
Expand Down Expand Up @@ -6741,6 +6762,38 @@ mod tests {
}
}

#[test]
fn sandbox_create_frozen_supports_inline_and_named_templates() {
for args in [
vec!["openshell", "sandbox", "create", "--frozen"],
vec![
"openshell",
"sandbox",
"create",
"--template",
"agent",
"--frozen",
],
] {
let cli = Cli::try_parse_from(args).unwrap();
assert!(matches!(
cli.command,
Some(Commands::Sandbox {
command: Some(SandboxCommands::Create { frozen: true, .. }),
..
})
));
}
let cli = Cli::try_parse_from(["openshell", "sandbox", "create"]).unwrap();
assert!(matches!(
cli.command,
Some(Commands::Sandbox {
command: Some(SandboxCommands::Create { frozen: false, .. }),
..
})
));
}

#[test]
fn sandbox_create_restart_policy_defaults_to_never() {
let cli = Cli::try_parse_from(["openshell", "sandbox", "create"]).unwrap();
Expand Down
Loading
Loading