pub struct AgentHandle { /* private fields */ }Expand description
A handle to an agent instance that manages its lifecycle.
AgentHandle wraps an AgentCore and provides:
- Socket path and PID file tracking
- Lifecycle management (shutdown, status checks)
- Thread-safe access to the underlying
AgentCore - Idle timeout and key locking
Unlike the previous global static pattern, multiple AgentHandle instances
can coexist, enabling proper testing and multi-agent scenarios.
Implementations§
Source§impl AgentHandle
impl AgentHandle
Sourcepub fn new(socket_path: PathBuf) -> Self
pub fn new(socket_path: PathBuf) -> Self
Creates a new agent handle with the specified socket path.
Sourcepub fn with_timeout(socket_path: PathBuf, idle_timeout: Duration) -> Self
pub fn with_timeout(socket_path: PathBuf, idle_timeout: Duration) -> Self
Creates a new agent handle with the specified socket path and timeout.
Sourcepub fn with_pid_file(socket_path: PathBuf, pid_file: PathBuf) -> Self
pub fn with_pid_file(socket_path: PathBuf, pid_file: PathBuf) -> Self
Creates a new agent handle with socket and PID file paths.
Sourcepub fn with_pid_file_and_timeout(
socket_path: PathBuf,
pid_file: PathBuf,
idle_timeout: Duration,
) -> Self
pub fn with_pid_file_and_timeout( socket_path: PathBuf, pid_file: PathBuf, idle_timeout: Duration, ) -> Self
Creates a new agent handle with socket, PID file, and custom timeout.
Sourcepub fn from_core(core: AgentCore, socket_path: PathBuf) -> Self
pub fn from_core(core: AgentCore, socket_path: PathBuf) -> Self
Creates an agent handle from an existing AgentCore.
Sourcepub fn socket_path(&self) -> &PathBuf
pub fn socket_path(&self) -> &PathBuf
Returns the socket path for this agent.
Sourcepub fn set_pid_file(&mut self, path: PathBuf)
pub fn set_pid_file(&mut self, path: PathBuf)
Sets the PID file path.
Sourcepub fn lock(&self) -> Result<MutexGuard<'_, AgentCore>, AgentError>
pub fn lock(&self) -> Result<MutexGuard<'_, AgentCore>, AgentError>
Sourcepub fn core_arc(&self) -> Arc<Mutex<AgentCore>>
pub fn core_arc(&self) -> Arc<Mutex<AgentCore>>
Returns a clone of the inner Arc<Mutex
Sourcepub fn is_running(&self) -> bool
pub fn is_running(&self) -> bool
Returns whether the agent is currently running.
Sourcepub fn set_running(&self, running: bool)
pub fn set_running(&self, running: bool)
Marks the agent as running.
Sourcepub fn idle_timeout(&self) -> Duration
pub fn idle_timeout(&self) -> Duration
Returns the configured idle timeout duration.
Sourcepub fn idle_duration(&self) -> Duration
pub fn idle_duration(&self) -> Duration
Returns the duration since the last activity.
Sourcepub fn is_idle_timed_out(&self) -> bool
pub fn is_idle_timed_out(&self) -> bool
Returns whether the agent has exceeded the idle timeout.
Sourcepub fn is_agent_locked(&self) -> bool
pub fn is_agent_locked(&self) -> bool
Returns whether the agent is currently locked.
Sourcepub fn lock_agent(&self) -> Result<(), AgentError>
pub fn lock_agent(&self) -> Result<(), AgentError>
Locks the agent, clearing all keys from memory.
After locking, sign operations will fail with AgentError::AgentLocked.
Sourcepub fn unlock_agent(&self)
pub fn unlock_agent(&self)
Unlocks the agent (marks as unlocked).
Note: This only clears the locked flag. Keys must be re-loaded separately
using register_key or the CLI auths agent unlock command.
Sourcepub fn check_idle_timeout(&self) -> Result<bool, AgentError>
pub fn check_idle_timeout(&self) -> Result<bool, AgentError>
Checks idle timeout and locks the agent if exceeded.
Call this periodically from a background task.
Sourcepub fn shutdown(&self) -> Result<(), AgentError>
pub fn shutdown(&self) -> Result<(), AgentError>
Shuts down the agent, clearing all keys and resources.
This method:
- Clears all keys from the agent core (zeroizing sensitive data)
- Marks the agent as not running
- Optionally removes the socket file and PID file
Sourcepub fn key_count(&self) -> Result<usize, AgentError>
pub fn key_count(&self) -> Result<usize, AgentError>
Returns the number of keys currently loaded in the agent.
Sourcepub fn public_keys(&self) -> Result<Vec<Vec<u8>>, AgentError>
pub fn public_keys(&self) -> Result<Vec<Vec<u8>>, AgentError>
Returns all public key bytes currently registered.
Sourcepub fn register_key(
&self,
pkcs8_bytes: Zeroizing<Vec<u8>>,
) -> Result<(), AgentError>
pub fn register_key( &self, pkcs8_bytes: Zeroizing<Vec<u8>>, ) -> Result<(), AgentError>
Registers a key in the agent core.