Skip to main content

agentmux_srv_core/backend/agent_runtime/
choice.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What `Agents update` with `model` or `effort` writes to a pane (the pick itself is `server::app_api::agent_runtime_pick`).
5
6use serde_json::{json, Value};
7
8use super::{flag_value, is_claude_model, without_flags, without_model_flags, EFFORT_LEVELS};
9use crate::backend::obj::MetaMapType;
10
11/// What `Agents update` with `model` or `effort` writes to a pane: the same two keys the Runtime menu writes
12/// (`frontend/app/view/agent/runtime-apply.ts`).
13pub struct RuntimeChoice {
14    /// The value for `agent:runtime`: what the pane has, with the new model and effort laid over it.
15    pub runtime: Value,
16    /// The value for `cmd:args`: the stored argv without its own `--model` and `--effort`, so the next launch builds
17    /// both from `runtime` ([`with_runtime_flags`]) and the two cannot disagree.
18    pub cli_args: Option<Vec<String>>,
19}
20
21/// The pane's runtime once `model` and `effort` (each only when given) are changed. `keep_effort_flag` leaves a
22/// `--effort` in the argv alone (the definition's own provider flags carry one).
23pub fn runtime_choice(meta: &MetaMapType, model: Option<&str>, effort: Option<&str>, keep_effort_flag: bool) -> RuntimeChoice {
24    let mut runtime = meta.get("agent:runtime").and_then(Value::as_object).cloned().unwrap_or_default();
25    if let Some(model) = model {
26        runtime.insert("model".to_string(), json!(model));
27    }
28    if let Some(effort) = effort {
29        runtime.insert("effort".to_string(), json!(effort));
30    }
31    let cli_args = meta.get("cmd:args").and_then(Value::as_array).map(|a| {
32        let args: Vec<String> = a.iter().filter_map(Value::as_str).map(str::to_string).collect();
33        let args = without_model_flags(args);
34        if keep_effort_flag { args } else { without_flags(args, &["--effort"]) }
35    });
36    RuntimeChoice { runtime: Value::Object(runtime), cli_args }
37}
38
39/// Whether the agent definition's own provider flags (`agent:provider_flags`) carry a model and an effort: that is
40/// the operator's choice, which a runtime pick does not override (`with_runtime_flags`).
41pub fn provider_flags_pin(meta: &MetaMapType) -> (bool, bool) {
42    flags_pin(&crate::backend::obj::meta_get_string(meta, "agent:provider_flags", ""))
43}
44
45/// [`provider_flags_pin`] for the definition's flags as stored (`AgentDefinition::provider_flags`), which an agent with
46/// no pane open has only here.
47pub fn flags_pin(provider_flags: &str) -> (bool, bool) {
48    let flags: Vec<String> = provider_flags.split_whitespace().map(str::to_string).collect();
49    (flag_value(&flags, &["--model", "-m"]).is_some(), flag_value(&flags, &["--effort"]).is_some())
50}
51
52/// Whether `model` is one a Claude agent can be given: an alias (`opus`, `sonnet`, `haiku`) or a concrete id
53/// (`claude-fable-5-2`), with no space or control character in it.
54pub fn is_claude_model_name(model: &str) -> bool {
55    is_claude_model(model) && !model.chars().any(|c| c.is_whitespace() || c.is_control())
56}
57
58/// The efforts a Claude agent takes.
59pub fn is_claude_effort(effort: &str) -> bool {
60    EFFORT_LEVELS.contains(&effort)
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    fn meta_of(v: serde_json::Value) -> MetaMapType {
68        v.as_object().unwrap().clone().into_iter().collect()
69    }
70
71    #[test]
72    fn a_runtime_choice_lays_the_pick_over_the_runtime_and_takes_the_models_flags_out_of_the_argv() {
73        let meta = meta_of(json!({
74            "cmd:args": ["-p", "--model", "sonnet", "--effort=high", "--verbose", "-m", "x"],
75            "agent:runtime": { "model": "sonnet", "effort": "high", "permissionMode": "plan" },
76        }));
77        let c = runtime_choice(&meta, Some("claude-fable-5-2"), None, false);
78        assert_eq!(c.runtime, json!({ "model": "claude-fable-5-2", "effort": "high", "permissionMode": "plan" }));
79        assert_eq!(c.cli_args.unwrap(), ["-p", "--verbose"]);
80        let kept = runtime_choice(&meta, None, Some("max"), true).cli_args.unwrap();
81        assert!(kept.contains(&"--effort=high".to_string()), "the definition's own effort flag stays: {kept:?}");
82        let bare = runtime_choice(&meta_of(json!({})), Some("opus"), Some("low"), false);
83        assert_eq!(bare.runtime, json!({ "model": "opus", "effort": "low" }));
84        assert!(bare.cli_args.is_none(), "a pane with no stored argv is left without one");
85    }
86
87    #[test]
88    fn the_definitions_own_model_and_effort_flags_are_seen_and_model_names_are_checked() {
89        let pinned = meta_of(json!({ "agent:provider_flags": "--model opus --verbose" }));
90        assert_eq!(provider_flags_pin(&pinned), (true, false));
91        assert_eq!(provider_flags_pin(&meta_of(json!({ "agent:provider_flags": "--effort=low" }))), (false, true));
92        assert_eq!(provider_flags_pin(&meta_of(json!({}))), (false, false));
93        assert_eq!(flags_pin("--effort high -m opus"), (true, true));
94        assert!(is_claude_model_name("claude-fable-5-2") && is_claude_model_name("sonnet"));
95        assert!(!is_claude_model_name("gpt-5") && !is_claude_model_name("claude opus") && !is_claude_model_name(""));
96        assert!(is_claude_effort("xhigh") && !is_claude_effort("ultra"));
97    }
98}