Skip to main content

agentmux_srv_core/backend/
agent_runtime_latest.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The latest `agentruntime` event of each pane, kept in memory so a reader that did not subscribe (the
5//! live facts `Agents list` and `Agents status` read) can ask what a pane runs with now: what it was
6//! asked to run (`model`, `effort`, `permission_mode`) and what its CLI reported using
7//! (`effective_model`, `effective_effort`). Written where the event is published, so it is as current as
8//! the event; lost with the process, like the event's own replay.
9
10use std::collections::HashMap;
11use std::sync::LazyLock;
12
13use crate::backend::agent_runtime::AgentRuntimeEvent;
14
15static LATEST: LazyLock<parking_lot::Mutex<HashMap<String, AgentRuntimeEvent>>> = LazyLock::new(Default::default);
16
17/// Remember `event` as its pane's latest.
18pub fn record(event: &AgentRuntimeEvent) {
19    LATEST.lock().insert(event.blockid.clone(), event.clone());
20}
21
22/// `block_id`'s latest event, if one was published since srv started.
23pub fn get(block_id: &str) -> Option<AgentRuntimeEvent> {
24    LATEST.lock().get(block_id).cloned()
25}
26
27/// Forget `block_id` (its pane closed).
28pub fn forget(block_id: &str) {
29    LATEST.lock().remove(block_id);
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use crate::backend::agent_runtime::agent_runtime_event;
36
37    #[test]
38    fn the_latest_event_of_a_pane_replaces_the_one_before_and_forget_drops_it() {
39        let id = format!("latest-{}", uuid::Uuid::new_v4());
40        assert!(get(&id).is_none());
41        record(&agent_runtime_event(&id, None, false, None));
42        assert!(!get(&id).unwrap().running);
43        let mut later = agent_runtime_event(&id, None, true, None);
44        later.model = Some("claude-sonnet-5-5".to_string());
45        record(&later);
46        assert_eq!(get(&id).unwrap().model.as_deref(), Some("claude-sonnet-5-5"));
47        assert!(get(&id).unwrap().restart_pending);
48        forget(&id);
49        assert!(get(&id).is_none());
50    }
51}