Skip to main content

agentmux_srv_core/backend/
agent_runtime_container.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::backend::agent_runtime::{agent_runtime_event, AgentRuntimeEvent};
5
6/// The `agentruntime` event of a container agent: each turn is its own exec, so there is no process to describe, only the
7/// model its last reply came from (`observed`), which the pane's chip holds against the model the pane is set to run.
8pub fn container_reply_event(block_id: &str, observed: Option<String>) -> AgentRuntimeEvent {
9    let mut event = agent_runtime_event(block_id, None, false, None);
10    event.observed_model = observed;
11    event
12}
13
14/// Tell the pane which model a container agent's last reply came from, when that changed since it was last said.
15pub fn publish_container_reply(broker: &crate::backend::mps::Broker, block_id: &str) {
16    let observed = crate::backend::context_reading::get(block_id).and_then(|r| r.model);
17    let said = crate::backend::agent_runtime_latest::get(block_id).and_then(|e| e.observed_model);
18    if observed == said {
19        return;
20    }
21    let event = container_reply_event(block_id, observed);
22    crate::backend::agent_runtime_latest::record(&event);
23    broker.publish_event::<crate::backend::mps_events::AgentRuntime>(&event, vec![format!("block:{block_id}")], 1);
24}
25
26/// Tell the pane which model a running process's reply came from, when that changed since its runtime event was last
27/// said (the event is built at spawn and when the effective runtime changes, so the first reply would otherwise stay
28/// unsaid until the next turn). Nothing to say while no process runs: its spawn event says what it runs.
29pub fn publish_running_reply(broker: &crate::backend::mps::Broker, block_id: &str) {
30    let Some(mut event) = crate::backend::agent_runtime_latest::get(block_id).filter(|e| e.running) else { return };
31    let observed = crate::backend::context_reading::get(block_id).and_then(|r| r.model);
32    if observed == event.observed_model {
33        return;
34    }
35    event.observed_model = observed;
36    crate::backend::agent_runtime_latest::record(&event);
37    broker.publish_event::<crate::backend::mps_events::AgentRuntime>(&event, vec![format!("block:{block_id}")], 1);
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44
45    #[test]
46    fn a_container_agents_event_names_only_the_model_of_its_last_reply() {
47        let event = container_reply_event("blk", Some("claude-fable-5-2".to_string()));
48        assert!(!event.running, "no process to describe: each turn is its own exec");
49        assert_eq!(event.observed_model.as_deref(), Some("claude-fable-5-2"));
50        assert_eq!(event.model, None);
51        assert_eq!(container_reply_event("blk", None).observed_model, None);
52    }
53
54    #[test]
55    fn a_running_process_says_its_first_reply_model_once_and_again_only_when_it_changes() {
56        let block = format!("ct-{}", uuid::Uuid::new_v4());
57        let broker = crate::backend::mps::Broker::new();
58        let reply = |model: &str, id: &str| {
59            crate::backend::context_reading::observe_claude_frame(
60                &block,
61                &serde_json::json!({"type": "assistant", "message": {"id": id, "model": model,
62                    "usage": {"input_tokens": 2, "cache_creation_input_tokens": 1000, "cache_read_input_tokens": 99_000}}}),
63            );
64        };
65        // Nothing runs: nothing to say.
66        reply("claude-fable-5-2", "m0");
67        publish_running_reply(&broker, &block);
68        assert!(crate::backend::agent_runtime_latest::get(&block).is_none());
69        // The spawn event says what it runs, with no reply yet.
70        crate::backend::context_reading::forget(&block);
71        let mut spawned = crate::backend::agent_runtime::agent_runtime_event(&block, Some(&crate::backend::agent_runtime::SpawnRuntime::default()), false, None);
72        spawned.observed_model = None;
73        crate::backend::agent_runtime_latest::record(&spawned);
74        publish_running_reply(&broker, &block);
75        assert_eq!(crate::backend::agent_runtime_latest::get(&block).unwrap().observed_model, None, "no reply yet");
76        // Its first reply names the model: the event is said again, now with it, and still describes the process.
77        reply("claude-fable-5-2", "m1");
78        publish_running_reply(&broker, &block);
79        let said = crate::backend::agent_runtime_latest::get(&block).unwrap();
80        assert_eq!(said.observed_model.as_deref(), Some("claude-fable-5-2"));
81        assert!(said.running, "it is still the running process's event");
82        // The same model again changes nothing; a different one is said.
83        crate::backend::agent_runtime_latest::record(&AgentRuntimeEvent { model: Some("marker".into()), ..said.clone() });
84        reply("claude-fable-5-2", "m2");
85        publish_running_reply(&broker, &block);
86        assert_eq!(crate::backend::agent_runtime_latest::get(&block).unwrap().model.as_deref(), Some("marker"), "unchanged: not said again");
87        reply("claude-sonnet-5-5", "m3");
88        publish_running_reply(&broker, &block);
89        assert_eq!(crate::backend::agent_runtime_latest::get(&block).unwrap().observed_model.as_deref(), Some("claude-sonnet-5-5"));
90        crate::backend::context_reading::forget(&block);
91        crate::backend::agent_runtime_latest::forget(&block);
92    }
93
94    #[test]
95    fn a_container_agents_reply_model_is_said_when_it_changes_and_not_again_until_it_does() {
96        let block = format!("ct-{}", uuid::Uuid::new_v4());
97        let broker = crate::backend::mps::Broker::new();
98        publish_container_reply(&broker, &block);
99        assert!(crate::backend::agent_runtime_latest::get(&block).is_none(), "nothing to say before a reply");
100        crate::backend::context_reading::observe_claude_frame(
101            &block,
102            &serde_json::json!({"type": "assistant", "message": {"id": "m1", "model": "claude-fable-5-2",
103                "usage": {"input_tokens": 2, "cache_creation_input_tokens": 1000, "cache_read_input_tokens": 99_000}}}),
104        );
105        publish_container_reply(&broker, &block);
106        let said = crate::backend::agent_runtime_latest::get(&block).unwrap();
107        assert_eq!(said.observed_model.as_deref(), Some("claude-fable-5-2"));
108        assert!(!said.running);
109        // A turn started on another model drops that reply: the pane is told it has none yet.
110        crate::backend::context_reading::drop_model_other_than(&block, "sonnet");
111        publish_container_reply(&broker, &block);
112        assert_eq!(crate::backend::agent_runtime_latest::get(&block).unwrap().observed_model, None);
113        crate::backend::context_reading::forget(&block);
114        crate::backend::agent_runtime_latest::forget(&block);
115    }
116}