From dfeffde41c9420cfd3d80c2dd533a02a8dbd4824 Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 16:49:25 +0700 Subject: [PATCH 01/86] fix(install): keep the host's scripts current on reruns and updates A rerun at the same version returned before host_units, so a missing pyo-reset-login stayed missing. Updates from the dashboard never touched the host at all. Reruns now write the host units every time. Pyo serves install.sh's host_units at /install/host-units.sh, and the host's tailnet timer runs it whenever it changes, so an update brings new host scripts too. --- crates/pyo-api/src/join.rs | 49 ++++++++++++++++++++++++++++++++++++++ crates/pyo-api/src/lib.rs | 1 + deploy/install.sh | 10 ++++++++ scripts/pyo-up.sh | 7 ++++++ 4 files changed, 67 insertions(+) diff --git a/crates/pyo-api/src/join.rs b/crates/pyo-api/src/join.rs index 15ef499..963e224 100644 --- a/crates/pyo-api/src/join.rs +++ b/crates/pyo-api/src/join.rs @@ -357,10 +357,59 @@ pub async fn serve_sandbox() -> impl axum::response::IntoResponse { ) } +/// install.sh's `host_units`: the scripts and timers on the machine Pyo +/// runs on. The host's tailnet timer runs it whenever it changes, so an +/// update from the dashboard brings them too. +static HOST_UNITS: std::sync::LazyLock = std::sync::LazyLock::new(|| { + let install = include_str!("../../../deploy/install.sh"); + let mut lines = install.lines().skip_while(|l| *l != "host_units() {"); + let mut body = String::new(); + // Its end is the first "}" line outside the scripts it writes (their + // own functions end the same way). + let mut heredoc: Option = None; + for line in lines.by_ref() { + body.push_str(line); + body.push('\n'); + match &heredoc { + Some(end) if line == end => heredoc = None, + Some(_) => {} + None if line == "}" => break, + None => { + if let Some((_, rest)) = line.split_once("<<'") { + heredoc = rest.split_once('\'').map(|(d, _)| d.to_owned()); + } + } + } + } + assert!(body.ends_with("\n}\n"), "install.sh's host_units() ends"); + format!("#!/usr/bin/env bash\nset -euo pipefail\n{body}host_units\n") +}); + +/// GET /install/host-units.sh +pub async fn serve_host_units() -> impl axum::response::IntoResponse { + ( + [( + axum::http::header::CONTENT_TYPE, + "text/x-shellscript; charset=utf-8", + )], + HOST_UNITS.as_str(), + ) +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn host_units_script_is_install_sh_s() { + let s = HOST_UNITS.as_str(); + assert!(s.starts_with("#!/usr/bin/env bash\nset -euo pipefail\nhost_units() {\n")); + assert!(s.ends_with("\n}\nhost_units\n")); + assert!(s.contains("/usr/local/bin/pyo-reset-login")); + assert!(s.contains("http://127.0.0.1:8000/install/host-units.sh")); + assert!(s.contains("systemctl enable --now pyo-connect.timer pyo-tailnet.timer")); + } + /// connect.sh, join.sh and install.sh carry the AppArmor workaround's /// profile: it must be the asset's, character for character. #[test] diff --git a/crates/pyo-api/src/lib.rs b/crates/pyo-api/src/lib.rs index 67cdf9e..dac8d4f 100644 --- a/crates/pyo-api/src/lib.rs +++ b/crates/pyo-api/src/lib.rs @@ -453,6 +453,7 @@ pub async fn run(ext: Extensions) -> anyhow::Result<()> { .route("/install/join.sh", get(join::serve)) .route("/install/connect.sh", get(join::serve_connect)) .route("/install/sandbox.sh", get(join::serve_sandbox)) + .route("/install/host-units.sh", get(join::serve_host_units)) .fallback_service(tower_http::compression::Compression::new(spa)) .layer(axum::middleware::from_fn_with_state( state.config.cookie_secure, diff --git a/deploy/install.sh b/deploy/install.sh index c1be621..5e1404a 100644 --- a/deploy/install.sh +++ b/deploy/install.sh @@ -888,6 +888,13 @@ case "$r" in fi ;; esac +# The host setup of the Pyo running now, once per version: an update from +# the dashboard brings new scripts and timers here too. Last, since it +# rewrites this script. +units=$(curl -fsS -m 30 http://127.0.0.1:8000/install/host-units.sh) || exit 0 +sum=$(sha256sum <<<"$units" | cut -d' ' -f1) +[ "$sum" != "$(cat /etc/pyo/host-units 2>/dev/null)" ] || exit 0 +exec bash -c "$units"$'\n'"echo $sum > /etc/pyo/host-units" SH "${sudo[@]}" tee /usr/local/lib/pyo/pyo-reset-login >/dev/null <<'SH' #!/usr/bin/env bash @@ -1565,6 +1572,9 @@ main() { cur=$(current_version) if ! check_update "$cur"; then install_k3s + # Pyo's scripts on this machine, like pyo-reset-login, as this + # release has them. + host_units say "Pyo $R_VERSION is already installed. Nothing to update" pyo_addresses print_sign_in diff --git a/scripts/pyo-up.sh b/scripts/pyo-up.sh index 21cd360..46943d7 100755 --- a/scripts/pyo-up.sh +++ b/scripts/pyo-up.sh @@ -551,6 +551,13 @@ case "$r" in fi ;; esac +# The host setup of the Pyo running now, once per version: an update from +# the dashboard brings new scripts and timers here too. Last, since it +# rewrites this script. +units=$(curl -fsS -m 30 http://127.0.0.1:8000/install/host-units.sh) || exit 0 +sum=$(sha256sum <<<"$units" | cut -d' ' -f1) +[ "$sum" != "$(cat /etc/pyo/host-units 2>/dev/null)" ] || exit 0 +exec bash -c "$units"$'\n'"echo $sum > /etc/pyo/host-units" SH "${sudo[@]}" tee /usr/local/lib/pyo/pyo-reset-login >/dev/null <<'SH' #!/usr/bin/env bash From 2072914bdbdca7dc50acc0d6af204cb568941dbb Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 16:49:25 +0700 Subject: [PATCH 02/86] fix(gateway): keep the proxies up while they update The proxies rolled old before new with maxUnavailable 1, so a server had no proxy until the new one was ready. Envoy's PreStop waited on the shutdown manager, which ends that wait at 10 s, past the default drain, so it failed with EOF and Envoy stopped without draining. Roll new-before-old like the WAF (Envoy has no host ports, so two fit on one server) and drain within 6 s. --- crates/pyo-proto/src/system.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/crates/pyo-proto/src/system.rs b/crates/pyo-proto/src/system.rs index 3ff540e..496e16f 100644 --- a/crates/pyo-proto/src/system.rs +++ b/crates/pyo-proto/src/system.rs @@ -654,6 +654,10 @@ pub fn envoy_proxy(geoip_image: &str, public: bool) -> Value { "type": "Kubernetes", "kubernetes": { "envoyDaemonSet": { + // New before old, like the WAF: a server's new proxy + // is ready before its old one stops. Envoy has no + // host ports, so two fit on one server. + "strategy": { "type": "RollingUpdate", "rollingUpdate": { "maxSurge": 1, "maxUnavailable": 0 } }, "pod": { "affinity": gateway_affinity(), "tolerations": [{ "key": crate::GPU_RESOURCE, "operator": "Exists", "effect": "NoSchedule" }], @@ -674,6 +678,10 @@ pub fn envoy_proxy(geoip_image: &str, public: bool) -> Value { "envoyService": envoy_service(public), }, }, + // A stopping proxy drains its connections, then exits. Envoy's + // PreStop waits for the shutdown manager, which closes that wait + // at 10 s (the EOF it failed with), so the drain ends before. + "shutdown": { "drainTimeout": "6s", "minDrainDuration": "3s" }, // The firewall's country rules place visitors with this. "geoIP": { "provider": { "type": "MaxMind", "maxMind": { "countryDbSource": { "local": { "path": GEOIP_PATH } } } } }, "telemetry": { "accessLog": { "settings": [{ @@ -1481,6 +1489,22 @@ mod tests { } } + /// An update never leaves a server without a proxy: the new one starts + /// first, and the old one drains within the shutdown manager's 10 s. + #[test] + fn proxies_update_new_before_old() { + let p = envoy_proxy("g", true); + let ds = &p["spec"]["provider"]["kubernetes"]["envoyDaemonSet"]; + assert_eq!(ds["strategy"]["rollingUpdate"]["maxSurge"], 1); + assert_eq!(ds["strategy"]["rollingUpdate"]["maxUnavailable"], 0); + let secs = |v: &serde_json::Value| -> u64 { + v.as_str().unwrap().trim_end_matches('s').parse().unwrap() + }; + let shutdown = &p["spec"]["shutdown"]; + assert!(secs(&shutdown["minDrainDuration"]) < secs(&shutdown["drainTimeout"])); + assert!(secs(&shutdown["drainTimeout"]) + 2 < 10); + } + /// The tailnet listeners: their own ports, tailnet sources only. Public /// clients reach the same proxies, so the policy on the Gateway refuses /// them, whatever Host or SNI they send: it matches on the client's From e52da87fca31a5463ca8c0eef439cc4076b48801 Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 16:50:02 +0700 Subject: [PATCH 03/86] feat(cli): add pyo version --- crates/pyo-cli/src/main.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/pyo-cli/src/main.rs b/crates/pyo-cli/src/main.rs index 5ff6c2a..5b7d495 100644 --- a/crates/pyo-cli/src/main.rs +++ b/crates/pyo-cli/src/main.rs @@ -262,6 +262,9 @@ enum Command { #[command(subcommand)] command: ServerCommand, }, + /// The CLI's version, like --version. + #[command(disable_version_flag = true)] + Version, /// Update the CLI to the latest release. /// /// Once a day, the CLI looks for a new release and says when one is @@ -996,6 +999,10 @@ async fn run(mut cli: Cli) -> anyhow::Result<()> { Command::Sandbox { command, team } => { sandbox::run(&config::client(url, token)?, team.as_deref(), command).await } + Command::Version => { + println!("pyo {}", env!("CARGO_PKG_VERSION")); + Ok(()) + } Command::Upgrade { version } => upgrade::run(version).await, Command::Hetzner { command, team } => { let api = config::client(url, token)?; From 5aa3245b4628702e8ce681c3617b3f0c4d5b4fa0 Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 16:54:15 +0700 Subject: [PATCH 04/86] fix(api): answer a missing query parameter in plain words Axum's Query rejection reached callers as raw text like "Failed to deserialize query string: missing field `team`". A Query extractor in error.rs turns it into a JSON 400 like "Add the team parameter.", and every handler uses it. --- crates/pyo-api/src/agent.rs | 3 +- crates/pyo-api/src/auth.rs | 2 +- crates/pyo-api/src/builders.rs | 3 +- crates/pyo-api/src/cloud/bootstrap.rs | 3 +- crates/pyo-api/src/cloud/mod.rs | 6 +- crates/pyo-api/src/cloudflare.rs | 7 +- crates/pyo-api/src/cronruns.rs | 6 +- crates/pyo-api/src/database_analytics.rs | 3 +- crates/pyo-api/src/database_imports.rs | 3 +- crates/pyo-api/src/database_logs.rs | 3 +- crates/pyo-api/src/databases.rs | 3 +- crates/pyo-api/src/error.rs | 80 +++++++++++++++++++++++ crates/pyo-api/src/git/mod.rs | 3 +- crates/pyo-api/src/github.rs | 3 +- crates/pyo-api/src/images.rs | 6 +- crates/pyo-api/src/install.rs | 7 +- crates/pyo-api/src/mcp.rs | 3 +- crates/pyo-api/src/oauth.rs | 3 +- crates/pyo-api/src/ops.rs | 12 ++-- crates/pyo-api/src/platform_import/mod.rs | 3 +- crates/pyo-api/src/recovery.rs | 3 +- crates/pyo-api/src/sandbox/api.rs | 3 +- crates/pyo-api/src/sandbox/join.rs | 3 +- crates/pyo-api/src/terminal.rs | 3 +- 24 files changed, 129 insertions(+), 45 deletions(-) diff --git a/crates/pyo-api/src/agent.rs b/crates/pyo-api/src/agent.rs index 7db49da..93d978e 100644 --- a/crates/pyo-api/src/agent.rs +++ b/crates/pyo-api/src/agent.rs @@ -1,11 +1,12 @@ //! Endpoints the in-cluster agent dials out to: heartbeats with what it sees, //! a long-poll for commands, and reports on commands and rollouts. +use crate::error::Query; use std::time::Duration; use axum::{ Json, Router, - extract::{FromRequestParts, Path, Query, State}, + extract::{FromRequestParts, Path, State}, http::{StatusCode, request::Parts}, routing::{get, post}, }; diff --git a/crates/pyo-api/src/auth.rs b/crates/pyo-api/src/auth.rs index dbea0ed..d45afd7 100644 --- a/crates/pyo-api/src/auth.rs +++ b/crates/pyo-api/src/auth.rs @@ -606,7 +606,7 @@ struct ConfigQuery { async fn config( State(state): State, - axum::extract::Query(q): axum::extract::Query, + crate::error::Query(q): crate::error::Query, ) -> ApiResult> { let mut conn = state.db.acquire().await?; let invite = match q.invite.as_deref() { diff --git a/crates/pyo-api/src/builders.rs b/crates/pyo-api/src/builders.rs index 42c8882..6b3c424 100644 --- a/crates/pyo-api/src/builders.rs +++ b/crates/pyo-api/src/builders.rs @@ -5,11 +5,12 @@ //! build-only server that joined it. It dials out here with its own token: heartbeats, a long-poll for build jobs, build logs and the //! result. See deploy.rs for what happens to a finished build. +use crate::error::Query; use std::time::Duration; use axum::{ Json, Router, - extract::{FromRequestParts, Path, Query, State}, + extract::{FromRequestParts, Path, State}, http::{StatusCode, request::Parts}, routing::{get, post}, }; diff --git a/crates/pyo-api/src/cloud/bootstrap.rs b/crates/pyo-api/src/cloud/bootstrap.rs index fe83a6f..2677233 100644 --- a/crates/pyo-api/src/cloud/bootstrap.rs +++ b/crates/pyo-api/src/cloud/bootstrap.rs @@ -21,13 +21,14 @@ //! //! Wrong codes are rate-limited. The server then shows as Joining. +use crate::error::Query; use std::net::{IpAddr, SocketAddr}; use std::time::Duration; use axum::{ Router, body::Bytes, - extract::{ConnectInfo, Query, State}, + extract::{ConnectInfo, State}, http::{StatusCode, header}, response::{IntoResponse, Response}, routing::post, diff --git a/crates/pyo-api/src/cloud/mod.rs b/crates/pyo-api/src/cloud/mod.rs index 0c56248..fe8b021 100644 --- a/crates/pyo-api/src/cloud/mod.rs +++ b/crates/pyo-api/src/cloud/mod.rs @@ -28,14 +28,12 @@ pub mod bootstrap; pub mod hetzner; +use crate::error::Query; use std::collections::BTreeMap; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; -use axum::{ - Json, - extract::{Query, State}, -}; +use axum::{Json, extract::State}; use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; use sqlx::PgPool; diff --git a/crates/pyo-api/src/cloudflare.rs b/crates/pyo-api/src/cloudflare.rs index e18b80a..40da52f 100644 --- a/crates/pyo-api/src/cloudflare.rs +++ b/crates/pyo-api/src/cloudflare.rs @@ -20,13 +20,10 @@ //! - A proxied record suggests the service's "Behind Cloudflare" firewall //! switch (the dashboard's DNS record line), until it's on. +use crate::error::Query; use std::{collections::HashMap, time::Duration}; -use axum::{ - Json, - extract::{Query, State}, - http::StatusCode, -}; +use axum::{Json, extract::State, http::StatusCode}; use reqwest::{Method, Url}; use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; diff --git a/crates/pyo-api/src/cronruns.rs b/crates/pyo-api/src/cronruns.rs index c86355c..855779b 100644 --- a/crates/pyo-api/src/cronruns.rs +++ b/crates/pyo-api/src/cronruns.rs @@ -2,10 +2,8 @@ //! Kubernetes holds on to. A run that fails says so in the activity feed, //! once, which is what notifications follow. -use axum::{ - Json, - extract::{Query, State}, -}; +use crate::error::Query; +use axum::{Json, extract::State}; use pyo_proto::CronRunReport; use serde::Deserialize; use serde_json::{Value, json}; diff --git a/crates/pyo-api/src/database_analytics.rs b/crates/pyo-api/src/database_analytics.rs index b14864f..f0d393f 100644 --- a/crates/pyo-api/src/database_analytics.rs +++ b/crates/pyo-api/src/database_analytics.rs @@ -6,11 +6,12 @@ //! they become series for the dashboard. Team members can read it, like //! service metrics, and it's an MCP tool like them. +use crate::error::Query; use std::collections::BTreeMap; use axum::{ Json, - extract::{Path, Query, State}, + extract::{Path, State}, }; use serde::Deserialize; use serde_json::{Value, json}; diff --git a/crates/pyo-api/src/database_imports.rs b/crates/pyo-api/src/database_imports.rs index 978d4d6..0f30ea0 100644 --- a/crates/pyo-api/src/database_imports.rs +++ b/crates/pyo-api/src/database_imports.rs @@ -31,12 +31,13 @@ //! unless an import is using it; an upload never sent, once its URL //! expired. +use crate::error::Query; use std::time::Duration; use axum::{ Json, Router, body::Body, - extract::{DefaultBodyLimit, Path, Query, State}, + extract::{DefaultBodyLimit, Path, State}, http::{HeaderMap, StatusCode}, response::Response, routing::{get, post, put}, diff --git a/crates/pyo-api/src/database_logs.rs b/crates/pyo-api/src/database_logs.rs index 7c095b6..03c50e2 100644 --- a/crates/pyo-api/src/database_logs.rs +++ b/crates/pyo-api/src/database_logs.rs @@ -6,11 +6,12 @@ //! `reported`) and the team's activity on it. Team members only, like //! service logs; not an MCP tool for now. +use crate::error::Query; use std::time::Duration; use axum::{ Json, - extract::{Path, Query, State}, + extract::{Path, State}, }; use pyo_proto::{Command, CommandResult, now_ms}; use serde::Deserialize; diff --git a/crates/pyo-api/src/databases.rs b/crates/pyo-api/src/databases.rs index 3a6fd47..64dc70b 100644 --- a/crates/pyo-api/src/databases.rs +++ b/crates/pyo-api/src/databases.rs @@ -16,9 +16,10 @@ //! `revealDatabasePassword` (owners and admins). //! - **Status:** the controller's, as each heartbeat reports it. +use crate::error::Query; use axum::{ Json, - extract::{Path, Query, State}, + extract::{Path, State}, }; use pyo_proto::{Command, DatabaseReport, DatabaseSpec, EnvVar, NamedSecret}; use serde::Deserialize; diff --git a/crates/pyo-api/src/error.rs b/crates/pyo-api/src/error.rs index 241ebb0..2733668 100644 --- a/crates/pyo-api/src/error.rs +++ b/crates/pyo-api/src/error.rs @@ -114,3 +114,83 @@ impl From for ApiError { pub async fn not_found() -> ApiError { ApiError::new(StatusCode::NOT_FOUND, "No such endpoint.") } + +/// Axum's `Query`, with a missing or wrong parameter as a plain 400 the +/// dashboard can show, instead of axum's own text. +#[derive(Debug, Clone, Copy, Default)] +pub struct Query(pub T); + +impl axum::extract::FromRequestParts for Query +where + T: serde::de::DeserializeOwned, + S: Send + Sync, +{ + type Rejection = ApiError; + + async fn from_request_parts( + parts: &mut axum::http::request::Parts, + state: &S, + ) -> Result { + match axum::extract::Query::::from_request_parts(parts, state).await { + Ok(axum::extract::Query(v)) => Ok(Query(v)), + Err(e) => Err(ApiError::bad_request(query_message(&e.body_text()))), + } + } +} + +/// axum's "Failed to deserialize query string: missing field `team`", in +/// plain words. +fn query_message(text: &str) -> String { + let quoted = |after: &str| { + let rest = &text[text.find(after)? + after.len()..]; + rest.split('`').next().map(str::to_owned) + }; + if let Some(name) = quoted("missing field `") { + return format!("Add the {name} parameter."); + } + if let Some(value) = quoted("unknown variant `") { + return format!("\"{value}\" isn't a value this parameter takes."); + } + "A parameter in the address isn't valid.".to_owned() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn query_errors_in_plain_words() { + assert_eq!( + query_message("Failed to deserialize query string: missing field `team`"), + "Add the team parameter." + ); + assert_eq!( + query_message( + "Failed to deserialize query string: unknown variant `x`, expected `a` or `b`" + ), + "\"x\" isn't a value this parameter takes." + ); + assert_eq!( + query_message("Failed to deserialize query string: invalid digit found in string"), + "A parameter in the address isn't valid." + ); + } + + #[tokio::test] + async fn a_missing_parameter_is_a_plain_400() { + #[derive(serde::Deserialize)] + #[allow(dead_code)] + struct Q { + team: String, + } + let req = axum::http::Request::get("/x?other=1").body(()).unwrap(); + let (mut parts, ()) = req.into_parts(); + let e = + as axum::extract::FromRequestParts<()>>::from_request_parts(&mut parts, &()) + .await + .err() + .unwrap(); + assert_eq!(e.status, StatusCode::BAD_REQUEST); + assert_eq!(e.message, "Add the team parameter."); + } +} diff --git a/crates/pyo-api/src/git/mod.rs b/crates/pyo-api/src/git/mod.rs index 7688ff5..3422025 100644 --- a/crates/pyo-api/src/git/mod.rs +++ b/crates/pyo-api/src/git/mod.rs @@ -30,12 +30,13 @@ pub mod gitlab; pub mod remote; +use crate::error::Query; use std::time::Duration; use axum::{ Json, Router, body::Bytes, - extract::{Path, Query, State}, + extract::{Path, State}, http::{HeaderMap, StatusCode}, response::IntoResponse, routing::{get, post}, diff --git a/crates/pyo-api/src/github.rs b/crates/pyo-api/src/github.rs index ed8ccd7..fc41928 100644 --- a/crates/pyo-api/src/github.rs +++ b/crates/pyo-api/src/github.rs @@ -12,6 +12,7 @@ //! - Each deploy reports a commit status on GitHub (pending, success, //! failure) linking to the deployment. +use crate::error::Query; use std::{ collections::HashMap, sync::OnceLock, @@ -21,7 +22,7 @@ use std::{ use axum::{ Json, Router, body::Bytes, - extract::{Query, State}, + extract::State, http::{HeaderMap, StatusCode}, response::{IntoResponse, Redirect}, routing::{get, post}, diff --git a/crates/pyo-api/src/images.rs b/crates/pyo-api/src/images.rs index 9458096..09b4fc7 100644 --- a/crates/pyo-api/src/images.rs +++ b/crates/pyo-api/src/images.rs @@ -3,12 +3,10 @@ //! them. Images Pyo built live in its registry, which also gives their size. use crate::error::ApiResult; +use crate::error::Query; use crate::ops::{Role, require_role}; use crate::{AppState, auth::SessionUser, deploy}; -use axum::{ - Json, - extract::{Query, State}, -}; +use axum::{Json, extract::State}; use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; use std::collections::BTreeMap; diff --git a/crates/pyo-api/src/install.rs b/crates/pyo-api/src/install.rs index e543bbe..4bdd342 100644 --- a/crates/pyo-api/src/install.rs +++ b/crates/pyo-api/src/install.rs @@ -3,11 +3,8 @@ //! The token is the cluster's agent credential; it's only valid for the //! cluster it was issued to and can be rotated from the dashboard. -use axum::{ - extract::{Query, State}, - http::header, - response::IntoResponse, -}; +use crate::error::Query; +use axum::{extract::State, http::header, response::IntoResponse}; use serde::Deserialize; use crate::{ diff --git a/crates/pyo-api/src/mcp.rs b/crates/pyo-api/src/mcp.rs index dba3850..7c90271 100644 --- a/crates/pyo-api/src/mcp.rs +++ b/crates/pyo-api/src/mcp.rs @@ -13,6 +13,7 @@ //! same handlers and `/ops` code with the same checks. Only the tools the //! connection's access allows are listed. +use crate::error::Query; use axum::{ Json, Router, extract::State, @@ -595,7 +596,7 @@ fn json_of(r: ApiResult>) -> Result /// An endpoint's tool: the endpoint's own handler, as this user. async fn run_endpoint(s: &AppState, user: &User, id: &str, args: &Value) -> Result { - use axum::extract::{Path, Query}; + use axum::extract::Path; let st = || State(s.clone()); let me = || SessionUser(user.clone()); let path_id = || arg(args, "id").map(|i| Path(i.to_owned())); diff --git a/crates/pyo-api/src/oauth.rs b/crates/pyo-api/src/oauth.rs index 5956861..cf0c9f1 100644 --- a/crates/pyo-api/src/oauth.rs +++ b/crates/pyo-api/src/oauth.rs @@ -11,9 +11,10 @@ //! //! API keys (Profile → API keys) stay the way scripts call the REST API. +use crate::error::Query; use axum::{ Form, Json, Router, - extract::{FromRequestParts, Query, State}, + extract::{FromRequestParts, State}, http::{HeaderMap, StatusCode, header, request::Parts}, response::{IntoResponse, Response}, routing::{get, post}, diff --git a/crates/pyo-api/src/ops.rs b/crates/pyo-api/src/ops.rs index 1364901..30cfc97 100644 --- a/crates/pyo-api/src/ops.rs +++ b/crates/pyo-api/src/ops.rs @@ -7924,7 +7924,7 @@ pub(crate) async fn observability_settings( pub(crate) async fn metrics( State(s): State, SessionUser(user): SessionUser, - axum::extract::Query(q): axum::extract::Query, + crate::error::Query(q): crate::error::Query, ) -> ApiResult> { let mut tx = s.db.begin().await?; require_role(&mut tx, &user, &q.team, Role::Viewer).await?; @@ -7994,7 +7994,7 @@ pub(crate) struct AccessLogQuery { pub(crate) async fn access_logs( State(s): State, SessionUser(user): SessionUser, - axum::extract::Query(q): axum::extract::Query, + crate::error::Query(q): crate::error::Query, ) -> ApiResult> { let hosts = team_hosts( &s, @@ -8070,7 +8070,7 @@ async fn team_hosts( pub(crate) async fn observability( State(s): State, SessionUser(user): SessionUser, - axum::extract::Query(q): axum::extract::Query, + crate::error::Query(q): crate::error::Query, ) -> ApiResult> { let hosts = team_hosts(&s, &user, &q.team, q.project, q.site, q.service).await?; let range = match q.range.as_deref() { @@ -8107,7 +8107,7 @@ pub(crate) async fn connections( State(s): State, SessionUser(user): SessionUser, axum::extract::Path(id): axum::extract::Path, - axum::extract::Query(q): axum::extract::Query, + crate::error::Query(q): crate::error::Query, ) -> ApiResult> { let mut tx = s.db.begin().await?; let team_id = team_of(&mut tx, "services", &id).await?; @@ -8150,7 +8150,7 @@ pub(crate) struct BlockedQuery { pub(crate) async fn firewall_blocked( State(s): State, SessionUser(user): SessionUser, - axum::extract::Query(q): axum::extract::Query, + crate::error::Query(q): crate::error::Query, ) -> ApiResult> { let mut tx = s.db.begin().await?; let team_id = team_of(&mut tx, "services", &q.service).await?; @@ -8185,7 +8185,7 @@ pub(crate) async fn firewall_blocked( pub(crate) async fn waf_hits( State(s): State, SessionUser(user): SessionUser, - axum::extract::Query(q): axum::extract::Query, + crate::error::Query(q): crate::error::Query, ) -> ApiResult> { let mut tx = s.db.begin().await?; let team_id = team_of(&mut tx, "services", &q.service).await?; diff --git a/crates/pyo-api/src/platform_import/mod.rs b/crates/pyo-api/src/platform_import/mod.rs index 6274c52..e1bc445 100644 --- a/crates/pyo-api/src/platform_import/mod.rs +++ b/crates/pyo-api/src/platform_import/mod.rs @@ -35,12 +35,13 @@ mod links; mod railway; mod vercel; +use crate::error::Query; use std::collections::{BTreeMap, HashSet}; use std::time::Duration; use axum::{ Json, Router, - extract::{Path, Query, State}, + extract::{Path, State}, http::StatusCode, routing::{get, post}, }; diff --git a/crates/pyo-api/src/recovery.rs b/crates/pyo-api/src/recovery.rs index c66ec37..660e88f 100644 --- a/crates/pyo-api/src/recovery.rs +++ b/crates/pyo-api/src/recovery.rs @@ -17,9 +17,10 @@ //! Using it sets the new password, ends the account's sessions and signs //! this browser in. +use crate::error::Query; use axum::{ Json, Router, - extract::{Query, State}, + extract::State, http::{HeaderMap, StatusCode}, routing::{get, post}, }; diff --git a/crates/pyo-api/src/sandbox/api.rs b/crates/pyo-api/src/sandbox/api.rs index c909421..7f117bb 100644 --- a/crates/pyo-api/src/sandbox/api.rs +++ b/crates/pyo-api/src/sandbox/api.rs @@ -3,9 +3,10 @@ //! talk to the sandbox server with the connection's token: exec, terminals, //! files and ports never go through Pyo. +use crate::error::Query; use axum::{ Json, Router, - extract::{Path, Query, State}, + extract::{Path, State}, http::StatusCode, routing::{get, post}, }; diff --git a/crates/pyo-api/src/sandbox/join.rs b/crates/pyo-api/src/sandbox/join.rs index 95aeb2e..e1ae203 100644 --- a/crates/pyo-api/src/sandbox/join.rs +++ b/crates/pyo-api/src/sandbox/join.rs @@ -18,12 +18,13 @@ //! A server that can run neither Firecracker nor gVisor is refused, with //! the detection's reason. +use crate::error::Query; use std::net::{IpAddr, SocketAddr}; use std::time::Duration; use axum::{ Router, - extract::{ConnectInfo, Query, State}, + extract::{ConnectInfo, State}, http::{StatusCode, header}, response::{IntoResponse, Response}, routing::post, diff --git a/crates/pyo-api/src/terminal.rs b/crates/pyo-api/src/terminal.rs index a4a901b..bfa4517 100644 --- a/crates/pyo-api/src/terminal.rs +++ b/crates/pyo-api/src/terminal.rs @@ -6,12 +6,13 @@ //! on another one, which forwards its socket to the replica holding the //! browser (`relay`, its pod address). +use crate::error::Query; use std::{collections::HashMap, sync::Arc, time::Duration}; use axum::{ Router, extract::{ - Path, Query, State, + Path, State, ws::{Message, WebSocket, WebSocketUpgrade}, }, http::{HeaderMap, header}, From a70dd1a94d10846ce0f84d37f3e0eba5c348dbdb Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 16:54:31 +0700 Subject: [PATCH 05/86] docs(api): list sandboxes among an API key's access levels --- crates/pyo-api/src/ops.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/pyo-api/src/ops.rs b/crates/pyo-api/src/ops.rs index 30cfc97..f553305 100644 --- a/crates/pyo-api/src/ops.rs +++ b/crates/pyo-api/src/ops.rs @@ -2076,7 +2076,7 @@ pub(crate) enum Op { CreateApiToken { id: String, name: String, - /// "read", "deploy" or "full" (the default). + /// "read", "deploy", "sandboxes" or "full" (the default). access: Option, /// Only these teams; all of yours when omitted. team_ids: Option>, From 23232033f55340abaa2d11f76b03f3fc48d7c29e Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 16:55:40 +0700 Subject: [PATCH 06/86] fix(dash): show no New button to sandbox viewers Add server is for owners and admins, and a viewer got an empty New menu. The menu now only shows when it has items. --- .../src/routes/_app/$team/sandboxes/index.tsx | 34 +++++++------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/dash/src/routes/_app/$team/sandboxes/index.tsx b/dash/src/routes/_app/$team/sandboxes/index.tsx index 28b9483..4167fca 100644 --- a/dash/src/routes/_app/$team/sandboxes/index.tsx +++ b/dash/src/routes/_app/$team/sandboxes/index.tsx @@ -86,25 +86,22 @@ function SandboxesPage() { : x.state === filter) && (!template || x.template === template), ); - const newMenu = ( - , href: "/sandboxes/new" }] - : []), - ...(canManage - ? [{ label: "Add server", icon: , href: "/sandboxes/servers/new" }] - : []), - ]} - /> - ); + // Add server is for owners and admins. A viewer gets no New button. + const newItems = [ + ...(canUse + ? [{ label: "New sandbox", icon: , href: "/sandboxes/new" }] + : []), + ...(canManage + ? [{ label: "Add server", icon: , href: "/sandboxes/servers/new" }] + : []), + ]; + const newMenu = newItems.length ? : undefined; // Nothing yet: the same empty state as the databases'. if (!servers.length && (q.status !== "success" || !all.length)) return ( - + - - {newMenu} - - } - /> + {pending.length ? : null} {q.status === "success" ? ( Date: Mon, 16 Nov 2026 16:56:52 +0700 Subject: [PATCH 07/86] fix(sandbox): pull templates named by the registry's local address A template image like 127.0.0.1:5005/team/x or localhost:5005/team/x is in Pyo's registry, but the server tried its own loopback. Every name of Pyo's registry now goes to the server as the tailnet door. --- crates/pyo-api/src/sandbox/templates.rs | 31 +++++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/crates/pyo-api/src/sandbox/templates.rs b/crates/pyo-api/src/sandbox/templates.rs index 561f4d1..18f0649 100644 --- a/crates/pyo-api/src/sandbox/templates.rs +++ b/crates/pyo-api/src/sandbox/templates.rs @@ -96,12 +96,22 @@ pub fn eligible(server_team: Option<&str>, template_team: Option<&str>) -> bool } /// The repository path of an image in Pyo's registry, by any of the -/// addresses it's known by. None for any other registry. -pub fn in_pyos_registry<'a>(image: &'a str, doors: &[String]) -> Option<&'a str> { - doors +/// addresses it's known by: its doors, and its names on Pyo's own machine +/// (`127.0.0.1:5005`, `localhost:5005`, in the cluster). None for any other +/// registry. +pub fn in_pyos_registry(image: &str, doors: &[String]) -> Option { + if let Some(path) = doors .iter() .filter(|d| !d.is_empty()) .find_map(|d| image.strip_prefix(d.as_str())?.strip_prefix('/')) + { + return Some(path.to_owned()); + } + let pulled = crate::deploy::pyo_registry_image(image)?.ok()?; + pulled + .strip_prefix(crate::deploy::registry().pull.as_str())? + .strip_prefix('/') + .map(str::to_owned) } /// The command that has a server prepare `t`. @@ -579,14 +589,25 @@ mod tests { "registry.tail1.ts.net".to_string(), "100.64.0.1:5006".to_string(), ]; + let path = |p: &str| Some(p.to_owned()); assert_eq!( in_pyos_registry("registry.tail1.ts.net/acme/app/api:1", &doors), - Some("acme/app/api:1") + path("acme/app/api:1") ); assert_eq!( in_pyos_registry("100.64.0.1:5006/acme/app/api@sha256:abc", &doors), - Some("acme/app/api@sha256:abc") + path("acme/app/api@sha256:abc") + ); + // Its names on Pyo's own machine, which a server can't reach. + assert_eq!( + in_pyos_registry("127.0.0.1:5005/frans/sandbox-base:1", &doors), + path("frans/sandbox-base:1") + ); + assert_eq!( + in_pyos_registry("localhost:5005/frans/sandbox-base", &doors), + path("frans/sandbox-base") ); + assert_eq!(in_pyos_registry("localhost:8000/x", &doors), None); assert_eq!(in_pyos_registry("python:3.13", &doors), None); assert_eq!( in_pyos_registry("registry.tail1.ts.net.evil.com/x", &doors), From ece6bf7c9fcca7849aa17589dd70bf09c8948fa5 Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 16:58:13 +0700 Subject: [PATCH 08/86] fix(registry): say how to push when an image isn't the team's The door's 403 only said "Not one of this team's images." It now names the path to push to and points to the Registry page. --- crates/pyo-api/src/registrydoor.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/pyo-api/src/registrydoor.rs b/crates/pyo-api/src/registrydoor.rs index 1203469..d564f9f 100644 --- a/crates/pyo-api/src/registrydoor.rs +++ b/crates/pyo-api/src/registrydoor.rs @@ -179,6 +179,13 @@ pub(crate) fn machine_allowed( read && parts(name).is_some_and(|(t, _, _)| t == slug) } +/// The door's 403 for an image that isn't the team's: what to do instead. +pub(crate) fn not_theirs(slug: &str) -> String { + format!( + "Not one of this team's images. Push it as {slug}// first. The Registry page shows how." + ) +} + /// What a request asks for: the repository it names (None for /v2/ itself) /// and whether it writes. #[derive(Debug, PartialEq)] @@ -456,11 +463,7 @@ async fn machine_door( &slug, service_team.as_deref() == Some(team_id.as_str()), ) { - return errors( - StatusCode::FORBIDDEN, - "DENIED", - "Not one of this team's images.", - ); + return errors(StatusCode::FORBIDDEN, "DENIED", ¬_theirs(&slug)); } // A team's own repository: only one it pushed first. if self::parts(&name).is_some() { From b9016e265f82b54d5022b7e4d6b55dfc9763dfd7 Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 16:59:53 +0700 Subject: [PATCH 09/86] fix(sandbox): pass the base image so 0.4.0 can update Pyo 0.4.0 checks every *_IMAGE env value in a new release's manifests against the images it knows and refused 0.4.1, whose worker got PYO_SANDBOX_BASE_IMAGE. The variable is now PYO_SANDBOX_BASE_REF, the release check makes sure it holds the release's sandboxBase image, and a test runs 0.4.0's rule against the manifests. --- crates/pyo-api/src/sandbox/base.rs | 14 ++-- crates/pyo-proto/src/lib.rs | 111 ++++++++++++++++++++++++++++- deploy/k8s/pyo.yaml | 7 +- scripts/release-manifest.ts | 2 +- 4 files changed, 123 insertions(+), 11 deletions(-) diff --git a/crates/pyo-api/src/sandbox/base.rs b/crates/pyo-api/src/sandbox/base.rs index 70c39fc..d817f32 100644 --- a/crates/pyo-api/src/sandbox/base.rs +++ b/crates/pyo-api/src/sandbox/base.rs @@ -5,7 +5,7 @@ //! //! - Seeding: when a worker starts, and in every sandbox round, Pyo makes //! sure `base` exists and points at its release's image -//! (`PYO_SANDBOX_BASE_IMAGE`, pinned by digest in release.json). It's +//! (`PYO_SANDBOX_BASE_REF`, pinned by digest in release.json). It's //! idempotent. //! - On update: a new release has a new image, so `base` gets a new //! revision (the digest's start). Servers make it beside the old one in @@ -37,10 +37,10 @@ pub struct Wanted { pub release: String, } -/// This release's image, from `PYO_SANDBOX_BASE_IMAGE`. None for a build +/// This release's image, from `PYO_SANDBOX_BASE_REF`. None for a build /// that isn't a release (scripts/pyo-up.sh leaves it empty). pub fn from_env() -> Option { - let image = std::env::var("PYO_SANDBOX_BASE_IMAGE").ok()?; + let image = std::env::var(pyo_proto::SANDBOX_BASE_ENV).ok()?; let image = image.trim(); pinned_image(image).then(|| Wanted { image: image.to_owned(), @@ -241,18 +241,18 @@ mod tests { #[test] fn only_a_pinned_image_from_the_env_counts() { // SAFETY: tests in this module don't read it concurrently. - unsafe { std::env::set_var("PYO_SANDBOX_BASE_IMAGE", "") }; + unsafe { std::env::set_var("PYO_SANDBOX_BASE_REF", "") }; assert_eq!(from_env(), None, "a dev build (pyo-up.sh)"); unsafe { std::env::set_var( - "PYO_SANDBOX_BASE_IMAGE", + "PYO_SANDBOX_BASE_REF", "127.0.0.1:5005/pyo/sandbox-base:dev", ) }; assert_eq!(from_env(), None, "not pinned"); - unsafe { std::env::set_var("PYO_SANDBOX_BASE_IMAGE", A) }; + unsafe { std::env::set_var("PYO_SANDBOX_BASE_REF", A) }; assert_eq!(from_env(), Some(want(A, crate::updates::current()))); - unsafe { std::env::remove_var("PYO_SANDBOX_BASE_IMAGE") }; + unsafe { std::env::remove_var("PYO_SANDBOX_BASE_REF") }; assert_eq!(from_env(), None); } diff --git a/crates/pyo-proto/src/lib.rs b/crates/pyo-proto/src/lib.rs index a67436e..0f9a069 100644 --- a/crates/pyo-proto/src/lib.rs +++ b/crates/pyo-proto/src/lib.rs @@ -1264,6 +1264,15 @@ impl Release { )); } } + let mut refs = vec![]; + collect_env(o, SANDBOX_BASE_ENV, &mut refs); + for r in refs { + if !r.is_empty() && r != self.images.sandbox_base { + out.push(format!( + "{SANDBOX_BASE_ENV} is {r}, not the release's sandboxBase image." + )); + } + } } if let Some(m) = &self.min_from && parse_version(m).is_none() @@ -1296,8 +1305,35 @@ pub fn image_repo(image: &str) -> &str { } } +/// The env var Pyo's worker reads the sandbox template `base`'s image from +/// (pinned to the release's `sandboxBase`). +pub const SANDBOX_BASE_ENV: &str = "PYO_SANDBOX_BASE_REF"; + +/// The values of env vars named `name` in a Kubernetes object. +pub fn collect_env(v: &serde_json::Value, name: &str, out: &mut Vec) { + match v { + serde_json::Value::Object(m) => { + if m.get("name").and_then(|n| n.as_str()) == Some(name) + && let Some(serde_json::Value::String(value)) = m.get("value") + { + out.push(value.clone()); + } + m.values().for_each(|x| collect_env(x, name, out)); + } + serde_json::Value::Array(a) => a.iter().for_each(|x| collect_env(x, name, out)), + _ => {} + } +} + /// Every `image` value in a Kubernetes object, and image references in its /// containers' env (Pyo passes its agent and builder images that way). +/// +/// Older Pyos run this same rule on a new release before they update to +/// it, with only the images they knew. So an env var whose name ends in +/// `_IMAGE` may only hold one of the images every supported Pyo knows +/// (the test `manifests_pass_older_pyos_checks` keeps this). A new image +/// goes in a var named otherwise, like [`SANDBOX_BASE_ENV`], and +/// [`Release::problems`] checks it by name. pub fn collect_images(v: &serde_json::Value, out: &mut Vec) { match v { serde_json::Value::Object(m) => { @@ -2578,7 +2614,7 @@ mod release_tests { pyo: vec![ json!({ "kind": "Deployment", "spec": { "template": { "spec": { "containers": [{ "name": "pyo", "image": img("pyo"), - "env": [{ "name": "PYO_AGENT_IMAGE", "value": img("agent") }, { "name": "PYO_BUILDER_IMAGE", "value": img("builder") }], + "env": [{ "name": "PYO_AGENT_IMAGE", "value": img("agent") }, { "name": "PYO_BUILDER_IMAGE", "value": img("builder") }, { "name": SANDBOX_BASE_ENV, "value": img("sandbox-base") }], }] } } } }), ], }, @@ -2792,6 +2828,79 @@ mod release_tests { assert!(bad.problems().iter().any(|p| p.contains("isn't one of"))); } + #[test] + fn the_sandbox_base_ref_is_the_pinned_sandbox_base() { + assert!(release().problems().is_empty()); + let mut r = release(); + r.images.sandbox_base = format!("ghcr.io/upsetdev/pyo/sandbox-base:0.2.1@sha256:{D}"); + assert!( + r.problems() + .iter() + .any(|p| p.starts_with("PYO_SANDBOX_BASE_REF is")), + "{:?}", + r.problems() + ); + // A dev build leaves it empty. + let mut r = release(); + r.manifests.pyo[0]["spec"]["template"]["spec"]["containers"][0]["env"][2]["value"] = + json!(""); + assert!(r.problems().is_empty(), "{:?}", r.problems()); + } + + /// Before updating, an older Pyo checks the new release with its own + /// rule: every `image` and every env var named `*_IMAGE` that's one of + /// Pyo's must be one of the release's images it knows. 0.4.0 knew these. + /// A new image has to go in an env var named otherwise. + #[test] + fn manifests_pass_older_pyos_checks() { + const KNOWN_TO_0_4_0: &[&str] = &[ + "pyo", + "agent", + "builder", + "flows", + "waf", + "geoip", + "db-controller", + "kv", + "static", + "mysql-operator", + "mysql-backup", + "postgres", + ]; + let docs: Vec = [ + include_str!("../../../deploy/k8s/platform.yaml"), + include_str!("../../../deploy/k8s/pyo.yaml"), + ] + .join("\n---\n") + .split("\n---") + .filter_map(|d| serde_norway::from_str::(d).ok()) + .filter(|d| d.is_object()) + .collect(); + assert!(docs.len() > 5, "the manifests parse"); + let mut images = vec![]; + for d in &docs { + collect_images(d, &mut images); + } + let ours: Vec<&String> = images + .iter() + .filter(|i| i.starts_with("__IMAGES__/")) + .collect(); + assert!(ours.len() > 5, "{images:?}"); + for i in ours { + let name = i["__IMAGES__/".len()..].split(':').next().unwrap(); + assert!( + KNOWN_TO_0_4_0.contains(&name), + "{i}: Pyo 0.4.0 would refuse a release that has it. Name its env var otherwise." + ); + } + // The base template's image is still passed, by its own name. + let mut refs = vec![]; + for d in &docs { + collect_env(d, SANDBOX_BASE_ENV, &mut refs); + } + assert_eq!(refs, vec!["__IMAGES__/sandbox-base:__TAG__".to_string()]); + } + #[test] fn min_from() { let mut r = release(); diff --git a/deploy/k8s/pyo.yaml b/deploy/k8s/pyo.yaml index dc0b426..8d7db7d 100644 --- a/deploy/k8s/pyo.yaml +++ b/deploy/k8s/pyo.yaml @@ -209,8 +209,11 @@ spec: - { name: PYO_POSTGRES_IMAGE_18, value: "__IMAGES__/postgres:18-__TAG__" } - { name: PYO_MYSQL_OPERATOR_IMAGE, value: "__IMAGES__/mysql-operator:__TAG__" } - { name: PYO_MYSQL_BACKUP_IMAGE, value: "__IMAGES__/mysql-backup:__TAG__" } - # Pyo Sandbox's built-in template `base` (sandbox/base.rs). - - { name: PYO_SANDBOX_BASE_IMAGE, value: "__IMAGES__/sandbox-base:__TAG__" } + # Pyo Sandbox's built-in template `base` (sandbox/base.rs). Not + # named *_IMAGE: Pyo 0.4.0 checks every *_IMAGE value against the + # images it knows, and refuses a release that has another + # (pyo-proto `collect_images`, and the test that keeps this rule). + - { name: PYO_SANDBOX_BASE_REF, value: "__IMAGES__/sandbox-base:__TAG__" } - { name: RUST_LOG, value: "pyo_api=info" } - { name: CLICKHOUSE_URL, value: "http://pyo-clickhouse.pyo-system.svc:8123" } - { name: CLICKHOUSE_USER, value: pyo } diff --git a/scripts/release-manifest.ts b/scripts/release-manifest.ts index 8e12b81..4f26cce 100644 --- a/scripts/release-manifest.ts +++ b/scripts/release-manifest.ts @@ -67,7 +67,7 @@ const names = { "mysql-operator": "mysqlOperator", "mysql-backup": "mysqlBackup", // Pyo Sandbox's built-in template `base` (multi-arch). Pyo reads it - // from PYO_SANDBOX_BASE_IMAGE and seeds the template from it. + // from PYO_SANDBOX_BASE_REF and seeds the template from it. "sandbox-base": "sandboxBase", } as const; const byName: Record = Object.fromEntries(Object.keys(names).map((n) => [n, ref(n)])); From eed8ae2ac63092d4cbe18e67e94042132fdddc1b Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 17:00:05 +0700 Subject: [PATCH 10/86] fix(sandbox): show template build errors in plain words Raw errors like "TLS with host: received corrupt message of type InvalidContentType" reached the dashboard. The templates' views now map the common ones (not a registry, refused login, missing image, unreachable) to plain messages. --- crates/pyo-api/src/sandbox/mod.rs | 17 +++++- crates/pyo-api/src/sandbox/templates.rs | 75 +++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/crates/pyo-api/src/sandbox/mod.rs b/crates/pyo-api/src/sandbox/mod.rs index 49b0f90..74f107d 100644 --- a/crates/pyo-api/src/sandbox/mod.rs +++ b/crates/pyo-api/src/sandbox/mod.rs @@ -360,7 +360,7 @@ pub async fn template_views( team_ids: &[String], id: Option<&str>, ) -> sqlx::Result> { - sqlx::query_scalar( + let mut rows: Vec = sqlx::query_scalar( "SELECT jsonb_build_object( 'id', t.id, 'teamId', t.team_id, 'name', t.name, 'source', t.source, 'state', t.state, 'builtin', t.builtin, 'revision', t.revision, @@ -387,7 +387,20 @@ pub async fn template_views( .bind(team_ids) .bind(id) .fetch_all(db) - .await + .await?; + // Build errors in plain words. + let plain = |e: &mut Value| { + if let Some(raw) = e.as_str() { + *e = json!(templates::plain_build_error(raw)); + } + }; + for r in &mut rows { + plain(&mut r["error"]); + for s in r["servers"].as_array_mut().into_iter().flatten() { + plain(&mut s["error"]); + } + } + Ok(rows) } // ── Create ───────────────────────────────────────────────────────────────── diff --git a/crates/pyo-api/src/sandbox/templates.rs b/crates/pyo-api/src/sandbox/templates.rs index 18f0649..273294a 100644 --- a/crates/pyo-api/src/sandbox/templates.rs +++ b/crates/pyo-api/src/sandbox/templates.rs @@ -359,6 +359,44 @@ pub fn roll_up(reports: &[TemplateReport], missing: usize) -> (&'static str, Opt ("pending", None) } +/// A server's build error, as the dashboard shows it: the common ones in +/// plain words, the others as they are. +pub fn plain_build_error(raw: &str) -> String { + let lower = raw.to_ascii_lowercase(); + let has = |s: &str| lower.contains(s); + // TLS that got something else back: a web server, not a registry. + if has("corrupt message") || has("invalidcontenttype") || has("wrong version number") { + return "That address isn't a registry.".into(); + } + if has("certificate") && has("tls with") { + return "The registry's certificate isn't valid.".into(); + } + if let Some(rest) = raw.split_once("The registry answered ").map(|(_, r)| r) { + let status = rest.split_whitespace().next().unwrap_or_default(); + // Pyo's registry door says what to do itself. + if let Some((_, door)) = rest.split_once("Not one of this team's images.") { + let door = door.trim(); + return if door.is_empty() { + "Push this image to Pyo's registry with your team's login first.".into() + } else { + format!("Not one of this team's images. {door}") + }; + } + match status { + "401" | "403" => return "The registry refused the login for this image.".into(), + "404" => return "That image isn't in the registry. Check its name and tag.".into(), + _ => {} + } + } + if has("dns error") || has("failed to lookup") || has("name or service not known") { + return "That registry's address doesn't resolve.".into(); + } + if has("connecting to") && (has("timed out") || has("refused")) { + return "The server couldn't reach that registry.".into(); + } + raw.to_owned() +} + /// Rolls every template's state up from the servers' latest reports, by /// its current revision. While a new revision is made, a template whose /// older one is ready on a server stays ready: new sandboxes start from it. @@ -616,6 +654,43 @@ mod tests { assert_eq!(in_pyos_registry("ghcr.io/o/r", &[String::new()]), None); } + #[test] + fn build_errors_in_plain_words() { + let p = plain_build_error; + assert_eq!( + p( + "TLS with asus.blue-pancake.ts.net: received corrupt message of type InvalidContentType" + ), + "That address isn't a registry." + ); + assert_eq!( + p( + "The registry answered 403 Forbidden for the manifest of frans/sandbox-base: Not one of this team's images." + ), + "Push this image to Pyo's registry with your team's login first." + ); + assert_eq!( + p( + "The registry answered 403 Forbidden for the manifest of frans/sandbox-base: Not one of this team's images. Push it as acme// first." + ), + "Not one of this team's images. Push it as acme// first." + ); + assert_eq!( + p( + "The registry answered 404 Not Found for the manifest of library/nope: manifest unknown" + ), + "That image isn't in the registry. Check its name and tag." + ); + assert_eq!( + p("connecting to 10.0.0.9:443: Connection refused (os error 111)"), + "The server couldn't reach that registry." + ); + assert_eq!( + p("The image has no linux/arm64 variant."), + "The image has no linux/arm64 variant." + ); + } + #[test] fn a_public_image_goes_as_is() { let key = SigningKey::generate("sbk_t"); From 5fa4a5ff05fe295e9a8a08a0e8735a5c84644ca1 Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 17:01:15 +0700 Subject: [PATCH 11/86] feat(dash): warn when a template's name and image look swapped An image in the name field and a bare word in the image field now get a plain warning and a Swap them button. --- dash/scripts/sandbox-templates.test.ts | 10 +++++- dash/src/lib/pyo/sandbox.ts | 2 +- dash/src/lib/pyo/sandboxTemplates.ts | 8 +++++ .../routes/_app/$team/sandboxes/templates.tsx | 34 +++++++++++++++++-- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/dash/scripts/sandbox-templates.test.ts b/dash/scripts/sandbox-templates.test.ts index ca6cb58..b16292f 100644 --- a/dash/scripts/sandbox-templates.test.ts +++ b/dash/scripts/sandbox-templates.test.ts @@ -2,7 +2,15 @@ // and that new sandboxes start on it. // bun test scripts/sandbox-templates.test.ts import { expect, test } from "bun:test"; -import { defaultTemplate, imageRepo, templateImage } from "../src/lib/pyo/sandboxTemplates"; +import { defaultTemplate, fieldsSwapped, imageRepo, templateImage } from "../src/lib/pyo/sandboxTemplates"; + +test("the new template form notices a swapped name and image", () => { + expect(fieldsSwapped("frans/sandbox-base", "sandbox-base")).toBe(true); + expect(fieldsSwapped("node:22", "node")).toBe(true); + expect(fieldsSwapped("node-22", "node:22")).toBe(false); + expect(fieldsSwapped("base", "frans/sandbox-base")).toBe(false); + expect(fieldsSwapped("node:22", "")).toBe(false); +}); const base = { name: "base", diff --git a/dash/src/lib/pyo/sandbox.ts b/dash/src/lib/pyo/sandbox.ts index 83e0ce6..90e473c 100644 --- a/dash/src/lib/pyo/sandbox.ts +++ b/dash/src/lib/pyo/sandbox.ts @@ -8,7 +8,7 @@ import { request } from "./backend"; import { BASE_TEMPLATE } from "./sandboxTemplates"; import type { CloudServerState } from "./types"; -export { BASE_TEMPLATE, defaultTemplate, imageRepo, templateImage } from "./sandboxTemplates"; +export { BASE_TEMPLATE, defaultTemplate, fieldsSwapped, imageRepo, templateImage } from "./sandboxTemplates"; // ── Types ────────────────────────────────────────────────────────────────── diff --git a/dash/src/lib/pyo/sandboxTemplates.ts b/dash/src/lib/pyo/sandboxTemplates.ts index ea9e43c..6bd1a54 100644 --- a/dash/src/lib/pyo/sandboxTemplates.ts +++ b/dash/src/lib/pyo/sandboxTemplates.ts @@ -27,6 +27,14 @@ export function templateImage(t: TemplateLike) { return t.builtin && t.source?.os ? `${t.source.os} · ${imageRepo(image)}` : image; } +/** The new template form's name and image the wrong way round: an image in + * the name (it has a "/", ":" or "@", which names can't) and a bare word in + * the image. */ +export function fieldsSwapped(name: string, image: string) { + const imageLike = (s: string) => /[/:@]/.test(s.trim()); + return imageLike(name) && !!image.trim() && !imageLike(image); +} + /** The template a new sandbox form starts on: base once it's ready, else * the first ready one. */ export function defaultTemplate(list: TemplateLike[]) { diff --git a/dash/src/routes/_app/$team/sandboxes/templates.tsx b/dash/src/routes/_app/$team/sandboxes/templates.tsx index fe53da4..8a52ab1 100644 --- a/dash/src/routes/_app/$team/sandboxes/templates.tsx +++ b/dash/src/routes/_app/$team/sandboxes/templates.tsx @@ -10,7 +10,13 @@ import { EmptyState, ErrorState, Page, PageHeader, Panel } from "~/components/ui import { Table, Td, Th, Tr } from "~/components/ui/Table"; import { megabytes, timeAgo } from "~/lib/pyo/format"; import { useQuery } from "~/lib/pyo/query"; -import { sandboxApi, templateImage, type SandboxTemplate, type SandboxTemplateServer } from "~/lib/pyo/sandbox"; +import { + fieldsSwapped, + sandboxApi, + templateImage, + type SandboxTemplate, + type SandboxTemplateServer, +} from "~/lib/pyo/sandbox"; import { roleIn, usePyo } from "~/lib/pyo/store"; import { toast } from "~/lib/pyo/toast"; import { seo } from "~/lib/seo"; @@ -61,7 +67,12 @@ function TemplatesPage() { const [error, setError] = useState(""); const [removing, setRemoving] = useState(null); const canRemove = (t: SandboxTemplate) => !t.builtin && (t.teamId ? canCreate : !!s.user.isAdmin); - const nameError = name && !NAME.test(name) ? "Lowercase letters, digits, dashes, dots and underscores." : ""; + const swapped = fieldsSwapped(name, image); + const nameError = swapped + ? "This looks like the image. Swap the name and image." + : name && !NAME.test(name) + ? "Lowercase letters, digits, dashes, dots and underscores." + : ""; const create = async () => { if (!name || !image.trim() || nameError || busy) return; @@ -164,7 +175,24 @@ function TemplatesPage() { setName(e.target.value.toLowerCase())} /> )} - + { + setName(image.trim().toLowerCase()); + setImage(name); + }} + > + Swap them + + ) : undefined + } + > {(p) => setImage(e.target.value)} />} diff --git a/dash/src/routes/_app/$team/sandboxes/templates.tsx b/dash/src/routes/_app/$team/sandboxes/templates.tsx index fe53da4..68a4b4f 100644 --- a/dash/src/routes/_app/$team/sandboxes/templates.tsx +++ b/dash/src/routes/_app/$team/sandboxes/templates.tsx @@ -1,5 +1,5 @@ import { DotsThreeIcon, SquaresFourIcon, TrashIcon } from "@phosphor-icons/react"; -import { createFileRoute } from "@tanstack/react-router"; +import { createFileRoute, useNavigate } from "@tanstack/react-router"; import { useState } from "react"; import { Dot } from "~/components/ui/Badge"; import { Button } from "~/components/ui/Button"; @@ -9,9 +9,18 @@ import { Menu } from "~/components/ui/Menu"; import { EmptyState, ErrorState, Page, PageHeader, Panel } from "~/components/ui/Panel"; import { Table, Td, Th, Tr } from "~/components/ui/Table"; import { megabytes, timeAgo } from "~/lib/pyo/format"; +import { currentSlug } from "~/lib/pyo/paths"; import { useQuery } from "~/lib/pyo/query"; -import { sandboxApi, templateImage, type SandboxTemplate, type SandboxTemplateServer } from "~/lib/pyo/sandbox"; -import { roleIn, usePyo } from "~/lib/pyo/store"; +import { + isBase, + sandboxApi, + templateImage, + templateStatus, + usable, + type SandboxTemplate, + type SandboxTemplateServer, +} from "~/lib/pyo/sandbox"; +import { roleIn, sandboxServersIn, usePyo } from "~/lib/pyo/store"; import { toast } from "~/lib/pyo/toast"; import { seo } from "~/lib/seo"; @@ -29,6 +38,7 @@ const STATE: Record sandboxApi.templates(team), { refreshMs: 10_000 }); const canCreate = roleIn(s) !== "viewer"; + // New sandboxes need a server that runs them. + const canUse = canCreate && sandboxServersIn(s).some((x) => x.state === "ready"); const [name, setName] = useState(""); const [image, setImage] = useState(""); const [busy, setBusy] = useState(false); @@ -112,7 +125,7 @@ function TemplatesPage() { - {STATE[t.state]?.label ?? t.state} + {templateStatus(t)} @@ -124,10 +137,26 @@ function TemplatesPage() { {serverLabel(x)} )) - : —} + : {isBase(t) ? "Made on first use" : "—"}} - {timeAgo(t.createdAt)} + {t.createdAt ? timeAgo(t.createdAt) : "—"} +
+ {canUse && usable(t) ? ( + + ) : null} {canRemove(t) ? ( ) : null} +
))} From 5b5e1240d76ca3a356556571108b2bd116ea6140 Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 17:21:50 +0700 Subject: [PATCH 23/86] feat(sandbox): run a new sandbox on a server you pick createSandbox takes an optional `server`, a sandbox server's id or name in the team. Pyo then uses only that server, makes base there first when it doesn't have it, and says plainly when it can't take the sandbox, like "sandbox-1 has no room for 2 GB. Pick another server or a smaller size." Left out, Pyo picks one as before. The CLI has --server and the MCP create_sandbox tool has `server`. --- crates/pyo-api/src/sandbox/api.rs | 6 +- crates/pyo-api/src/sandbox/e2e.rs | 12 +++ crates/pyo-api/src/sandbox/mcp.rs | 2 + crates/pyo-api/src/sandbox/mod.rs | 40 ++++++-- crates/pyo-api/src/sandbox/scheduler.rs | 131 ++++++++++++++++++++++++ crates/pyo-cli/src/sandbox.rs | 12 +++ 6 files changed, 190 insertions(+), 13 deletions(-) diff --git a/crates/pyo-api/src/sandbox/api.rs b/crates/pyo-api/src/sandbox/api.rs index 5e04b22..3554655 100644 --- a/crates/pyo-api/src/sandbox/api.rs +++ b/crates/pyo-api/src/sandbox/api.rs @@ -133,7 +133,7 @@ pub(crate) async fn get_one( /// `connection`: the server's address and a token, valid an hour, for exec, /// terminals, files and ports there. Ask for a new token with /// `createSandboxToken`. `teamId` may be left out when the API key (or -/// you) can use only one team. +/// you) can use only one team. `server` runs it on that server only. /// /// A server that doesn't have the template `base` yet makes it first: /// the first sandbox from `base` there takes a minute or so longer, and @@ -144,10 +144,10 @@ pub(crate) async fn get_one( (status = 201, description = "`sandbox` (as in `listSandboxes`) and `connection`: `url`, `sandboxId`, `token` and `expiresAt`.", body = Object), (status = 400, description = "A size, lifetime or other value Pyo can't accept, or no `teamId` when you can use more than one team.", body = ErrorBody), (status = 403, description = "Your role, the API key's access or the team's sandbox limits don't allow it.", body = ErrorBody), - (status = 404, description = "No such team, template or project.", body = ErrorBody), + (status = 404, description = "No such team, template, project or server.", body = ErrorBody), (status = 409, description = "No sandbox server can run it: none yet, none with the template, or none with KVM for `isolation: vm`.", body = ErrorBody), (status = 502, description = "The server couldn't make `base`. The error says why.", body = ErrorBody), - (status = 503, description = "Every sandbox server is full or down, or `base` is still being made there. Try again soon.", body = ErrorBody), + (status = 503, description = "Every sandbox server is full or down, the server asked for has no room, or `base` is still being made there. Try again soon.", body = ErrorBody), ), extensions(("x-pyo-access" = json!("sandboxes"))))] pub(crate) async fn create( diff --git a/crates/pyo-api/src/sandbox/e2e.rs b/crates/pyo-api/src/sandbox/e2e.rs index 85092d8..e55925c 100644 --- a/crates/pyo-api/src/sandbox/e2e.rs +++ b/crates/pyo-api/src/sandbox/e2e.rs @@ -250,6 +250,7 @@ async fn pyo_drives_a_real_daemon() { let candidate = scheduler::Candidate { id: server.into(), name: "e2e".into(), + team_id: Some(team.into()), enabled: true, ready: true, report_age_secs: 0, @@ -702,6 +703,17 @@ async fn the_control_plane_drives_a_real_daemon() { super::delete(&s, &user, other["sandbox"]["id"].as_str().unwrap()) .await .unwrap(); + // On the server asked for, by name. One the team doesn't have is refused. + let mut on_e2e = no_template.clone(); + on_e2e.server = Some("e2e".into()); + let pinned = super::create(&s, &user, on_e2e.clone()).await.unwrap(); + assert_eq!(pinned["sandbox"]["serverId"], json!(server), "{pinned}"); + super::delete(&s, &user, pinned["sandbox"]["id"].as_str().unwrap()) + .await + .unwrap(); + on_e2e.server = Some("nowhere".into()); + let e = super::create(&s, &user, on_e2e).await.unwrap_err(); + assert_eq!(e.status, axum::http::StatusCode::NOT_FOUND, "{}", e.message); let first_id = first["sandbox"]["id"].as_str().unwrap().to_owned(); let on: String = sqlx::query_scalar("SELECT spec->>'template' FROM pyo.sandboxes WHERE id = $1") diff --git a/crates/pyo-api/src/sandbox/mcp.rs b/crates/pyo-api/src/sandbox/mcp.rs index 3c69aea..3cdb417 100644 --- a/crates/pyo-api/src/sandbox/mcp.rs +++ b/crates/pyo-api/src/sandbox/mcp.rs @@ -40,6 +40,7 @@ pub fn defs() -> Vec { json!({ "name": "create_sandbox", "title": "Create sandbox", "description": "Create an isolated machine (a sandbox) on the team's sandbox servers, to run code, commands and files in. It ends after its lifetime (5 minutes when omitted). Returns its id.", "inputSchema": schema(json!({ "team": team, "template": { "type": "string", "description": "A sandbox template name, like node-22 (list_sandbox_templates shows them). base when omitted: Debian 13 with git, Python, uv, Node.js and pnpm. A server makes base the first time it's used there, which takes a minute or so" }, + "server": { "type": "string", "description": "A sandbox server's name or id to run it on. Pyo picks one when omitted" }, "cpus": { "type": "integer", "minimum": 1, "maximum": 32 }, "memory_mib": { "type": "integer", "minimum": 128 }, "ttl": { "type": "string", "description": "How long it lives, like 30m or 1h (24h at most)" }, @@ -152,6 +153,7 @@ pub async fn run(s: &AppState, user: &User, name: &str, args: &Value) -> Result< let mut req = json!({ "teamId": team }); for (from, to) in [ ("template", "template"), + ("server", "server"), ("cpus", "cpus"), ("memory_mib", "memoryMib"), ("ttl", "ttl"), diff --git a/crates/pyo-api/src/sandbox/mod.rs b/crates/pyo-api/src/sandbox/mod.rs index f83affa..32b0136 100644 --- a/crates/pyo-api/src/sandbox/mod.rs +++ b/crates/pyo-api/src/sandbox/mod.rs @@ -422,6 +422,10 @@ pub struct CreateSandbox { /// the first sandbox from it there takes longer. #[serde(default)] pub template: Option, + /// The sandbox server to run it on, by id or name: the team's own or + /// the installation's. Pyo picks one when omitted. + #[serde(default)] + pub server: Option, /// 1 when omitted. #[serde(default)] pub cpus: Option, @@ -536,6 +540,7 @@ pub async fn candidates( struct Row { id: String, name: String, + team_id: Option, address: Option, state: String, enabled: bool, @@ -546,7 +551,7 @@ pub async fn candidates( pending_mib: i64, } let rows: Vec = sqlx::query_as( - "SELECT s.id, s.name, s.address, s.state, s.enabled, s.capability, s.capacity, s.templates, + "SELECT s.id, s.name, s.team_id, s.address, s.state, s.enabled, s.capability, s.capacity, s.templates, extract(epoch FROM now() - s.last_report_at)::bigint AS age, coalesce((SELECT sum((x.spec->>'memoryMib')::bigint) FROM pyo.sandboxes x WHERE x.server_id = s.id AND x.state NOT IN ('stopped', 'failed', 'paused') @@ -563,6 +568,7 @@ pub async fn candidates( let c = scheduler::Candidate { id: r.id, name: r.name, + team_id: r.team_id, enabled: r.enabled, ready: r.state == "ready" && r.address.is_some(), report_age_secs: r.age.unwrap_or(i64::MAX), @@ -691,15 +697,29 @@ pub async fn create(s: &AppState, user: &User, req: CreateSandbox) -> ApiResult< let servers = candidates(&mut tx, &team_id).await?; let list: Vec = servers.iter().map(|(c, _)| c.clone()).collect(); let on_demand = base::on_demand(&template.id); - let placed = scheduler::place(&list, &spec, &template.id, on_demand).map_err(|n| { - let n = match n { - scheduler::NoServer::NoTemplate(_) => { - scheduler::NoServer::NoTemplate(template.name.clone()) - } - n => n, - }; - ApiError::new(n.status(), n.message()) - })?; + let placed = match req + .server + .as_deref() + .map(str::trim) + .filter(|v| !v.is_empty()) + { + Some(asked) => scheduler::place_on(&list, &team_id, asked, &spec, &template.id, on_demand) + .map_err(|(status, message)| { + ApiError::new( + status, + message.replace(&template.server_name, &template.name), + ) + })?, + None => scheduler::place(&list, &spec, &template.id, on_demand).map_err(|n| { + let n = match n { + scheduler::NoServer::NoTemplate(_) => { + scheduler::NoServer::NoTemplate(template.name.clone()) + } + n => n, + }; + ApiError::new(n.status(), n.message()) + })?, + }; // The server may still start it from an older revision of the template. spec.template = placed.template.clone(); let build = placed.build; diff --git a/crates/pyo-api/src/sandbox/scheduler.rs b/crates/pyo-api/src/sandbox/scheduler.rs index 5574ca5..15cc1a7 100644 --- a/crates/pyo-api/src/sandbox/scheduler.rs +++ b/crates/pyo-api/src/sandbox/scheduler.rs @@ -28,6 +28,8 @@ pub const FRESH_SECS: i64 = 90; pub struct Candidate { pub id: String, pub name: String, + /// None for the installation's, which every team may use. + pub team_id: Option, pub enabled: bool, pub ready: bool, /// Seconds since its last report. @@ -82,6 +84,29 @@ impl NoServer { } } + /// Why the one server asked for (`server`, by name) can't take a + /// sandbox of `memory_mib`. + pub fn message_on(&self, server: &str, memory_mib: u32) -> String { + match self { + NoServer::None | NoServer::NoneUp => { + format!("{server} can't take sandboxes right now. Pick another server.") + } + NoServer::NoVm => { + format!("{server} has no KVM, so isolation \"vm\" can't run there.") + } + NoServer::NoNetwork => { + format!("{server} has no networking. Pick another server, or set network to none.") + } + NoServer::NoTemplate(t) => { + format!("{server} doesn't have the template {t} yet. Pick another server.") + } + NoServer::Full => format!( + "{server} has no room for {}. Pick another server or a smaller size.", + size(memory_mib) + ), + } + } + /// 503 for what passes on its own, 409 for what needs a change. pub fn status(&self) -> axum::http::StatusCode { match self { @@ -91,6 +116,16 @@ impl NoServer { } } +/// "512 MB", "2 GB", "1.5 GB". +fn size(mib: u32) -> String { + if mib >= 1024 { + let gb = (mib as f64 / 1024.0 * 10.0).round() / 10.0; + format!("{gb} GB") + } else { + format!("{mib} MB") + } +} + /// The servers up and able to run `spec`, whatever their templates. fn usable<'a>( servers: &'a [Candidate], @@ -262,6 +297,31 @@ pub fn place<'a>( .ok_or(NoServer::Full) } +/// [`place`] on one server the create asked for, by its id or name: one of +/// `team`'s or the installation's. The error says why it can't, plainly. +pub fn place_on<'a>( + servers: &'a [Candidate], + team: &str, + asked: &str, + spec: &SandboxSpec, + id: &str, + on_demand: bool, +) -> Result, (axum::http::StatusCode, String)> { + let asked = asked.trim(); + let server = servers + .iter() + .filter(|c| c.team_id.as_deref().is_none_or(|t| t == team)) + .find(|c| c.id == asked || c.name == asked) + .ok_or_else(|| { + ( + axum::http::StatusCode::NOT_FOUND, + format!("No sandbox server {asked} in this team."), + ) + })?; + place(std::slice::from_ref(server), spec, id, on_demand) + .map_err(|n| (n.status(), n.message_on(&server.name, spec.memory_mib))) +} + #[cfg(test)] mod tests { use super::*; @@ -273,6 +333,7 @@ mod tests { Candidate { id: id.into(), name: format!("{id}-name"), + team_id: None, enabled: true, ready: true, report_age_secs: 5, @@ -539,6 +600,76 @@ mod tests { assert_eq!((p.server.id.as_str(), p.build), ("failed", true)); } + #[test] + fn a_server_asked_for_takes_it_or_says_why() { + use axum::http::StatusCode; + let mut s = spec(); + let mut other_team = server("theirs", Runtime::Firecracker, 30); + other_team.team_id = Some("tm_two".into()); + let mut ours = server("ours", Runtime::Firecracker, 2); + ours.team_id = Some("tm_one".into()); + let mut joining = server("joining", Runtime::Firecracker, 30); + joining.ready = false; + let installation = server("shared", Runtime::Gvisor, 30); + let servers = vec![other_team, ours, joining, installation]; + let on = |asked: &str, s: &SandboxSpec, on_demand: bool| { + place_on(&servers, "tm_one", asked, s, "stp_node", on_demand) + .map(|p| (p.server.id.clone(), p.build)) + }; + // By id or name, the team's own or the installation's, even when + // another has more room. + assert_eq!(on("ours", &s, false), Ok(("ours".into(), false))); + assert_eq!(on("ours-name", &s, false), Ok(("ours".into(), false))); + assert_eq!(on(" shared ", &s, false), Ok(("shared".into(), false))); + // Another team's is as good as missing. + assert_eq!( + on("theirs", &s, false), + Err(( + StatusCode::NOT_FOUND, + "No sandbox server theirs in this team.".into() + )) + ); + // Not ready. + assert_eq!( + on("joining-name", &s, false), + Err(( + StatusCode::SERVICE_UNAVAILABLE, + "joining-name can't take sandboxes right now. Pick another server.".into() + )) + ); + // No room. + s.memory_mib = 3072; + assert_eq!( + on("ours", &s, false), + Err(( + StatusCode::SERVICE_UNAVAILABLE, + "ours-name has no room for 3 GB. Pick another server or a smaller size.".into() + )) + ); + s.memory_mib = 1024; + // No KVM for "vm". + let mut vm = s.clone(); + vm.isolation = Isolation::Vm; + assert_eq!( + on("shared", &vm, false).unwrap_err().1, + "shared-name has no KVM, so isolation \"vm\" can't run there." + ); + // A server without base makes it first. A team template it lacks + // is refused. + let mut base = s.clone(); + base.template = "stp_base.new".into(); + let p = place_on(&servers, "tm_one", "ours", &base, "stp_base", true).unwrap(); + assert_eq!((p.server.id.as_str(), p.build), ("ours", true)); + assert_eq!( + place_on(&servers, "tm_one", "ours", &base, "stp_base", false) + .unwrap_err() + .1, + "ours-name doesn't have the template stp_base.new yet. Pick another server." + ); + assert_eq!(size(512), "512 MB"); + assert_eq!(size(1536), "1.5 GB"); + } + #[test] fn internet_needs_a_server_with_networking() { let mut off = server("off", Runtime::Firecracker, 8); diff --git a/crates/pyo-cli/src/sandbox.rs b/crates/pyo-cli/src/sandbox.rs index 978c74c..7cf7fca 100644 --- a/crates/pyo-cli/src/sandbox.rs +++ b/crates/pyo-cli/src/sandbox.rs @@ -25,6 +25,10 @@ pub enum Command { /// Default base: Debian 13 with git, Python, uv, Node.js and pnpm. /// A server makes base the first time it's used there. template: Option, + /// The sandbox server to run it on, by name or id. Pyo picks one + /// when left out. + #[arg(long)] + server: Option, /// CPUs. Default 1. #[arg(long)] cpus: Option, @@ -249,6 +253,7 @@ pub async fn run(api: &Api, team: Option<&str>, cmd: Command) -> Result<()> { match cmd { Command::Create { template, + server, cpus, memory, disk, @@ -265,6 +270,7 @@ pub async fn run(api: &Api, team: Option<&str>, cmd: Command) -> Result<()> { let body = create_body(CreateArgs { team: &team, template: template.as_deref(), + server: server.as_deref(), cpus, memory, disk, @@ -412,6 +418,7 @@ async fn lifecycle(api: &Api, id: &str, what: &str, done: &str) -> Result<()> { pub struct CreateArgs<'a> { pub team: &'a str, pub template: Option<&'a str>, + pub server: Option<&'a str>, pub cpus: Option, pub memory: Option, pub disk: Option, @@ -433,6 +440,7 @@ pub fn create_body(a: CreateArgs) -> Result { } }; set("template", json!(a.template)); + set("server", json!(a.server)); set("cpus", json!(a.cpus)); set("memoryMib", json!(a.memory)); set("diskMib", json!(a.disk)); @@ -1115,6 +1123,7 @@ mod tests { let b = create_body(CreateArgs { team: "tm_1", template: Some("node-22"), + server: Some("sandbox-1"), cpus: Some(2), memory: None, disk: None, @@ -1132,6 +1141,7 @@ mod tests { json!({ "teamId": "tm_1", "template": "node-22", + "server": "sandbox-1", "cpus": 2, "ttl": "1h", "network": "none", @@ -1145,6 +1155,7 @@ mod tests { let b = create_body(CreateArgs { team: "tm_1", template: None, + server: None, cpus: None, memory: None, disk: None, @@ -1177,6 +1188,7 @@ mod tests { let e = create_body(CreateArgs { team: "t", template: Some("x"), + server: None, cpus: None, memory: None, disk: None, From e130b006ea943cdabc4c01f0eb1fb0f3475111ee Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 17:21:50 +0700 Subject: [PATCH 24/86] feat(dash): pick the server in New sandbox A Server field, "Any server" by default, lists each server with its runtime and free memory. Servers that aren't ready, are off, lack networking or have no room for the size are disabled with the reason. The code beside the form shows the server when one is picked. --- dash/scripts/sandbox-templates.test.ts | 24 ++++++++++- dash/src/lib/pyo/sandbox.ts | 6 +++ dash/src/lib/pyo/sandboxTemplates.ts | 31 +++++++++++++- dash/src/routes/_app/$team/sandboxes/new.tsx | 43 +++++++++++++++++--- 4 files changed, 95 insertions(+), 9 deletions(-) diff --git a/dash/scripts/sandbox-templates.test.ts b/dash/scripts/sandbox-templates.test.ts index 69a58dc..0fbfe57 100644 --- a/dash/scripts/sandbox-templates.test.ts +++ b/dash/scripts/sandbox-templates.test.ts @@ -1,11 +1,13 @@ // Pyo's built-in sandbox template in the dashboard: how its image reads, -// and that new sandboxes start on it. +// that new sandboxes start on it, and the servers a new sandbox can go to. // bun test scripts/sandbox-templates.test.ts import { expect, test } from "bun:test"; import { defaultTemplate, + freeLabel, gettingReady, imageRepo, + serverRefusal, templateImage, templateStatus, usable, @@ -58,3 +60,23 @@ test("base's status says it's ready to use", () => { expect(gettingReady("base", "sandbox-1")).toBe("Getting base ready on sandbox-1…"); expect(gettingReady("base", null)).toBe("Getting base ready…"); }); + +test("a server is offered with its room, or why it can't take the sandbox", () => { + const GB = 1024 ** 3; + const ok = { + state: "ready", + enabled: true, + capability: { runtime: "firecracker", network: true }, + capacity: { memoryAvailable: 7.2 * GB, memoryReserved: GB }, + }; + expect(serverRefusal(ok, 2048, "internet")).toBe(""); + expect(freeLabel(6.2 * GB)).toBe("6.2 GB free"); + expect(freeLabel(512 * 1024 ** 2)).toBe("512 MB free"); + expect(serverRefusal(ok, 8192, "internet")).toBe("No room"); + expect(serverRefusal({ ...ok, state: "joining" }, 512, "none")).toBe("Setting up"); + expect(serverRefusal({ ...ok, state: "unreachable" }, 512, "none")).toBe("Not reachable"); + expect(serverRefusal({ ...ok, enabled: false }, 512, "none")).toBe("Turned off"); + const offline = { ...ok, capability: { runtime: "gvisor", network: false } }; + expect(serverRefusal(offline, 512, "internet")).toBe("No networking"); + expect(serverRefusal(offline, 512, "none")).toBe(""); +}); diff --git a/dash/src/lib/pyo/sandbox.ts b/dash/src/lib/pyo/sandbox.ts index 2086595..cfbea5f 100644 --- a/dash/src/lib/pyo/sandbox.ts +++ b/dash/src/lib/pyo/sandbox.ts @@ -11,9 +11,12 @@ import type { CloudServerState } from "./types"; export { BASE_TEMPLATE, defaultTemplate, + freeLabel, + freeMemory, gettingReady, imageRepo, isBase, + serverRefusal, templateImage, templateStatus, usable, @@ -246,6 +249,8 @@ export type SandboxTemplateServer = { export type CreateSandbox = { teamId: string; template: string; + /** A sandbox server's id: Pyo picks one when left out. */ + server?: string; cpus?: number; memoryMib?: number; diskMib?: number; @@ -569,6 +574,7 @@ export function sdkSnippet(b: Omit) { // base is the default, so it's left out. const lines: string[] = b.template && b.template !== BASE_TEMPLATE ? [` template: ${JSON.stringify(b.template)},`] : []; + if (b.server) lines.push(` server: ${JSON.stringify(b.server)},`); if (b.cpus) lines.push(` cpus: ${b.cpus},`); if (b.memoryMib) lines.push(` memoryMib: ${b.memoryMib},`); if (b.ttl) lines.push(` ttl: ${JSON.stringify(b.ttl)},`); diff --git a/dash/src/lib/pyo/sandboxTemplates.ts b/dash/src/lib/pyo/sandboxTemplates.ts index 85f6384..de1323f 100644 --- a/dash/src/lib/pyo/sandboxTemplates.ts +++ b/dash/src/lib/pyo/sandboxTemplates.ts @@ -1,6 +1,6 @@ // Sandbox templates as the dashboard shows them: Pyo's built-in `base` -// (crates/pyo-api/src/sandbox/base.rs) and the rest. No imports, so -// dash/scripts tests them as they are. +// (crates/pyo-api/src/sandbox/base.rs) and the rest, and the servers a new +// sandbox can go to. No imports, so dash/scripts tests them as they are. type TemplateLike = { name?: string; @@ -49,3 +49,30 @@ export function templateStatus(t: TemplateLike) { /** What a sandbox waiting for its server to make its template shows. */ export const gettingReady = (template: string, server: string | null) => server ? `Getting ${template} ready on ${server}…` : `Getting ${template} ready…`; + +type ServerLike = { + state: string; + enabled: boolean; + capability: { runtime: string; network?: boolean } | null; + capacity: { memoryAvailable: number; memoryReserved: number } | null; +}; + +/** Bytes a server can still give sandboxes. */ +export const freeMemory = (x: ServerLike) => + x.capacity ? Math.max(0, x.capacity.memoryAvailable - x.capacity.memoryReserved) : 0; + +/** "6.2 GB free", "512 MB free". */ +export function freeLabel(bytes: number) { + const mb = bytes / 1024 ** 2; + return mb >= 1024 ? `${+(mb / 1024).toFixed(1)} GB free` : `${Math.floor(mb)} MB free`; +} + +/** Why a server can't take a sandbox of this size, or "" when it can. */ +export function serverRefusal(x: ServerLike, memoryMib: number, network: "internet" | "none") { + if (x.state !== "ready") return x.state === "joining" ? "Setting up" : "Not reachable"; + if (!x.enabled) return "Turned off"; + if (!x.capability || x.capability.runtime === "none") return "Can't run sandboxes"; + if (network === "internet" && !x.capability.network) return "No networking"; + if (freeMemory(x) < memoryMib * 1024 ** 2) return "No room"; + return ""; +} diff --git a/dash/src/routes/_app/$team/sandboxes/new.tsx b/dash/src/routes/_app/$team/sandboxes/new.tsx index 438cbdb..0a7e443 100644 --- a/dash/src/routes/_app/$team/sandboxes/new.tsx +++ b/dash/src/routes/_app/$team/sandboxes/new.tsx @@ -2,7 +2,7 @@ import { CaretDownIcon, CircleNotchIcon, CodeIcon, HardDrivesIcon } from "@phosp import { createFileRoute, useNavigate } from "@tanstack/react-router"; import { useState } from "react"; import { z } from "zod"; -import { useSandboxes } from "~/components/pyo/sandbox/common"; +import { RuntimeBadge, useSandboxes } from "~/components/pyo/sandbox/common"; import { AppLink } from "~/components/ui/AppLink"; import { Button, ButtonLink } from "~/components/ui/Button"; import { Field, Segmented, Select, Switch, Textarea } from "~/components/ui/Field"; @@ -15,9 +15,13 @@ import { currentSlug } from "~/lib/pyo/paths"; import { useQuery } from "~/lib/pyo/query"; import { defaultTemplate, + freeLabel, + freeMemory, gettingReady, isBase, LIFETIMES, + RUNTIME_LABEL, + serverRefusal, usable, SIZES, rememberConnection, @@ -104,6 +108,8 @@ function NewSandboxForm() { const ready = templates.status === "success" ? templates.data.templates : []; const search = Route.useSearch(); const [template, setTemplate] = useState(search.template ?? ""); + const servers = sandboxServersIn(s); + const [server, setServer] = useState(""); const [size, setSize] = useState<(typeof SIZES)[number]["value"]>("1"); const [ttl, setTtl] = useState<(typeof LIFETIMES)[number]["value"]>("1h"); const [idle, setIdle] = useState(false); @@ -123,6 +129,8 @@ function NewSandboxForm() { const picked = (template && ready.some((t) => t.name === template && usable(t)) ? template : "") || defaultTemplate(ready); const sz = SIZES.find((x) => x.value === size)!; + const refusal = (x: (typeof servers)[number]) => serverRefusal(x, sz.memoryMib, network); + const pinned = servers.find((x) => x.id === server && !refusal(x)); const pickedBase = ready.find((t) => t.name === picked && isBase(t)); // While a create waits for its server to make base there. const preparing = @@ -134,6 +142,7 @@ function NewSandboxForm() { const body: CreateSandbox = { teamId: team, template: picked, + ...(pinned ? { server: pinned.id } : {}), cpus: sz.cpus, memoryMib: sz.memoryMib, ttl, @@ -225,11 +234,33 @@ function NewSandboxForm() { ) }
- {pickedBase && !pickedBase.servers.length ? ( -

- The first sandbox from base on a server takes a minute longer. -

- ) : null} + + ) : pickedBase && !pickedBase.servers.length ? ( + "The first sandbox from base on a server takes a minute longer." + ) : undefined + } + > + {(p) => ( + + )} + Date: Mon, 16 Nov 2026 19:48:17 +0700 Subject: [PATCH 25/86] chore(dash): serve the dev server on the tailnet too --- dash/vite.config.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dash/vite.config.ts b/dash/vite.config.ts index 270d1c6..923b042 100644 --- a/dash/vite.config.ts +++ b/dash/vite.config.ts @@ -15,6 +15,9 @@ export default defineConfig(({ mode }) => { return { server: { port: 5173, + // Reachable from the tailnet too, at http://..ts.net:5173. + host: true, + allowedHosts: [".ts.net"], proxy: { // Only /api and /api/*: /api-keys is a dashboard page. "^/api(/|$)": { target: api, changeOrigin: false, ws: true }, From fe55a97f814dcc4cd817dd9ee4344c97a0cc13c6 Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 20:24:39 +0700 Subject: [PATCH 26/86] fix(build): copy install.sh into the pyo image build pyo-api reads install.sh's host units at compile time since dfeffde, so the image build failed without it. --- deploy/images/pyo.Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/deploy/images/pyo.Dockerfile b/deploy/images/pyo.Dockerfile index ccf9214..c5fbd84 100644 --- a/deploy/images/pyo.Dockerfile +++ b/deploy/images/pyo.Dockerfile @@ -27,8 +27,10 @@ RUN apt-get update && apt-get install -y --no-install-recommends musl-tools && r WORKDIR /src COPY Cargo.toml Cargo.lock ./ COPY crates ./crates -# pyo-api serves the sandbox server's installer (include_str! in join.rs). +# pyo-api serves the sandbox server's installer and the host units +# from install.sh (include_str! in join.rs). COPY deploy/sandbox.sh ./deploy/sandbox.sh +COPY deploy/install.sh ./deploy/install.sh ARG CARGO_PROFILE=release # Static (musl): pyo-builder (it also runs on any Linux VM), pyo-flows and # pyo-static (scratch images) and the CLI. The two builds run side by side, each in its From 38d19cda51eabc8861b323b506152f42fd473464 Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 20:39:44 +0700 Subject: [PATCH 27/86] style(dash): simplify the sandbox templates list Show each template's image under its name, align the Use buttons with a fixed menu slot, use the table's type sizes and shorten the delete action to Delete. --- .../routes/_app/$team/sandboxes/templates.tsx | 140 ++++++++++-------- 1 file changed, 82 insertions(+), 58 deletions(-) diff --git a/dash/src/routes/_app/$team/sandboxes/templates.tsx b/dash/src/routes/_app/$team/sandboxes/templates.tsx index 1edf1ff..b0757ce 100644 --- a/dash/src/routes/_app/$team/sandboxes/templates.tsx +++ b/dash/src/routes/_app/$team/sandboxes/templates.tsx @@ -112,72 +112,96 @@ function TemplatesPage() { - - + - - {templates.map((t) => ( - - - - - - - + + + + + - - ))} + ) : null} + {/* The menu keeps its slot, so Use lines up on every row. */} +
+ {canRemove(t) ? ( + , danger: true, onSelect: () => setRemoving(t) }]} + trigger={(p) => ( + + )} + /> + ) : null} +
+ + + + ); + })}
NameImageTemplate Status Servers Added + + Actions +
- {t.name} - {t.teamId ? null : Installation} - {templateImage(t) || "—"} - - - {templateStatus(t)} - - - {t.servers.length - ? t.servers.map((x) => ( -
- - {x.name} - {serverLabel(x)} -
- )) - : {isBase(t) ? "Made on first use" : "—"}} -
{t.createdAt ? timeAgo(t.createdAt) : "—"} -
- {canUse && usable(t) ? ( -
+
+ {t.name} + {t.teamId ? null : Installation} +
+ {image ? ( +
+ {image} +
+ ) : null} +
+ - Use - - ) : null} - {canRemove(t) ? ( - , danger: true, onSelect: () => setRemoving(t) }]} - trigger={(p) => ( - + {t.servers.length ? ( +
    + {t.servers.map((x) => ( +
  • + + {x.name} + + {serverLabel(x)} + +
  • + ))} +
+ ) : ( + {isBase(t) ? "Made on first use" : "—"} + )} +
+ {t.createdAt ? timeAgo(t.createdAt) : "—"} + +
+ {canUse && usable(t) ? ( + - )} - /> - ) : null} -
-
From 1f0cbdf96d708b2b7c579bb2a273332646512874 Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 20:46:36 +0700 Subject: [PATCH 28/86] style(dash): show sandbox templates with logos and details on hover Each template gets a logo tile from its image, Debian for base. The table keeps the template, its status and Use. The image, servers and when it was added move into a tooltip on the name, and errors into one on the status. --- .../routes/_app/$team/sandboxes/templates.tsx | 237 ++++++++++++------ 1 file changed, 155 insertions(+), 82 deletions(-) diff --git a/dash/src/routes/_app/$team/sandboxes/templates.tsx b/dash/src/routes/_app/$team/sandboxes/templates.tsx index b0757ce..5737d3c 100644 --- a/dash/src/routes/_app/$team/sandboxes/templates.tsx +++ b/dash/src/routes/_app/$team/sandboxes/templates.tsx @@ -1,6 +1,28 @@ -import { DotsThreeIcon, SquaresFourIcon, TrashIcon } from "@phosphor-icons/react"; +import { CubeIcon, DotsThreeIcon, SquaresFourIcon, TrashIcon } from "@phosphor-icons/react"; import { createFileRoute, useNavigate } from "@tanstack/react-router"; import { useState } from "react"; +import { + siAlmalinux, + siAlpinelinux, + siArchlinux, + siBun, + siCentos, + siDebian, + siDeno, + siDocker, + siFedora, + siGo, + siNodedotjs, + siOpenjdk, + siPhp, + siPython, + siRockylinux, + siRuby, + siRust, + siUbuntu, + type SimpleIcon, +} from "simple-icons"; +import { BrandMark } from "~/components/pyo/common"; import { Dot } from "~/components/ui/Badge"; import { Button } from "~/components/ui/Button"; import { ConfirmDialog } from "~/components/ui/Dialog"; @@ -8,6 +30,7 @@ import { Field, Input } from "~/components/ui/Field"; import { Menu } from "~/components/ui/Menu"; import { EmptyState, ErrorState, Page, PageHeader, Panel } from "~/components/ui/Panel"; import { Table, Td, Th, Tr } from "~/components/ui/Table"; +import { Tooltip } from "~/components/ui/Tooltip"; import { megabytes, timeAgo } from "~/lib/pyo/format"; import { currentSlug } from "~/lib/pyo/paths"; import { useQuery } from "~/lib/pyo/query"; @@ -101,7 +124,7 @@ function TemplatesPage() { const templates = q.status === "success" ? q.data.templates : []; return ( - + {q.status === "error" ? ( @@ -109,99 +132,65 @@ function TemplatesPage() { ) : templates.length ? ( - +
- - - + - {templates.map((t) => { - const image = templateImage(t); - return ( - - + - - - - + + - - ); - })} + + + + ))}
TemplateStatusServersAddedStatus Actions
-
+ {templates.map((t) => ( +
+ }> + + {t.name} - {t.teamId ? null : Installation} - - {image ? ( -
- {image} -
- ) : null} -
- - - {templateStatus(t)} - - {t.servers.length ? ( -
    - {t.servers.map((x) => ( -
  • - - {x.name} - - {serverLabel(x)} - -
  • - ))} -
- ) : ( - {isBase(t) ? "Made on first use" : "—"} - )} -
- {t.createdAt ? timeAgo(t.createdAt) : "—"} - -
- {canUse && usable(t) ? ( - + +
+ + +
+ {canUse && usable(t) ? ( + + ) : null} + {/* The menu keeps its slot, so Use lines up on every row. */} +
+ {canRemove(t) ? ( + , danger: true, onSelect: () => setRemoving(t) }]} + trigger={(p) => ( + + )} + /> ) : null} - {/* The menu keeps its slot, so Use lines up on every row. */} -
- {canRemove(t) ? ( - , danger: true, onSelect: () => setRemoving(t) }]} - trigger={(p) => ( - - )} - /> - ) : null} -
-
@@ -269,3 +258,87 @@ function TemplatesPage() {
); } + +// Logos by what the image runs: languages first, since node:22 is Debian +// underneath but reads as Node. +const LOGOS: [RegExp, SimpleIcon][] = [ + [/\bnode(js)?\b/, siNodedotjs], + [/\bpython\b/, siPython], + [/\bbun\b/, siBun], + [/\bdeno\b/, siDeno], + [/\bgolang\b|(^|\/)go(:|$)/, siGo], + [/\brust\b/, siRust], + [/\bruby\b/, siRuby], + [/\bphp\b/, siPhp], + [/\b(openjdk|java|temurin)\b/, siOpenjdk], + [/\bubuntu\b/, siUbuntu], + [/\bdebian\b/, siDebian], + [/\balpine\b/, siAlpinelinux], + [/\bfedora\b/, siFedora], + [/\barch(linux)?\b/, siArchlinux], + [/\bcentos\b/, siCentos], + [/\brocky(linux)?\b/, siRockylinux], + [/\balma(linux)?\b/, siAlmalinux], + [/\bdocker\b/, siDocker], +]; + +function logoFor(t: SandboxTemplate) { + if (isBase(t)) return siDebian; + const text = `${t.source?.image ?? ""} ${t.source?.os ?? ""} ${t.name}`.toLowerCase(); + return LOGOS.find(([re]) => re.test(text))?.[1]; +} + +function TemplateLogo({ t }: { t: SandboxTemplate }) { + const logo = logoFor(t); + return ( + + {logo ? : } + + ); +} + +/** The status, with its error on hover when there is one. */ +function TemplateState({ t }: { t: SandboxTemplate }) { + const label = ( + + + {templateStatus(t)} + + ); + const error = t.error ?? t.servers.find((x) => x.error)?.error; + return error ? {label} : label; +} + +/** What hovering a template shows: its image, its servers and when it was added. */ +function TemplateDetails({ t }: { t: SandboxTemplate }) { + const image = templateImage(t); + const added = t.createdAt ? `Added ${timeAgo(t.createdAt)}${t.createdBy ? ` by ${t.createdBy}` : ""}` : ""; + return ( + + {/* Base says what it runs ("Debian 13 · ghcr.io/…"): the OS on its own line. */} + {image.split(" · ").map((line, i, all) => ( + 1 && !i ? "font-medium" : "font-mono text-[11.5px]"}`}> + {line} + + ))} + {t.servers.length ? ( + + {t.servers.map((x) => ( + + + {x.name} + + {serverLabel(x)} + + + ))} + + ) : isBase(t) ? ( + Made on first use + ) : null} + {added || !t.teamId ? ( + {[t.teamId ? "" : "Installation", added].filter(Boolean).join(" · ")} + ) : null} + + ); +} From 590870696202388aa2f773f11a94945d56636a9d Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 20:52:09 +0700 Subject: [PATCH 29/86] fix(dash): stop the domain check flashing an error after deploy The store adds the new service as soon as Deploy is clicked, so the form's checks found its own name and domains taken and showed them in red until the page moved on. Leave the services this form made out of those checks, and hide problems while the service is being created. --- .../components/pyo/wizard/DeployWizard.tsx | 26 ++++++++++++------- dash/src/components/pyo/wizard/sections.tsx | 18 ++++++++----- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/dash/src/components/pyo/wizard/DeployWizard.tsx b/dash/src/components/pyo/wizard/DeployWizard.tsx index b2dbbd4..c7c6273 100644 --- a/dash/src/components/pyo/wizard/DeployWizard.tsx +++ b/dash/src/components/pyo/wizard/DeployWizard.tsx @@ -585,7 +585,13 @@ function Configure({ () => s.services.filter((a) => a.projectId === d.projectId).map((a) => providerOfService(a)), [s.services, d.projectId], ); - const sections = useDraftSections(d, set, attempted, [], providers); + // Services this form made: shown in the store at once, they don't count + // as taking the draft's own name or domains. + const [ownIds, setOwnIds] = useState([]); + const [creating, setCreating] = useState(false); + // While it's being created the form was valid: no problems flash up. + const showProblems = attempted && !creating; + const sections = useDraftSections(d, set, showProblems, [], providers, ownIds); const { t, all, count, shown, header, summaries, rows, icons, runsOn, project } = sections; // A template's databases: each picked or made, connected on create. @@ -596,7 +602,6 @@ function Configure({ const catalog = useExtensionCatalog(); const dbProblems = useDbProblems(reqs, dbs, d.clusterId, d.name, catalog); const [dbError, setDbError] = useState(""); - const [creating, setCreating] = useState(false); const made = useRef<{ projectId?: string; companions: string[] }>({ companions: [] }); const clusterName = s.clusters.find((c) => c.id === d.clusterId)?.name ?? "the cluster"; @@ -693,7 +698,8 @@ function Configure({ // On to the deployment once the API has the service. A refusal shows // as a toast and the form stays as it is. setCreating(true); - const { deploymentId, done } = api.createService(target, { databases, confirm }); + const { serviceId, deploymentId, done } = api.createService(target, { databases, confirm }); + setOwnIds((ids) => [...ids, serviceId]); if (!(await done)) { setCreating(false); return; @@ -718,7 +724,7 @@ function Configure({ const [open, setOpen] = useState>>( initial.cron ? { schedule: true } : {}, ); - const bad = (k: Collapsible) => attempted && !!Object.keys(all[k]).length; + const bad = (k: Collapsible) => showProblems && !!Object.keys(all[k]).length; const isOpen = (k: Collapsible) => bad(k) || (open[k] ?? ((k === "volumes" && !!t?.storage?.required) || (k === "domains" && "domains" in all.domains))); const toggle = (k: Collapsible) => @@ -748,7 +754,7 @@ function Configure({ icon={} title="Project & name" description={project ? `${project.isNew ? "New project" : "In"} ${project.name}${d.name ? ` · ${d.name}` : ""}` : "Pick a project"} - error={attempted && ["name", "projectId"].some((k) => k in all.general)} + error={showProblems && ["name", "projectId"].some((k) => k in all.general)} > @@ -757,7 +763,7 @@ function Configure({ icon={} title="Where it runs" description={runsOn ? `Runs ${runsOn.startsWith("anywhere") ? runsOn : `on ${runsOn}`}` : "Pick where it runs"} - error={attempted && ["clusterId", "placement"].some((k) => k in all.general)} + error={showProblems && ["clusterId", "placement"].some((k) => k in all.general)} > @@ -767,7 +773,7 @@ function Configure({ icon={} title={`${t.name} settings`} description={t.fields.map((f) => f.label).slice(0, 3).join(", ")} - error={attempted && Object.keys(all.general).some((k) => k.startsWith("field."))} + error={showProblems && Object.keys(all.general).some((k) => k.startsWith("field."))} >
@@ -791,7 +797,7 @@ function Configure({ icon={} title={reqs.length > 1 ? `${ENGINE_LABEL[req.engine]} database` : "Database"} description={dbSummary(req, dbs[i], d.name, s.databases.find((x) => x.id === dbs[i].existingId))} - error={(attempted && !!dbProblems[i]) || !!dbError} + error={(showProblems && !!dbProblems[i]) || !!dbError} >
cur.map((x, j) => (j === i ? v : x))); setDbError(""); }} - problem={attempted ? dbProblems[i] : ""} + problem={showProblems ? dbProblems[i] : ""} catalog={catalog} /> {dbError ? ( @@ -862,7 +868,7 @@ function Configure({ } note={ - attempted && (count || dbProblems.some(Boolean)) ? ( + showProblems && (count || dbProblems.some(Boolean)) ? (

{count ? `Fix ${count} field${count === 1 ? "" : "s"} to deploy.` : dbProblems.find(Boolean)}

diff --git a/dash/src/components/pyo/wizard/sections.tsx b/dash/src/components/pyo/wizard/sections.tsx index fe9b64e..efe73e0 100644 --- a/dash/src/components/pyo/wizard/sections.tsx +++ b/dash/src/components/pyo/wizard/sections.tsx @@ -126,6 +126,7 @@ export function useDraftSections( attempted: boolean, otherNames: string[] = [], providers?: Provider[], + ownIds: string[] = [], ) { const s = usePyo(); // The builder decides when it has the code: the Dockerfile, else the @@ -147,23 +148,26 @@ export function useDraftSections( : (found ?? "Zero config"); const t = getTemplate(d.templateId); const clusters = clustersIn(s); - // A new project has no services yet, so any name is free there. - const ctx = useMemo( - () => ({ + // A new project has no services yet, so any name is free there. The + // service this draft is creating doesn't take its own name or domains. + const ctx = useMemo(() => { + const others = s.services.filter((a) => !ownIds.includes(a.id)); + return { clusters, - takenNames: s.services + takenNames: others .filter((a) => a.projectId === d.projectId) .map((a) => a.name) .concat(otherNames), takenHosts: Object.fromEntries( - s.services.flatMap((a) => + others.flatMap((a) => a.domains.map((x) => [x.hostname.toLowerCase(), a.name]), ), ), providers, - }), + }; + }, // eslint-disable-next-line react-hooks/exhaustive-deps -- names by value - [clusters, d.projectId, s.services, otherNames.join(), providers], + [clusters, d.projectId, s.services, otherNames.join(), providers, ownIds.join()], ); const issuesOf = (k: Collapsible | "general"): Issues => SECTION_CHECKS[k].reduce( From f512cd1b2ea9523f75b8500f4e483685a1aa321b Mon Sep 17 00:00:00 2001 From: Frans Allen Date: Mon, 16 Nov 2026 21:10:33 +0700 Subject: [PATCH 30/86] style(dash): add space below the sidebar's last item --- dash/src/components/shell/AppShell.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dash/src/components/shell/AppShell.tsx b/dash/src/components/shell/AppShell.tsx index f37d12e..fe3993c 100644 --- a/dash/src/components/shell/AppShell.tsx +++ b/dash/src/components/shell/AppShell.tsx @@ -242,7 +242,7 @@ function AccountNav({ }; return (