Skip to main content

AgentHandle

Struct AgentHandle 

Source
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

Source

pub fn new(socket_path: PathBuf) -> AgentHandle

Creates a new agent handle with the specified socket path.

Source

pub fn with_timeout(socket_path: PathBuf, idle_timeout: Duration) -> AgentHandle

Creates a new agent handle with the specified socket path and idle timeout, using the default absolute unlock cap.

Source

pub fn with_unlock_cap( socket_path: PathBuf, idle_timeout: Duration, max_unlock_ttl: Duration, ) -> AgentHandle

Creates a new agent handle with an explicit idle timeout and absolute unlock cap.

The idle timeout locks the agent after a period of no signing (a “walked away” control); the unlock cap locks it a fixed time after it was last unlocked, regardless of activity, so continuous signing cannot keep it unlocked forever.

Args:

  • socket_path: The Unix-domain socket path.
  • idle_timeout: Sliding inactivity timeout (0 disables).
  • max_unlock_ttl: Absolute cap measured from the last unlock (0 disables).

Usage:

let handle = AgentHandle::with_unlock_cap(path, idle, cap);
Source

pub fn with_pid_file(socket_path: PathBuf, pid_file: PathBuf) -> AgentHandle

Creates a new agent handle with socket and PID file paths.

Source

pub fn with_pid_file_and_timeout( socket_path: PathBuf, pid_file: PathBuf, idle_timeout: Duration, ) -> AgentHandle

Creates a new agent handle with socket, PID file, and custom timeout.

Source

pub fn from_core(core: AgentCore, socket_path: PathBuf) -> AgentHandle

Creates an agent handle from an existing AgentCore.

Source

pub fn socket_path(&self) -> &PathBuf

Returns the socket path for this agent.

Source

pub fn pid_file(&self) -> Option<&PathBuf>

Returns the PID file path, if configured.

Source

pub fn set_pid_file(&mut self, path: PathBuf)

Sets the PID file path.

Source

pub fn lock(&self) -> Result<MutexGuard<'_, AgentCore>, AgentError>

Acquires a lock on the agent core.

§Errors

Returns AgentError::MutexError if the mutex is poisoned.

Source

pub fn core_arc(&self) -> Arc<Mutex<AgentCore>>

Returns a clone of the inner Arc<Mutex<AgentCore>> for sharing.

Source

pub fn is_running(&self) -> bool

Returns whether the agent is currently running.

Source

pub fn set_running(&self, running: bool)

Marks the agent as running.

Source

pub fn idle_timeout(&self) -> Duration

Returns the configured idle timeout duration.

Source

pub fn touch(&self)

Records activity, resetting the idle timer.

Source

pub fn idle_duration(&self) -> Duration

Returns the duration since the last activity.

Source

pub fn is_idle_timed_out(&self) -> bool

Returns whether the agent has exceeded the idle timeout.

Source

pub fn max_unlock_ttl(&self) -> Duration

Returns the absolute cap on how long a single unlock keeps signing capability.

Source

pub fn unlock_age(&self) -> Duration

Returns the time elapsed since the agent was last unlocked.

Source

pub fn is_unlock_window_expired(&self) -> bool

Returns whether the agent has been unlocked longer than its absolute cap.

Unlike the idle timeout, this does not reset on activity — it bounds the total lifetime of a single unlock so continuous signing cannot keep the agent unlocked indefinitely. A cap of 0 disables it.

Source

pub fn is_agent_locked(&self) -> bool

Returns whether the agent is currently locked.

Source

pub fn lock_agent(&self) -> Result<(), AgentError>

Locks the agent, clearing all keys from memory.

After locking, sign operations will fail with AgentError::AgentLocked.

Source

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.

Source

pub fn check_idle_timeout(&self) -> Result<bool, AgentError>

Locks the agent if its unlock window has ended, clearing its keys.

The agent locks when it has been idle past the idle timeout (a “walked away” control) or when it has been unlocked longer than the absolute cap (a backstop so continuous signing cannot keep it unlocked indefinitely). Call this periodically from a background task. Neither control stops a process running as the same user from using the agent within the window — they only bound the window.

Source

pub fn shutdown(&self) -> Result<(), AgentError>

Shuts down the agent, clearing all keys and resources.

This method:

  1. Clears all keys from the agent core (zeroizing sensitive data)
  2. Marks the agent as not running
  3. Optionally removes the socket file and PID file
Source

pub fn key_count(&self) -> Result<usize, AgentError>

Returns the number of keys currently loaded in the agent.

Source

pub fn public_keys(&self) -> Result<Vec<Vec<u8>>, AgentError>

Returns all public key bytes currently registered.

Source

pub fn register_key( &self, pkcs8_bytes: Zeroizing<Vec<u8>>, ) -> Result<(), AgentError>

Registers a key in the agent core.

Source

pub fn sign(&self, pubkey: &[u8], data: &[u8]) -> Result<Vec<u8>, AgentError>

Signs data using a key in the agent core.

§Errors

Returns AgentError::AgentLocked if the agent is locked.

Trait Implementations§

Source§

impl Clone for AgentHandle

Source§

fn clone(&self) -> AgentHandle

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AgentHandle

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more