Gateway upgrade: review round 6 fixes
Secret Scan / scan (push) Successful in 5s
Build App (Preview) / compute-version (pull_request) Successful in 5s
Secret Scan / scan (pull_request) Successful in 5s
Build App (Preview) / create-release (pull_request) Successful in 4s
Build App (Preview) / build-macos (pull_request) Successful in 3m15s
Build App (Preview) / test (pull_request) Successful in 4m10s
Build App (Preview) / build-windows (pull_request) Successful in 6m50s
Build App (Preview) / build-linux (pull_request) Successful in 8m3s
Build App (Preview) / prune-previews (pull_request) Successful in 2s

- Hash only significant lines of the build context, so a comment edit in
  a later release doesn't mark every local build outdated.
- A failed image lookup no longer fails ensure for a gateway that is fine:
  it takes the keep-current-image path.
- Recreates stop with Docker's default grace again, as before; the 3s
  grace stays for app exit.
- Cleanup keeps every build of the current pin (a stable and a preview
  app no longer delete each other's), removing only `latest` and older
  pins; parse locally instead of depending on the commands layer.
- GatewayContainer struct instead of a 4-tuple; separate repo/version
  constants; cache the local tag; fix the outdated-check doc.
- UI says "gateway image update available" rather than claiming a LiteLLM
  update.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Opus 5.5 committed 2026-11-02 15:48:35 -08:00
1 parent 2ea5bba830
commit b2cae374f9
4 files changed
+146 -74

No files matched your search

@@ -169,7 +169,7 @@ fn pick_update<'a>(
}
/// Parse a semver string like "0.2.5" -> (0, 2, 5)
pub(crate) fn parse_semver(version: &str) -> Option<(u32, u32, u32)> {
fn parse_semver(version: &str) -> Option<(u32, u32, u32)> {
let clean = version.trim_start_matches('v');
let parts: Vec<&str> = clean.split('.').collect();
if parts.len() >= 3 {
+141 -69
View File
@@ -49,6 +49,11 @@ const GATEWAY_CONFIG_VOLUME: &str = "triple-c-gateway-config";
/// also above the floors set by the proxy auth-bypass, SSRF and salt-key CVEs —
/// see the long comment in `gateway-container/Dockerfile`, and keep the two in
/// lockstep.
const GATEWAY_REGISTRY_REPO: &str = "ghcr.io/berriai/litellm";
const GATEWAY_LITELLM_VERSION: &str = "v1.106.6";
/// `repo:tag` — what the Dockerfile's `FROM` names (with the digest). Only the
/// lockstep tests read it; runtime code uses the repo, version and digest.
#[cfg(test)]
const GATEWAY_REGISTRY_IMAGE: &str = "ghcr.io/berriai/litellm:v1.106.6";
/// The digest [`GATEWAY_REGISTRY_IMAGE`] is pinned to — the same one the
@@ -58,11 +63,7 @@ const GATEWAY_REGISTRY_DIGEST: &str =
"sha256:37e2dafb088a7723beb998590682957386dfc83ffea1848486f8e0e5cfac0b06";
fn registry_image_ref() -> String {
let repo = GATEWAY_REGISTRY_IMAGE
.rsplit_once(':')
.map(|(repo, _)| repo)
.unwrap_or(GATEWAY_REGISTRY_IMAGE);
format!("{}@{}", repo, GATEWAY_REGISTRY_DIGEST)
format!("{}@{}", GATEWAY_REGISTRY_REPO, GATEWAY_REGISTRY_DIGEST)
}
/// Local builds are tagged with the pin plus a hash of the embedded build
@@ -76,20 +77,39 @@ const GATEWAY_LOCAL_REPO: &str = "triple-c-gateway";
/// The LiteLLM release this app is pinned to — the tag of
/// [`GATEWAY_REGISTRY_IMAGE`], e.g. `v1.106.6`.
fn pinned_litellm_version() -> &'static str {
GATEWAY_REGISTRY_IMAGE
.rsplit_once(':')
.map(|(_, tag)| tag)
.unwrap_or_default()
GATEWAY_LITELLM_VERSION
}
fn local_image_ref() -> String {
let context = sha256_hex(&format!("{}\0{}", GATEWAY_DOCKERFILE, GATEWAY_DEFAULT_CONFIG));
format!(
"{}:{}-{}",
GATEWAY_LOCAL_REPO,
pinned_litellm_version(),
&context[..8]
)
static REF: OnceLock<String> = OnceLock::new();
REF.get_or_init(|| {
let context = sha256_hex(&format!(
"{}\0{}",
significant_lines(GATEWAY_DOCKERFILE),
significant_lines(GATEWAY_DEFAULT_CONFIG)
));
format!(
"{}:{}-{}",
GATEWAY_LOCAL_REPO,
pinned_litellm_version(),
&context[..8]
)
})
.clone()
}
/// The build context minus full-line comments and blank lines, so rewording a
/// comment in a later release doesn't make every local build look outdated.
/// (Both files use `#` for comments.)
fn significant_lines(text: &str) -> String {
text.lines()
.map(str::trim_end)
.filter(|l| {
let t = l.trim_start();
!t.is_empty() && !t.starts_with('#')
})
.collect::<Vec<_>>()
.join("\n")
}
/// The image the gateway container should run, and its content id. The id goes
@@ -117,6 +137,16 @@ fn choose_gateway_image(local_id: Option<String>, registry_id: Option<String>) -
})
}
/// The existing gateway container, as far as `ensure` and status need it.
#[derive(Debug, Clone)]
struct GatewayContainer {
id: String,
state: String,
/// The config-fingerprint label it was created with.
fingerprint: String,
image_id: Option<String>,
}
/// Ids of the locally present images that carry the current pin.
#[derive(Debug, Clone, Default, PartialEq)]
struct PinnedImages {
@@ -161,10 +191,11 @@ async fn pinned_images() -> Result<PinnedImages, String> {
}
/// Whether an existing gateway container runs something other than the image
/// `ensure` would start it on — exactly the condition under which the next
/// start or settings save recreates it, so status and `ensure` always agree.
/// `None` for `chosen` means the image check failed: unknown is reported as
/// not outdated rather than guessed.
/// `ensure` would start it on. With a pinned image present (`Some(Some(_))`)
/// that is exactly when the next start or settings save recreates it. With no
/// pinned image (`Some(None)`) it is outdated but `ensure` keeps it as is until
/// one is pulled or built. `None` means the image check failed: unknown is
/// reported as not outdated rather than guessed.
fn container_image_outdated(
container_image_id: Option<&str>,
chosen: Option<Option<&ChosenImage>>,
@@ -199,9 +230,8 @@ async fn remove_superseded_local_builds() {
return;
}
};
let current = local_image_ref();
for tag in images.iter().flat_map(|i| i.repo_tags.iter()) {
if is_superseded_local_build(tag, &current) {
if is_superseded_local_build(tag) {
match docker
.remove_image(
tag,
@@ -220,10 +250,11 @@ async fn remove_superseded_local_builds() {
}
}
/// `latest`, a build for an older pin, or an earlier build of the current pin.
/// A newer pin (an app downgrade, another branch) and any custom tag are left
/// alone.
fn is_superseded_local_build(tag: &str, current: &str) -> bool {
/// `latest` or a build for an *older* pin. Other builds of the current pin are
/// kept: nothing shows which of two is older, and a stable and a preview app
/// with different Dockerfiles would otherwise delete each other's build. A
/// newer pin (an app downgrade, another branch) and custom tags are kept too.
fn is_superseded_local_build(tag: &str) -> bool {
let Some(name) = tag
.strip_prefix(GATEWAY_LOCAL_REPO)
.and_then(|rest| rest.strip_prefix(':'))
@@ -233,19 +264,24 @@ fn is_superseded_local_build(tag: &str, current: &str) -> bool {
if name == "latest" {
return true;
}
if tag == current {
return false;
}
let version = name.split_once('-').map(|(v, _)| v).unwrap_or(name);
match (
crate::commands::update_commands::parse_semver(version),
crate::commands::update_commands::parse_semver(pinned_litellm_version()),
) {
(Some(old), Some(pin)) => old <= pin,
match (parse_release(version), parse_release(pinned_litellm_version())) {
(Some(old), Some(pin)) => old < pin,
_ => false,
}
}
/// `v1.106.6` → `(1, 106, 6)`; anything else (custom tags) → `None`.
fn parse_release(tag: &str) -> Option<(u32, u32, u32)> {
let mut parts = tag.strip_prefix('v')?.split('.');
let parsed = (
parts.next()?.parse().ok()?,
parts.next()?.parse().ok()?,
parts.next()?.parse().ok()?,
);
parts.next().is_none().then_some(parsed)
}
const GATEWAY_DOCKERFILE: &str = include_str!("../../../../gateway-container/Dockerfile");
const GATEWAY_DEFAULT_CONFIG: &str = include_str!("../../../../gateway-container/config.yaml");
@@ -417,11 +453,11 @@ pub async fn get_gateway_status(settings: &GatewaySettings) -> Result<GatewaySta
let image_exists = matches!(chosen, Some(Some(_)));
let container = container?;
let (container_exists, running) = match &container {
Some((_, state, _, _)) => (true, state == "running"),
Some(c) => (true, c.state == "running"),
None => (false, false),
};
let image_outdated = container_image_outdated(
container.as_ref().and_then(|c| c.3.as_deref()),
container.as_ref().and_then(|c| c.image_id.as_deref()),
chosen.as_ref().map(|c| c.as_ref()),
);
@@ -441,7 +477,7 @@ pub async fn get_gateway_status(settings: &GatewaySettings) -> Result<GatewaySta
/// settings reconcile, which must not start anything the user never started.
pub async fn gateway_container_presence() -> Result<(bool, bool), String> {
Ok(match find_gateway_container().await? {
Some((_, state, _, _)) => (true, state == "running"),
Some(c) => (true, c.state == "running"),
None => (false, false),
})
}
@@ -458,9 +494,8 @@ fn is_gateway_container(names: Option<&Vec<String>>) -> bool {
names.is_some_and(|names| names.iter().any(|n| n == &expected))
}
/// `(id, state, config fingerprint label, image id)` for the gateway container,
/// if any.
async fn find_gateway_container() -> Result<Option<(String, String, String, Option<String>)>, String> {
/// The gateway container, if any.
async fn find_gateway_container() -> Result<Option<GatewayContainer>, String> {
let docker = get_docker()?;
let filters: HashMap<String, Vec<String>> = HashMap::from([(
@@ -493,7 +528,12 @@ async fn find_gateway_container() -> Result<Option<(String, String, String, Opti
let image_id = container.image_id.clone().filter(|s| !s.is_empty());
return Ok(Some((id, state, fingerprint, image_id)));
return Ok(Some(GatewayContainer {
id,
state,
fingerprint,
image_id,
}));
}
Ok(None)
@@ -716,17 +756,14 @@ async fn create_gateway_container(
Ok(response.id)
}
/// Stops a running container gracefully first (so in-flight LiteLLM requests
/// can drain, as before), then removes it, tolerating one already gone.
/// Stops a running container gracefully first, with Docker's default grace as
/// before (so in-flight LiteLLM requests can drain; the shorter
/// [`GATEWAY_STOP_GRACE_SECS`] is for app exit), then removes it, tolerating
/// one already gone.
async fn remove_gateway_container(docker: &Docker, id: &str, state: &str) -> Result<(), String> {
if state == "running" {
docker
.stop_container(
id,
Some(StopContainerOptions {
t: GATEWAY_STOP_GRACE_SECS,
}),
)
.stop_container(id, None::<StopContainerOptions>)
.await
.map_err(|e| format!("Failed to stop gateway container: {}", e))?;
}
@@ -777,7 +814,18 @@ async fn ensure_gateway_running_locked(
// Rotation id, not a hash of either secret — see `storage::secure`.
let secret_version = secure::get_gateway_secret_version()?.unwrap_or_default();
let (existing, pinned) = tokio::join!(find_gateway_container(), pinned_images());
let (existing, chosen) = (existing?, pinned?.chosen());
let existing = existing?;
// A failed image lookup must not fail a gateway that's fine as it is: it
// takes the same path as "no pinned image" and keeps the current image.
let mut lookup_error = None;
let chosen = match pinned {
Ok(pinned) => pinned.chosen(),
Err(e) => {
log::warn!("Gateway image check failed: {}", e);
lookup_error = Some(e);
None
}
};
let on_pin = chosen.is_some();
let image = match chosen {
Some(image) => image,
@@ -785,7 +833,7 @@ async fn ensure_gateway_running_locked(
// user pulls or builds). Never tear down a working gateway over that:
// keep it on the image it already runs — recreated with the new
// config if that changed — and let status report it as outdated.
None => match existing.as_ref().and_then(|c| c.3.clone()) {
None => match existing.as_ref().and_then(|c| c.image_id.clone()) {
Some(id) => {
log::warn!(
"Gateway image for LiteLLM {} not present; keeping the gateway on its current image {}",
@@ -798,10 +846,13 @@ async fn ensure_gateway_running_locked(
}
}
None => {
return Err(format!(
"Gateway image for LiteLLM {} not found. Please pull or build the image first.",
pinned_litellm_version()
))
return Err(match lookup_error {
Some(e) => e,
None => format!(
"Gateway image for LiteLLM {} not found. Please pull or build the image first.",
pinned_litellm_version()
),
})
}
},
};
@@ -814,7 +865,13 @@ async fn ensure_gateway_running_locked(
&image.id,
);
if let Some((id, state, existing_fingerprint, _)) = existing {
if let Some(GatewayContainer {
id,
state,
fingerprint: existing_fingerprint,
..
}) = existing
{
if existing_fingerprint == fingerprint {
if state == "running" {
return get_gateway_status(settings).await;
@@ -874,7 +931,7 @@ pub async fn stop_gateway_container() -> Result<(), String> {
let _guard = gateway_lock().lock().await;
let docker = get_docker()?;
if let Some((id, state, _, _)) = find_gateway_container().await? {
if let Some(GatewayContainer { id, state, .. }) = find_gateway_container().await? {
if state == "running" {
docker
.stop_container(
@@ -1083,19 +1140,34 @@ mod tests {
}
#[test]
fn only_latest_older_pins_and_older_builds_are_superseded() {
fn only_latest_and_older_pins_are_superseded() {
let v = pinned_litellm_version();
let current = local_image_ref();
assert!(is_superseded_local_build("triple-c-gateway:latest", &current));
assert!(is_superseded_local_build("triple-c-gateway:v1.82.6", &current));
assert!(is_superseded_local_build("triple-c-gateway:v1.82.6-0badc0de", &current));
// An earlier build of this pin, from a different Dockerfile/config.
assert!(is_superseded_local_build(&format!("triple-c-gateway:{v}-0badc0de"), &current));
assert!(!is_superseded_local_build(&current, &current));
assert!(!is_superseded_local_build("triple-c-gateway:v9.0.0-0badc0de", &current));
assert!(!is_superseded_local_build("triple-c-gateway:dev", &current));
assert!(!is_superseded_local_build("triple-c-gateway-other:latest", &current));
assert!(!is_superseded_local_build("ghcr.io/berriai/litellm:v1.82.6", &current));
assert!(is_superseded_local_build("triple-c-gateway:latest"));
assert!(is_superseded_local_build("triple-c-gateway:v1.82.6"));
assert!(is_superseded_local_build("triple-c-gateway:v1.82.6-0badc0de"));
// Any build of the current pin is kept, including another app's.
assert!(!is_superseded_local_build(&local_image_ref()));
assert!(!is_superseded_local_build(&format!("triple-c-gateway:{v}-0badc0de")));
assert!(!is_superseded_local_build("triple-c-gateway:v9.0.0-0badc0de"));
assert!(!is_superseded_local_build("triple-c-gateway:dev"));
assert!(!is_superseded_local_build("triple-c-gateway-other:latest"));
assert!(!is_superseded_local_build("ghcr.io/berriai/litellm:v1.82.6"));
}
#[test]
fn comment_edits_dont_change_the_local_build_tag() {
let a = "# about\nFROM x\n\n # indented note\nRUN y\n";
let b = "# reworded\nFROM x\nRUN y\n# trailing\n";
assert_eq!(significant_lines(a), significant_lines(b));
assert_ne!(significant_lines(a), significant_lines("FROM x\nRUN z\n"));
}
#[test]
fn registry_constants_agree() {
assert_eq!(
GATEWAY_REGISTRY_IMAGE,
format!("{}:{}", GATEWAY_REGISTRY_REPO, GATEWAY_LITELLM_VERSION)
);
}
#[test]
@@ -109,7 +109,7 @@ describe("GatewaySettings", () => {
render(<GatewaySettings />);
const stop = await screen.findByRole("button", { name: "Stop" });
expect(screen.getByText(/LiteLLM update available, Pull or Build/i)).toBeInTheDocument();
expect(screen.getByText(/image update available, Pull or Build/i)).toBeInTheDocument();
fireEvent.click(stop);
await waitFor(() => expect(stopGateway).toHaveBeenCalledTimes(1));
@@ -132,6 +132,6 @@ describe("GatewaySettings", () => {
expect(await screen.findByRole("button", { name: "Start" })).toBeInTheDocument();
// Starting it keeps the old LiteLLM, so say so.
expect(screen.getByText(/Stopped — LiteLLM update available/)).toBeInTheDocument();
expect(screen.getByText(/Stopped — gateway image update available/)).toBeInTheDocument();
});
});
@@ -193,11 +193,11 @@ export default function GatewaySettings() {
: status.image_outdated
? status.image_exists
? `Running on port ${status.port} — new gateway image ready, Stop and Start to apply`
: `Running on port ${status.port} — LiteLLM update available, Pull or Build to get it`
: `Running on port ${status.port} — gateway image update available, Pull or Build to get it`
: `Running on port ${status.port}`
: status?.container_exists
? status.image_outdated && !status.image_exists
? "Stopped — LiteLLM update available, Pull or Build to get it"
? "Stopped — gateway image update available, Pull or Build to get it"
: "Stopped"
: !status?.image_exists
? "No image"