Skip to main content

agentmux_srv_core/backend/history/
agent_sessions.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! One agent's sessions from its record, for the History pane
5//! (`history.agent_sessions`).
6
7use super::HistoryService;
8
9impl HistoryService {
10    /// Agent `uid`'s sessions as its record holds them (the History
11    /// pane, `history.agent_sessions`), most recent first. The first read
12    /// of a zone scans it; later reads only what was appended.
13    pub fn agent_sessions(&self, uid: &str) -> crate::backend::rpc_types::HistoryAgentSessions {
14        let (sessions, problems) = self.records.sessions_of(uid);
15        let sessions = sessions
16            .into_iter()
17            .filter(|s| !s.ranges.is_empty())
18            .map(|s| crate::backend::rpc_types::HistorySession {
19                bytes: s.ranges.iter().map(|(_, from, to)| (to - from).max(0) as u64).sum(),
20                archived: s.ranges.iter().all(|(zone, _, _)| zone.contains(":archive:")),
21                zone: s.ranges.last().map(|(zone, _, _)| zone.clone()).unwrap_or_default(),
22                end_byte: s.ranges.last().map(|(_, _, end)| (*end).max(0) as u64).unwrap_or(0),
23                session_id: s.session_id,
24                cwd: s.cwd.unwrap_or_default(),
25                first_ms: s.first_ms.max(0) as u64,
26                last_ms: s.last_ms.max(0) as u64,
27            })
28            .collect();
29        crate::backend::rpc_types::HistoryAgentSessions { sessions, problems }
30    }
31}