Skip to main content

agentmux_srv/server/agent_handlers/
template_image.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The container image a new agent gets when it is created from a template (the new-agent form's image choice).
5//! Spec: docs/specs/SPEC_CONTAINER_AGENT_DEVELOPER_IMAGE_2026_10_29.md section 4.
6
7/// The image a new container agent runs in: the picker's choice between the two images AgentMux ships
8/// (`requested`), else the template's own. Any other image is refused (an agent's own image is set by editing its
9/// definition), and a native agent has none to choose.
10pub(crate) fn chosen_container_image(requested: Option<&str>, agent_type: &str, template_image: &str) -> Result<String, String> {
11    use crate::backend::container::image::{DEFAULT_AGENT_IMAGE, DEFAULT_DEVELOPER_IMAGE};
12    match requested.map(str::trim) {
13        Some(image) if crate::backend::runtime_adapter::RuntimeKind::of_agent_type(agent_type).is_container() => {
14            if image == DEFAULT_AGENT_IMAGE || image == DEFAULT_DEVELOPER_IMAGE {
15                Ok(image.to_string())
16            } else {
17                Err(format!("container_image must be {DEFAULT_AGENT_IMAGE} or {DEFAULT_DEVELOPER_IMAGE}, not {image:?}"))
18            }
19        }
20        // The retired image is never carried to a new agent: empty is this version's base image (#47725).
21        _ if crate::backend::container::image::is_legacy_agent_image(template_image) => Ok(String::new()),
22        _ => Ok(template_image.to_string()),
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::chosen_container_image;
29    use crate::backend::container::image::{DEFAULT_AGENT_IMAGE, DEFAULT_DEVELOPER_IMAGE};
30
31    #[test]
32    fn a_container_agent_gets_the_image_the_picker_chose_and_nothing_else_is_accepted() {
33        assert_eq!(chosen_container_image(Some(DEFAULT_DEVELOPER_IMAGE), "container", "").unwrap(), DEFAULT_DEVELOPER_IMAGE);
34        assert_eq!(chosen_container_image(Some(&format!(" {DEFAULT_AGENT_IMAGE} ")), "container", "x/y:1").unwrap(), DEFAULT_AGENT_IMAGE);
35        assert!(chosen_container_image(Some("my/own:1"), "container", "").unwrap_err().contains("container_image must be"));
36        assert!(chosen_container_image(Some("ghcr.io/agentmuxai/agent-base:0.59.1"), "container", "").is_err(), "a pinned version is set on the definition, not here");
37    }
38
39    #[test]
40    fn the_retired_image_is_not_carried_from_the_source_to_a_new_agent() {
41        let legacy = "ghcr.io/agentmuxai/agent-claude:latest";
42        assert_eq!(chosen_container_image(None, "container", legacy).unwrap(), "");
43        assert_eq!(chosen_container_image(None, "host", legacy).unwrap(), "");
44        assert_eq!(chosen_container_image(Some(DEFAULT_DEVELOPER_IMAGE), "container", legacy).unwrap(), DEFAULT_DEVELOPER_IMAGE);
45    }
46
47    #[test]
48    fn without_a_choice_or_without_a_container_the_template_keeps_its_image() {
49        assert_eq!(chosen_container_image(None, "container", "tpl/image:2").unwrap(), "tpl/image:2");
50        assert_eq!(chosen_container_image(Some(DEFAULT_DEVELOPER_IMAGE), "host", "tpl/image:2").unwrap(), "tpl/image:2", "a native agent has no image to choose");
51        assert_eq!(chosen_container_image(Some("my/own:1"), "host", "").unwrap(), "", "and a native agent's request is not validated");
52    }
53}