Skip to main content

vtcode_core/tools/registry/
pty_facade.rs

1//! PTY-related ToolRegistry accessors.
2
3use std::future::Future;
4use std::sync::Arc;
5
6use anyhow::Result;
7
8use crate::config::PtyConfig;
9use crate::tools::exec_session::ExecSessionManager;
10use crate::tools::pty::PtyManager;
11
12use super::ToolRegistry;
13use super::pty;
14
15impl ToolRegistry {
16    pub fn pty_manager(&self) -> &PtyManager {
17        self.pty_sessions.manager()
18    }
19
20    pub fn pty_config(&self) -> &PtyConfig {
21        self.pty_sessions.config()
22    }
23
24    pub fn can_start_pty_session(&self) -> bool {
25        self.pty_sessions.can_start_session()
26    }
27
28    pub fn start_pty_session(&self) -> Result<pty::PtySessionGuard> {
29        self.pty_sessions.start_session()
30    }
31
32    pub fn end_pty_session(&self) {
33        self.pty_sessions.end_session();
34    }
35
36    pub fn active_pty_sessions(&self) -> usize {
37        self.pty_sessions.active_sessions()
38    }
39
40    pub fn terminate_all_pty_sessions(&self) {
41        self.pty_sessions.terminate_all();
42    }
43
44    /// Inline-delegating wrapper over [`pty::PtySessionManager::terminate_all_async`].
45    /// Returns the inner future directly to avoid an extra coroutine state machine
46    /// (per the async-state-machine bloat audit, section 16).
47    pub fn terminate_all_pty_sessions_async(&self) -> impl Future<Output = Result<()>> + '_ {
48        self.pty_sessions.terminate_all_async()
49    }
50
51    /// Inline-delegating wrapper over
52    /// [`Self::terminate_all_exec_sessions_async`].
53    pub fn terminate_all_exec_sessions_async(&self) -> impl Future<Output = Result<()>> + '_ {
54        self.exec_sessions.terminate_all_sessions_async()
55    }
56
57    pub fn exec_session_manager(&self) -> ExecSessionManager {
58        self.exec_sessions.clone()
59    }
60
61    /// Set the active PTY sessions counter for tracking
62    pub fn set_active_pty_sessions(&self, counter: Arc<std::sync::atomic::AtomicUsize>) {
63        if let Ok(mut guard) = self.active_pty_sessions.write() {
64            *guard = Some(counter);
65        }
66    }
67
68    /// Increment active PTY sessions count
69    pub fn increment_active_pty_sessions(&self) {
70        if let Some(counter) = self
71            .active_pty_sessions
72            .read()
73            .ok()
74            .and_then(|g| g.as_ref().map(Arc::clone))
75        {
76            counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
77        }
78    }
79
80    /// Decrement active PTY sessions count
81    pub fn decrement_active_pty_sessions(&self) {
82        if let Some(counter) = self
83            .active_pty_sessions
84            .read()
85            .ok()
86            .and_then(|g| g.as_ref().map(Arc::clone))
87        {
88            counter.fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
89        }
90    }
91
92    /// Get the current active PTY sessions count
93    pub fn active_pty_sessions_count(&self) -> usize {
94        self.active_pty_sessions
95            .read()
96            .ok()
97            .and_then(|g| {
98                g.as_ref()
99                    .map(|c| c.load(std::sync::atomic::Ordering::Relaxed))
100            })
101            .unwrap_or(0)
102    }
103}